Browse Source

Pouvoir modifier son mot de passe dans le profil

prodstable
keun 7 years ago
parent
commit
712a8f5203
3 changed files with 84 additions and 10 deletions
  1. +54
    -2
      common/models/User.php
  2. +21
    -3
      frontend/controllers/UserController.php
  3. +9
    -5
      frontend/views/user/update.php

+ 54
- 2
common/models/User.php View File

const STATUS_BOULANGER = 11; const STATUS_BOULANGER = 11;
const STATUS_ADMIN = 13; const STATUS_ADMIN = 13;


var $password_old ;
var $password_new ;
var $password_new_confirm ;
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function rules() public function rules()
{ {
return [ return [
['confiance','default','value'=>0],
['confiance','default','value'=>1],
[['no_mail','mail_prod_lundi','mail_prod_mardi','mail_prod_mercredi','mail_prod_jeudi','mail_prod_vendredi','mail_prod_samedi','mail_prod_dimanche'],'boolean'], [['no_mail','mail_prod_lundi','mail_prod_mardi','mail_prod_mercredi','mail_prod_jeudi','mail_prod_vendredi','mail_prod_samedi','mail_prod_dimanche'],'boolean'],
[['nom','prenom','telephone','adresse'], 'string'], [['nom','prenom','telephone','adresse'], 'string'],
[['nom','prenom'],'required','message'=> 'Ce champs ne peut être vide'], [['nom','prenom'],'required','message'=> 'Ce champs ne peut être vide'],
['email','verifyEmail'], ['email','verifyEmail'],
['status', 'default', 'value' => self::STATUS_ACTIVE], ['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED, self::STATUS_ADMIN,self::STATUS_BOULANGER ]], ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED, self::STATUS_ADMIN,self::STATUS_BOULANGER ]],
[['date_derniere_connexion'],'safe'],
['password_old','verifyPasswordOld'],
['password_new','verifyPasswordNew'],
['password_new_confirm','verifyPasswordNewConfirm'],
[['date_derniere_connexion','password_old','password_new','password_new_confirm','password_hash'],'safe'],
]; ];
} }
public function verifyPasswordOld($attribute,$params)
{
if(strlen($this->password_old))
{
if(!$this->validatePassword($this->password_old))
{
$this->addError($attribute, 'Mot de passe invalide.') ;
}
}
if(!strlen($this->password_old) && (strlen($this->password_new) || strlen($this->password_new_confirm)))
{
$this->addError($attribute, 'Ce champs ne peut être vide') ;
}
if(!strlen($this->password_new) && (strlen($this->password_old) || strlen($this->password_new_confirm)))
{
$this->addError('password_new', 'Ce champs ne peut être vide') ;
}
if(!strlen($this->password_new_confirm) && (strlen($this->password_old) || strlen($this->password_new)))
{
$this->addError('password_new_confirm', 'Ce champs ne peut être vide') ;
}
}


public function verifyPasswordNew($attribute,$params)
{
if(strlen($this->password_new) < 6)
{
$this->addError($attribute, 'Votre mot de passe doit comporter au moins 6 caractères.') ;
}
}
public function verifyPasswordNewConfirm($attribute,$params)
{
if($this->password_new != $this->password_new_confirm)
{
$this->addError($attribute, 'Les deux mots de passe doivent être identiques') ;
}
}
public function verifyEmail($attribute,$params) { public function verifyEmail($attribute,$params) {
$user = User::find()->where("email LIKE :email AND id != :id")->params(array(':email'=>'%'.$this->email.'%', ':id'=>$this->id))->one() ; $user = User::find()->where("email LIKE :email AND id != :id")->params(array(':email'=>'%'.$this->email.'%', ':id'=>$this->id))->one() ;
'mail_prod_vendredi' => 'Vendredi', 'mail_prod_vendredi' => 'Vendredi',
'mail_prod_samedi' => 'Samedi', 'mail_prod_samedi' => 'Samedi',
'mail_prod_dimanche' => 'Dimanche', 'mail_prod_dimanche' => 'Dimanche',
'password_old' => 'Ancien mot de passe',
'password_new' => 'Nouveau mot de passe',
'password_new_confirm' => 'Confirmation du nouveau mot de passe',
]; ];
} }

+ 21
- 3
frontend/controllers/UserController.php View File

{ {
$model = $this->findModel(Yii::$app->user->identity->id); $model = $this->findModel(Yii::$app->user->identity->id);


if ($model->load(Yii::$app->request->post())) {
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// l'utilisateur ne peut pas changer d'adresse email // l'utilisateur ne peut pas changer d'adresse email
$old_model = $this->findModel(Yii::$app->user->identity->id) ; $old_model = $this->findModel(Yii::$app->user->identity->id) ;
$model->email = $old_model->email ; $model->email = $old_model->email ;
// modification du mot de passe
if(strlen($model->password_new))
{
//$model->setPassword($model->password_new) ;
$model->password_hash = Yii::$app->security->generatePasswordHash($model->password_new);
$model->password_old = '' ;
$model->password_new = '' ;
$model->password_new_confirm = '' ;
}
$model->save() ; $model->save() ;
Yii::$app->session->setFlash('success','Votre profil a bien été modifié.') ;
return $this->render('update', [ return $this->render('update', [
'model' => $model, 'model' => $model,
'edit_ok' => true
]); ]);
} else { } else {
if(!$model->validate())
{
Yii::$app->session->setFlash('error','Le formulaire comporte des erreurs.') ;
}
return $this->render('update', [ return $this->render('update', [
'model' => $model, 'model' => $model,
]); ]);
} }
} }


public function actionCredit() public function actionCredit()

+ 9
- 5
frontend/views/user/update.php View File

<div id="profil-user" class="user-update"> <div id="profil-user" class="user-update">


<h1 class="title-systeme-commande"><span class="glyphicon glyphicon-user"></span> Mon profil</h1> <h1 class="title-systeme-commande"><span class="glyphicon glyphicon-user"></span> Mon profil</h1>
<?php if(isset($edit_ok)): ?>
<div class="alert alert-success">
Votre profil a bien été modifié.
</div>
<?php endif; ?>
<div class="user-form"> <div class="user-form">


<?php $form = ActiveForm::begin([ <?php $form = ActiveForm::begin([
'enableClientScript' => false 'enableClientScript' => false
]); ?> ]); ?>
<h2>Informations</h2>
<?= $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(['readonly' => true]); ?> <?= $form->field($model, 'email')->textInput(['readonly' => true]); ?>
<?= $form->field($model, 'adresse')->textarea() ?> <?= $form->field($model, 'adresse')->textarea() ?>
<h2>Mot de passe</h2>
<p>Renseignez les champs ci-dessous si vous souhaitez modifier votre mot de passe.</p>
<?= $form->field($model, 'password_old')->passwordInput() ?>
<?= $form->field($model, 'password_new')->passwordInput() ?>
<?= $form->field($model, 'password_new_confirm')->passwordInput() ?>
<div class="clr"></div> <div class="clr"></div>
<div class="form-group"> <div class="form-group">

Loading…
Cancel
Save