Je suis donc passé par la fonction "edit" avec le hashage en beforesave mon mot de passe est bien hashé/crypté dans ma bdd. Maintenant je veux me connecter, je crois que sa marche car je suis renvoyé à admin/pages comme je lui ai demandé mais.... j'ai l'erreur :
$controller does not implement an isAuthorized() method.
Error: An Internal Error Has Occurred.
Je vous remet mes pages...
<?php
class AppController extends Controller{
public $helpers = array('Text','Form','Html','Session');
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array('controller' => 'users', 'action' => 'login', 'admin' => false),
'loginRedirect' => array('controller' => 'pages', 'action' => 'index', 'admin' => false),
'authenticate' => array('Form'),
'authorize' => array('Controller'),
)
);
function beforeFilter(){
$this->Auth->loginAction = array('controller'=>'users','action'=>'login','admin'=>false);
$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;
}
<?php
class UsersController extends AppController{
function login(){
if($this->request->is('post')){
if($this->Auth->login()){
return $this->redirect('/admin/pages');
}else{
$this->Session->setFlash("Votre login ou votre mot de passe ne correspond pas","notif",array('type'=>'error'));
}
}
}
function logout(){
}
function 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 correspondent pas","notif",array('type'=>'error'));
}else{
if(empty($d'password']))
unset($d'password']);
if($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('username,role,id');
}
$d = array();
$d'roles'] = array(
'admin' => 'admin',
'user' => 'membre'
);
$this->set($d);
}
}
<?php
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
public function beforeSave($options = array()) {
if (!$this->id) {
$passwordHasher = new SimplePasswordHasher();
$this->data'User']'password'] = $passwordHasher->hash(
$this->data'User']'password']
);
}
return true;
}
}
Page login
<h1>Se connecter</h1>
<?php echo $this->Form->create('User'); ?>
<?php echo $this->Form->input('username',array('label'=>"Login")); ?>
<?php echo $this->Form->input('password',array('label'=>"Mot de passe")); ?>
<?php echo $this->Form->end('Se connecter'); ?>
Page edit
<h1>Editer un utilisateur</h1>
</div>
<?php echo $this->Form->create('User'); ?>
<?php echo $this->Form->input('username',array('label'=>'Login')); ?>
<?php echo $this->Form->input('password',array('label'=>'Mot de passe')); ?>
<?php echo $this->Form->input('passwordconfirm',array('label'=>'Confirmer mot de passe','type'=>'password')); ?>
<?php echo $this->Form->input('role',array('label'=>"Role")); ?>
<?php echo $this->Form->input('id'); ?>
<?php echo $this->Form->end('Envoyer'); ?>