Ajouter pour le boulanger la possibilité de créer des utilisateurs. Envoi d'un email à l'utilisateur pour le prévenir en lui indiquant ses identifiants. Ajout d'un helper pour générer des mots de passe de la longueur de notre choix.master
use yii\filters\VerbFilter; | use yii\filters\VerbFilter; | ||||
use yii\filters\AccessControl; | use yii\filters\AccessControl; | ||||
use common\helpers\Upload ; | use common\helpers\Upload ; | ||||
use common\helpers\Password ; | |||||
use common\models\UserEtablissement ; | |||||
use common\models\Etablissement ; | |||||
/** | /** | ||||
* UserController implements the CRUD actions for User model. | * UserController implements the CRUD actions for User model. | ||||
'allow' => true, | 'allow' => true, | ||||
'roles' => ['@'], | 'roles' => ['@'], | ||||
'matchCallback' => function ($rule, $action) { | 'matchCallback' => function ($rule, $action) { | ||||
if($action->actionMethod == 'actionIndex') | |||||
if($action->actionMethod == 'actionIndex' || | |||||
$action->actionMethod == 'actionCreate') | |||||
{ | { | ||||
return Yii::$app->user->identity->status == USER::STATUS_ADMIN | return Yii::$app->user->identity->status == USER::STATUS_ADMIN | ||||
|| Yii::$app->user->identity->status == USER::STATUS_BOULANGER ; | || Yii::$app->user->identity->status == USER::STATUS_BOULANGER ; | ||||
{ | { | ||||
$model = new User(); | $model = new User(); | ||||
if ($model->load(Yii::$app->request->post()) && $model->save()) { | |||||
return $this->redirect(['view', 'id' => $model->id]); | |||||
if ($model->load(Yii::$app->request->post()) && $model->validate()) { | |||||
// save use | |||||
$password = Password::generate() ; | |||||
$model->setPassword($password); | |||||
$model->generateAuthKey(); | |||||
$model->username = $model->email ; | |||||
$model->save() ; | |||||
// liaison etablissement / user | |||||
$user_etablissement = new UserEtablissement() ; | |||||
$user_etablissement->id_user = $model->id ; | |||||
$user_etablissement->id_etablissement = Yii::$app->user->identity->id_etablissement ; | |||||
$user_etablissement->save() ; | |||||
// send mail | |||||
$etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ; | |||||
Yii::$app->mailer->compose() ; | |||||
$mail = Yii::$app->mailer->compose( | |||||
['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'], | |||||
['user' => $model, 'etablissement' => $etablissement, 'password' => $password]) | |||||
->setTo($model->email) | |||||
->setFrom(['contact@laboiteapain.net' => 'La boîte à pain']) | |||||
->setSubject('[La boîte à pain] Inscription') | |||||
->send() ; | |||||
return $this->redirect(['index']); | |||||
} else { | } else { | ||||
return $this->render('create', [ | return $this->render('create', [ | ||||
'model' => $model, | 'model' => $model, |
<div class="user-form"> | <div class="user-form"> | ||||
<?php $form = ActiveForm::begin(); ?> | <?php $form = ActiveForm::begin(); ?> | ||||
<p>Si cette option n'est pas cochée, l'utilisateur ne pourra commander que 3 pains à la fois.</p> | |||||
<?= $form->field($model, 'confiance')->checkbox() ?> | |||||
<?= $form->field($model, 'nom')->textInput() ?> | <?= $form->field($model, 'nom')->textInput() ?> | ||||
<?= $form->field($model, 'prenom')->textInput() ?> | <?= $form->field($model, 'prenom')->textInput() ?> | ||||
<?= $form->field($model, 'telephone')->textInput() ?> | <?= $form->field($model, 'telephone')->textInput() ?> | ||||
<?= $form->field($model, 'email')->textInput() ?> | <?= $form->field($model, 'email')->textInput() ?> | ||||
<?= $form->field($model, 'status')->textInput() ?> | |||||
<?= $form->field($model, 'adresse')->textarea() ?> | |||||
<div class="form-group"> | <div class="form-group"> | ||||
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | |||||
<?= Html::submitButton($model->isNewRecord ? 'Ajouter' : 'Modifier', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | |||||
</div> | </div> | ||||
<?php ActiveForm::end(); ?> | <?php ActiveForm::end(); ?> |
/* @var $this yii\web\View */ | /* @var $this yii\web\View */ | ||||
/* @var $model common\models\User */ | /* @var $model common\models\User */ | ||||
$this->title = 'Create User'; | |||||
$this->params['breadcrumbs'][] = ['label' => 'Users', 'url' => ['index']]; | |||||
$this->params['breadcrumbs'][] = $this->title; | |||||
$this->title = 'Ajouter un utilisateur'; | |||||
$this->params['breadcrumbs'][] = ['label' => 'Utilisateurs', 'url' => ['index']]; | |||||
$this->params['breadcrumbs'][] = 'Ajouter' ; | |||||
?> | ?> | ||||
<div class="user-create"> | <div class="user-create"> | ||||
?> | ?> | ||||
<div class="user-index"> | <div class="user-index"> | ||||
<h1><?= Html::encode($this->title) ?> <?= Html::a('<span class="glyphicon glyphicon-envelope"></span> Liste des emails', ['mail'], ['class' => 'btn btn-default']) ?></h1> | |||||
<h1> | |||||
<?= Html::encode($this->title) ?> | |||||
<?= Html::a('Ajouter', ['create'], ['class' => 'btn btn-primary']) ?> | |||||
<?= Html::a('<span class="glyphicon glyphicon-envelope"></span> Liste des emails', ['mail'], ['class' => 'btn btn-default']) ?> | |||||
</h1> | |||||
<?= GridView::widget([ | <?= GridView::widget([ | ||||
'dataProvider' => $dataProvider, | 'dataProvider' => $dataProvider, |
/* @var $this yii\web\View */ | /* @var $this yii\web\View */ | ||||
/* @var $model common\models\User */ | /* @var $model common\models\User */ | ||||
$this->title = 'Update User: ' . ' ' . $model->id; | |||||
$this->params['breadcrumbs'][] = ['label' => 'Users', 'url' => ['index']]; | |||||
$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]]; | |||||
$this->params['breadcrumbs'][] = 'Update'; | |||||
$this->title = 'Modifier un utilisateur' ; | |||||
$this->params['breadcrumbs'][] = ['label' => 'Utilisateurs', 'url' => ['index']]; | |||||
$this->params['breadcrumbs'][] = ['label' => Html::encode($model->nom.' '.$model->prenom)]; | |||||
$this->params['breadcrumbs'][] = 'Modifier'; | |||||
?> | ?> | ||||
<div class="user-update"> | <div class="user-update"> | ||||
<?php | |||||
namespace common\helpers; | |||||
class Password { | |||||
public static function generate($length = 6) | |||||
{ | |||||
$chaine = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789' ; | |||||
$nb = strlen($chaine) - 1; | |||||
$code = ''; | |||||
for($i=0; $i < $length; $i++) | |||||
{ | |||||
$pos = mt_rand(0, $nb); | |||||
$car = $chaine[$pos]; | |||||
$code .= $car; | |||||
} | |||||
return $code ; | |||||
} | |||||
} |
<?php | |||||
use yii\helpers\Html; | |||||
?> | |||||
<p>Bonjour <?= Html::encode($user->prenom); ?>,</p> | |||||
<p>Votre boulangerie <strong><?= Html::encode($etablissement->nom); ?></strong> vient | |||||
de créer un compte pour vous sur le site <a href="http://www.laboiteapain.net/">La boîte à pain</a>.</p> | |||||
<p><strong>Ce compte vous permet de passer vos commandes dans cette boulangerie.</strong></p> | |||||
<p>Voci vos identifiants de connexion :<br /> | |||||
Identifiant : <strong><?= Html::encode($user->email); ?></strong><br /> | |||||
Mot de passe : <strong><?= $password ?></strong></p> | |||||
<p>Cordialement,<br /> | |||||
<em>La boîte à pain</em></p> |
Bonjour <?= $user->prenom ?>, | |||||
Votre boulangerie "<?= $etablissement->nom ?>" vient | |||||
de créer un compte pour vous sur le site http://www.laboiteapain.net/. | |||||
Ce compte vous permet de passer vos commandes dans cette boulangerie. | |||||
Voci vos identifiants de connexion : | |||||
Identifiant : <?= $user->email; ?> | |||||
Mot de passe : <?= $password ?> | |||||
Cordialement, | |||||
La boîte à pain |
use common\models\Etablissement; | use common\models\Etablissement; | ||||
use yii\base\Model; | use yii\base\Model; | ||||
use common\models\UserEtablissement ; | use common\models\UserEtablissement ; | ||||
use common\helpers\Password ; | |||||
/** | /** | ||||
* Signup form | * Signup form | ||||
// génération d'un code | // génération d'un code | ||||
do { | do { | ||||
$chaine = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789' ; | |||||
$nb = strlen($chaine) - 1; | |||||
$code = ''; | |||||
for($i=0; $i < 6; $i++) | |||||
{ | |||||
$pos = mt_rand(0, $nb); | |||||
$car = $chaine[$pos]; | |||||
$code .= $car; | |||||
} | |||||
$code = Password::generate() ; | |||||
$etablissement->code = $code ; | $etablissement->code = $code ; | ||||
} while(Etablissement::findOne(['code' => $code])); | } while(Etablissement::findOne(['code' => $code])); | ||||