Voilà, suis content ca marche bien. Le problème que j'avais encore, c'est que lorsque l'utilisateur a resete son mot de passe, je ne veux pas qu'il puisse s'authentifier comme usuellement. Il doit d'abord changer son mot de passe.
Pour cela, j'ai rajouter le chanps passwordrst ici:
class AppController extends Controller {
public $helpers = array('Html','Text','Form','Session','Date','Cache','Subcategories','Image.Image','Mathcaptcha');
public $components = array(
'Acl',
'Auth' => array(
// For the ACL
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'authError' => 'Pensiez-vous réellement que vous étiez autorisés à voir cela ?',
// Mets des condition au niveau de la validation d'authentificate
// Contient l'ensemble des methode d'authentification
'authenticate' => array(
// Mehtode Form est utilisée
'Form' => array(
// Defini les paramêtre scope. Ici défini que le compte doit est actif
'scope'=> array(
'User.active' =>1,
'User.passwordrst' =>0
)
)
)
),
'Session',
'Ctrl'
);
Si l'tilisateur reset son mot de passe, le champs passwordrst passe a 1. Quans il est changer ce champs passe à 0.
Pour la solution à mon problème j'ai fais comme ceci en suivant l'idée de latak11 (encore merci). Que pensez vous de mon code?
Es-ce que je peux faire mieux ou plus propre? Dans tous les cas ca marche, mais etant encore un novice dans CakePHP, votre d'avis d'expert me serait très intéressant :o)
Ainsi, si je veux banir un utilisateur, je n'ai juste a agir indépendemment sur le field 'active'
/**
* When user request a new password
**/
function forgottenpassword(){
if($this->request->is('post')){
$v = current($this->request->data);
// Check if the count associate to the e-mail is active
$user = $this->User->find('first',array(
'conditions'=> array(
'email'=> $v'email'],
'active' => 1
))
);
// If not
if(empty($user)){
$this->Session->setFlash(__("Cet e-mail n'existe pas"),'notif',array('type'=>'danger'));
}else{
// Generate new password
$newPass = substr(md5(uniqid(rand(), true)), 8, 8);
// Generate a token
$token = substr(md5(uniqid(rand(), true)), 16, 16);
//Set the DB row at user ID
$this->User->id = $user'User']'id'];
// Save new password, the haching is done in the controller at beforeSave()
$this->User->saveField('password',$newPass);
//Change passwordrst field status
$this->User->saveField('passwordrst',1);
// Save token
$this->User->saveField('token',$token);
// Send e-mail
App::uses('CakeEmail','Network/Email');
$mail = new CakeEmail();
$mail->from($this->Session->read('Site.email'))
->to($user'User']'email'])
->subject($this->Session->read('Site.name').' - '.__('Nouveau mot de passe'))
->emailFormat('both')
->template('forgottenpassword')
->viewVars(array('username'=>$user'User']'username'],'pw'=>$newPass,'token'=>$token))
// The URL is define ine the view forgottenpassword
->send();
$this->Session->setFlash(__('Votre mot de passe temporaire vous a été envoyé'),'notif');
}
}
}
/**
* When user click on the received mail to reset it password
**/
function resetpassword($username,$token){
// Check if the username and the password match to a row
$d'user'] = $this->User->find('first',array(
'conditions'=>array(
'username'=>$username,
'token'=>$token
)
));
// We are going to use the user ID from $user
$user = current($d);
// If yes
if(!empty($user)){
// GET Post
if($this->request->is('post')){
// Save in a var
$data = $this->request->data'User'];
// User fill up a form with temp password and new password. He has to enter his username, email and password
// Now, it check if username, email and password match to the user id field
$ok = $this->User->find('first',array(
'conditions'=>array(
'User.id'=>$user'User']'id'], // Compare ID
'username'=>$data'username'],
'email'=>$data'email'],
'password'=>AuthComponent::password($data'password']) // Compare password
)
));
$ok = current($ok);
// IF result matches
if(!empty($ok)){
// Select the row DB
$this->User->id = $ok'id'];
// Replace the Temp passsword with the new
$this->User->saveField('password',$data'newpassword']);
// Change the passwordrst field status
$this->User->saveField('passwordrst',0);
// Notify User
$this->Session->setFlash(__("Le mot de passe a été changé"),'notif');
}else{
$this->Session->setFlash(__("Les informations fournies ne correspondent à aucun compte"),'notif',array('type'=>'danger'));
}
}
}else{
$this->Session->setFlash(__("Il n'est plus possible d'initier le mot de passe"),'notif',array('type'=>'danger'));
}
}