Created
December 23, 2011 15:34
-
-
Save patrickmaciel/1514477 to your computer and use it in GitHub Desktop.
Erro ao efetuar login (CakePHP 2.0 - Assando Sites | Exemplo)
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 | |
class AppController extends Controller { | |
public $helpers = array('Html', 'Form', 'Session', 'Paginator'); | |
public $components = array('Session', 'Auth'); | |
public function isAuthorized() { | |
return true; | |
} | |
public function beforeFilter() { | |
if($this->isPrefix('admin')) { | |
$this->layout = 'admin'; | |
$this->Auth->autoRedirect = false; | |
$this->Auth->userScope = array('Usuario.ativo = 1'); | |
$this->Auth->loginAction = array('controller'=>'usuarios', 'action'=>'login', 'admin'=>false); | |
$this->Auth->logoutAction = array('controller'=>'usuarios', 'action'=>'logout', 'admin'=>false); | |
$this->Auth->loginRedirect = array('controller'=>'usuarios', 'action'=>'index', 'admin'=>true); | |
// 'logoutRedirect'=>array('controller'=>'works', 'action'=>'index', 'admin'=>false), | |
$this->Auth->authError = 'Você não tem permissão para acessar esta página'; | |
$this->Auth->authorize = array('Controller'); | |
$this->Auth->authenticate = array('Form', array( | |
'userModel' => 'Usuario', | |
'fields' => array( | |
'username' => 'usuario', | |
'password' => 'senha' | |
) | |
) | |
); | |
$this->set('logged_in', $this->Auth->loggedIn()); | |
$this->set('current_user', $this->Auth->user()); | |
} else { | |
$this->Auth->allow('*'); | |
} | |
} | |
public function beforeRender() { | |
if ((isset($this->params['action'])) && ($this->params['action'] == 'login')) { | |
$this->layout = 'login'; | |
} else { | |
if ((isset($this->params['admin'])) && ($this->params['admin'])) { | |
$this->layout = 'admin'; | |
} | |
} | |
if($this->Session->read('Auth.User')){ | |
} | |
} | |
public function isPrefix($prefix) { | |
return isset($this->request->params['prefix']) && | |
$this->request->params['prefix'] == $prefix; | |
} | |
} |
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 | |
class UsuariosController extends AppController { | |
public $uses = array('Usuario'); | |
public $helpers = array('Time', 'Text', 'Util'); | |
public function beforeFilter() { | |
parent::beforeFilter(); | |
} | |
public function isAuthorized($usuario) { | |
return true; | |
} | |
public function login() { | |
if($this->request->is('post')) { | |
if($this->Auth->login()) { | |
$this->redirect($this->Auth->redirect()); | |
} else { | |
$this->Session->setFlash('Usuário/Senha incorretos', 'default', array(), 'auth'); | |
} | |
} | |
} | |
public function logout() { | |
$this->redirect($this->Auth->logout()); | |
} | |
public function admin_index() { | |
$this->Usuario->recursive = 1; | |
$this->set('usuarios', $this->paginate()); | |
} | |
public function admin_add() { | |
if($this->request->is('post')) { | |
$this->Usuario->create(); | |
if($this->Usuario->save($this->request->data)) { | |
$this->Session->setFlash('Usuario salvo com sucesso.', 'default', array('class' => 'message success')); | |
$this->redirect(array('action' => 'index')); | |
} else { | |
$this->Session->setFlash('Não foi possível salvar o Usuario, tente novamente mais tarde.'); | |
} | |
} | |
$grupos = $this->Usuario->Grupo->find('list', array( | |
'order' => array( | |
'nome' => 'ASC' | |
) | |
)); | |
$this->set(compact('grupos')); | |
} | |
public function admin_edit($id = null) { | |
// debug($this->request->data); | |
$this->Usuario->id = $id; | |
if(!$this->Usuario->exists()) { | |
throw new NotFoundException('Usuario inválida'); | |
} | |
if(($this->request->is('post')) || ($this->request->is('put'))) { | |
$this->Usuario->create(); | |
// debug($this->request->data); | |
if($this->request->data['Usuario']['senha'] == '') { | |
unset($this->request->data['Usuario']['senha']); | |
unset($this->request->data['Usuario']['confirmacao_senha']); | |
} | |
// exit; | |
if($this->Usuario->save($this->request->data)) { | |
$this->Session->setFlash('Usuario atualizado com sucesso.', 'default', array('class' => 'message success')); | |
$this->redirect(array('controller' => 'usuarios', 'action' => 'index')); | |
} else { | |
$this->Session->setFlash('Não foi possível salvar o Usuario, tente novamente mais tarde.'); | |
} | |
} else { | |
$this->request->data = $this->Usuario->read(null, $id); | |
} | |
$grupos = $this->Usuario->Grupo->find('list', array( | |
// 'fields' => 'Grupo.nome', | |
'order' => array( | |
'nome' => 'ASC' | |
) | |
)); | |
$this->set(compact('grupos')); | |
} | |
public function admin_view($id = null) { | |
$this->Usuario->recursive = 1; | |
$this->Usuario->id = $id; | |
if(!$this->Usuario->exists()) { | |
throw new NotFoundException('Usuario inválida.'); | |
} | |
$usuario = $this->Usuario->read(null, $id); | |
$this->set(compact('usuario')); | |
} | |
public function admin_delete($id = null) { | |
if(!$this->request->is('post')) { | |
throw new MethodNotAllowedException('Usuario inválida.'); | |
} | |
$this->Usuario->id = $id; | |
if($this->Usuario->delete($id)) { | |
$this->Session->setFlash('Usuario deletado com sucesso.', 'default', array('class' => 'message success')); | |
$this->redirect(array('action' => 'index')); | |
} else { | |
$this->Session->setFlash('Não foi possível excluir o Usuario, tente novamente mais tarde.'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment