Bonjour,
J'ai fais le tutoriel concernant un site en 4 jours avec cakephp et actuellement j'ai commencer a intégré un système de memebre mais le soucis c'est que j'arrive pas a me connecter. Le mot de passe est hashé lors de l'envoi dans la base de donnée et lors de la connexion les mot de passes sont différent
Ci-dessous mon UsersController.php
<?php
class UsersController extends AppController{
function admin_index(){
$d'users'] = $this->Paginate('User');
$this->set($d);
}
function admin_edit($id = null){
if($this->request->is('post') || $this->request->is('put')){
$d = $this->request->data'User'];
if($d'password'] != $d'passwordconfirm']){
$this->Session->setflash("Les mots de passes ne correcpondent pas ","notif",array('type'=>'error'));
}else{
if(empty($d'password']))
unset($d'password']);
$this->User->save($d);
$this->Session->setflash("L'utilisateur a bien été enregistré ",'notif');
}
}elseif($id){
$this->User->id = $id;
$this->request->data = $this->User->read('id,username,role');
}
$d = array();
$d'roles'] = array(
'admin' => 'admin',
'user' => 'membre'
);
$this->set($d);
}
function admin_delete($id){
$this->User->delete($id);
$this->Session->setflash("l'utilisateur a bien été supprimé ","notif");
$this->redirect($this->referer());
}
function signup(){
if($this->request->is('post')){
$d = $this->request->data;
$d'User']'id'] = null;
if(!empty($d'User']'password'])){
$d'User']'password'] = Security::hash($d'User']'password'],null,true);
}
if($this->User->save($d,true,array('username','password','role','mail'))){
$link =array('controller'=>'users','action'=>'activate',$this->User->id,'-'.md5($d'User']'password']));
App::uses('CakeEmail','Network/Email');
$mail = new CakeEmail();
$mail->from('noreply@localhost.com')
->to($d'User']'mail'])
->subject('Test::Inscription')
->emailFormat('html')
->template('signup')
->viewVars(array('username'=>$d'User']'username'],'link'=>$link))
->send();
$this->request->data = array();
$this->Session->setFlash("Votre compte a bien été créé",'notif');
}else{
$this->Session->setFlash("Merci de corriger les erreurs",'notif',array('type' =>'error'));
}
}
}
function login(){
if($this->request->is('post')){
if($this->Auth->login()){
$this->User->id = $this->Auth->user('id');
$this->User->saveField('lastlogin',date('Y-m-d H:i:s'));
$this->Session->setFlash("Vous êtres maintenant connecté",'notif');
$this->redirect('/');
}else{
$this->Session->setFlash("Identifiant incorrecte",'notif',array('type'=>'error'));
debug(Security::hash($this->request->data'User']'password']));
}
}
}
function logout(){
$this->Auth->logout();
$this->redirect($this->referer());
/*$this->redirect($this->Auth->logout());*/
}
function password(){
if(!empty($this->request->params'named']'token'])){
$token = $this->request->params'named']'token'];
$user = $this->User->find('first',array(
'conditions' =>array('id' =>$token[0],'MD5(User.password)'=>$token[1], 'active' => 1)
));
if($user){
$this->User->id =$user'User']'id'];
$password = substr(MD5(uniqid(rand(),true)),0,8);
$this->User->saveField('password',Securty::hash($password,null,true));
$this->Session->setFlash("Votre mot de passe a bien été reinitialisé, voici votre nouveau mot de passe : $password",'notif');
}else{
$this->Session->setFlash("Le lien n'est pas valide",'notif', array('type'=>'error'));
}
}
if($this->request->is('post')){
$v = current($this->request->data);
$user = $this->User->find('first',array(
'conditions' => array('mail'=>$v'mail'],'active'=>1)
));
if(empty($users)){
$this->Session->setFlash('Aucun utilisateur ne correspond à cet mail','notif', array('type'=>'error'));
}else{
App::uses('CakeEmail','Network/Email');
$link =array('controller'=>'users','action'=>'password','token',$user'user']'id'],'-'.md5($user'User']'password']));
$mail = new CakeEmail();
$mail ->from('noreply@localhost.com')
->to($user'User']'mail'])
->subject('Test::Mot de passe oublié ?')
->emailFormat('html')
->template('mdp')
->viewVars(array('username'=>$user'User']'username'],'link'=>$link))
->send();
$this->request->data = array();
}
}
}
function activate($token){
$token = explode('-',$token);
$user = $this->User->find('first',array(
'conditions' =>array('id' =>$token[0],'MD5(User.password)'=>$token[1], 'active' => 0)
));
If(!empty($user)){
$this->User->id = $user'User']'id'];
$this->User->saveField('active',1);
$this->Session->setFlash("Votre compte a bien été activé",'notif');
$this->Auth->login($user'User']);
}else{
$this->Session->setFlash("Ce lien d'activation n'est pas valide",'notif',array('type'=>'error'));
}
$this->redirect('/');
die();
}
public function edit() {
$user_id = $this->Auth->user('id');
if(!$user_id){
$this->redirect('/');
die();
}
$this->User->id = $user_id;
$passError = false;
if($this->request->is('put') || $this->request->is('post')){
$d = $this->request->data;
$d'User']'id'] = $user_id;
if(!empty($d'User']'pass'])){
if($d'User']'pass1'] == $d'User']'pass2']){
$d'User']'password'] = Security::hash($d'User']'pass1'],null,true);
}else{
$passError = true;
}
}
if($this->User->save($d,true,array('firstname','lastname','password'))){
$this->Session->setFlash("Votre profit a bien été édité",'notif');
}else{
$this->Session->setFlash("Impossible de sauvegarder",'notif',array('type'=>'error'));
}
if($passError)
$this->User->validationErrors'pass2'] = array('les mots de passe ne correspondent pas');
}else{
$this->request->data = $this->User->read();
}
$this->request->data'User']'pass1'] = $this->request->data'User']'pass2'] = '';
}
}
?>
Et voici mon AppController.php
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $helpers = array('Text','Form','Html', 'Session', 'Date');
public $components = array('Session','Cookie',
'Auth' => array(
'authenticate' =>array(
'Form' => array(
'scope'=>array('User.active'=>1)
)
)
)
);
function beforeFilter(){
$this->Auth->loginAction =array('controller'=>'users','action'=>'login','admin'=>false);
/** Système d'authentification par controller*/
$this->Auth->authorize= array('controller');
if(!isset($this->request->params'prefix'])){
$this->Auth->allow();
}
if(isset($this->request->params'prefix']) && $this->request->params'prefix'] == 'admin'){
$this->layout = 'admin';
}
}
function isAuthorized($user){
if(!isset($this->request->params'prefix'])){
return true;
}
$roles = array(
'admin'=> 10,
'user' => 5
);
if(isset($roles$this->request->params'prefix']])){
$lvlAction = $roles$this->request->params'prefix']];
$lvlUser = $roles$user'role']];
if($lvlUser >= $lvlAction){
return true;
}else{
return false;
}
}
return false;
}
}
Lors du débug du mot de passe : test
saisi a partir du formulaire :
a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
mot de passe en base de donnée :
ee710ca6d014a126789babb743e06f4c4b235e80
J'ai revu mes code et j'arrive pas a savoir d'ou cela provient mon erreur, toutes mes pages sont bien en UTF-8
La je galère depuis quelques jours ce ce problème
merci de votre aide
Cafreunion