CakePHP - 自定义密码

时间:2016-07-27 07:58:17

标签: php authentication cakephp login passwords

最近我尝试通过直接添加新文件来更改CakePHP上的密码编码。不幸的是,我似乎没有联系,即她在sha256

中的类Simple非常适合

AppController的

class AppController extends Controller {
public $components = array('Auth' => array(
    'authenticate'  => array(
        'Form'  => array(
            'fields'    => array('username'   => 'email','password' => 'password'),
            'passwordHasher'    => array(
                'className' => 'Custom'),
            'scope' => array('active'   => '1')
        )
    )
));

UsersController(登录)

    App::uses('CustomPasswordHasher', 'Controller/Component/Auth');

class UsersController extends AppController{

    public function login()
    {

        $erreur = false;
        if($this->request->is('post')) {
            if(!empty($this->data)) {
                if(!$this->Auth->user('id')) {
                    if($this->Auth->login()) {
                        echo 'ok';
                    } else {
                        $erreur = "Identifiant incorrect.";
                    }
                } else {
                    $erreur = "Vous êtes déjà connecter.";
                }
            } else {
                $erreur = "Veuillez saisir vos identifiants";
            }
            $this->set(compact('erreur'));
        }
    }
}

CustomPasswordHasher

    App::uses('AbstractPasswordHasher', 'Controller/Component/Auth');
class CustomPasswordHasher extends AbstractPasswordHasher {
    public function hash($password)
    {
        $before = substr(Configure::read('Security.salt'),0,43);
        $after = substr(Configure::read('Security.salt'),43,42);

        $chaine = $before.$password.$after;
        return hash("sha256",$chaine);

    }
    public function check($password, $email, $hashType = null) {
        $this->User = ClassRegistry::init('User');
        $u = $this->User->find('first', array(
            'fields'        => array('user.password'),
            'conditions'    => array('user.email'   => $email)
        ));
        if($u) {
           return $this->hash($password) == $u['User']['password'];
        }
        return false;
    }
}

我的问题可能出现在哪里?

谢谢!

0 个答案:

没有答案