Last active
December 30, 2015 21:49
-
-
Save andrebian/7890249 to your computer and use it in GitHub Desktop.
Autenticação com model != User no CakePHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// AppController | |
// ... | |
public function beforeFilter() { | |
// Mecanismo de autenticação | |
$this->Auth->authenticate = array('Blowfish' => array( | |
// Configura o model e os campos | |
'userModel' => 'Usuario', | |
'fields' => array( | |
'username' => 'email', | |
'password' => 'senha', | |
), | |
)); | |
// ... | |
// UsuariosController | |
public function login() | |
{ | |
$this->layout = 'login'; | |
if ($this->request->is('post')) { | |
$email = $this->request->data['Usuario']['email']; | |
$usuario = $this->Usuario->find('first', array('conditions' => array('email' => $email))); | |
if (!empty($usuario)) | |
if (strpos($usuario['Usuario']['senha'], '$2a$10') === false) { | |
$this->redirect(array('controller' => 'usuarios', 'action' => 'minha_conta', base64_encode($usuario['Usuario']['id']))); | |
} | |
if ($this->Auth->login()) { | |
$user = array( | |
'NOME' => $usuario['Usuario']['nome'], | |
'EMAIL' => $usuario['Usuario']['email'], | |
'ID' => $usuario['Usuario']['id'], | |
'TIPO' => $usuario['Usuario']['tipo'], | |
'TOKEN' => md5($usuario['Usuario']['id'] . $usuario['Usuario']['email'] . date('YmdHi'))); | |
$this->Session->write('USER', $user); | |
$this->Session->setFlash('Login realizado com sucesso, sua sessão permanecerá ativa por ' . Configure::read('Session.timeout') . ' minutos a partir de agora.', 'default', array('class' => 'notification msginfo')); | |
$this->__postLogin($this->Auth->user()); | |
$this->redirect($this->Auth->redirectUrl()); | |
} else { | |
$this->log('Tentativa de login com falha. Usuário: ' . $this->request->data['username'], 'login-error'); | |
$this->Session->setFlash('Login ou senha incorretos', 'default', array('class' => 'notification msgerror', 'before' => '<p>', 'after' => '</p>')); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment