Created
September 11, 2011 16:06
-
-
Save sergiopvilar/1209752 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* App Controller | |
* | |
* @link http://book.cakephp.org/pt/view/957/A-classe-AppController | |
*/ | |
class AppController extends Controller | |
{ | |
/** | |
* Define os componentes disponíveis por padrão | |
* | |
* @var array | |
* @access public | |
*/ | |
var $components = array( | |
'Auth', | |
'Session' | |
); | |
/** | |
* Define os helpers disponíveis por padrão | |
* | |
* @var array | |
* @access public | |
*/ | |
var $helpers = array( | |
'Html', | |
'Form', | |
'Session', | |
'Ajax', | |
'Paginator' | |
); | |
/** | |
* Before Filter | |
* | |
* Função de callback executada antes que qualquer outra | |
* | |
* @access public | |
* @link http://book.cakephp.org/pt/view/984/Callbacks | |
*/ | |
function beforeFilter() | |
{ | |
Security::setHash('md5'); // Método de Hash da senha | |
$this->Auth->userModel = "Usuario"; // Nome do modelo para os usuários | |
$this->Auth->fields = array( | |
'username'=>'username', // Troque o segundo parametro se desejar | |
'password'=>'password', // Troque o segundo parametro se desejar | |
); | |
$this->Auth->userScope = array( | |
'Usuario.active' => '1' // Permite apenas usuários ativos | |
); | |
$this->Auth->authorize = 'controller'; // Utiliza a função isAuthorize para autorizar os usuários | |
$this->Auth->autoRedirect = true; // Redireciona o usuário para a requisição anterior que foi negada após o login | |
$this->Auth->loginAction = array( | |
'controller' => 'Usuarios', | |
'action' => 'login', | |
'admin' => false | |
); | |
$this->Auth->loginRedirect = array( | |
'controller' => 'pages', | |
'action' => 'index', | |
'admin' => true | |
); | |
$this->Auth->logoutRedirect = array( | |
'controller' => 'pages', | |
'action' => 'home', | |
'admin' => false | |
); | |
$this->Auth->loginError = __('Usuário ou senha inválidos.', true); | |
$this->Auth->authError = __('Você não tem permissão para acessar.', true); | |
// Libera acesso para actions sem prefixo admin | |
if ( !(isset($this->params['admin'])) ) | |
{ | |
$this->Auth->allow('*'); // Libera acesso | |
} | |
//$this->Auth->allow('*'); | |
} // beforefilter | |
/** | |
* Is Authorized | |
* | |
* Faz a autorização do usuário | |
* | |
* @return boolean | |
* @access public | |
*/ | |
function isAuthorized() { | |
// Pode ser mais complexo antes de liberar o acesso | |
return true; | |
} | |
} |
This file contains 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 { | |
var $name = 'Usuarios'; | |
function admin_index() { | |
$this->Usuario->recursive = 0; | |
$this->set('usuarios', $this->paginate()); | |
} | |
function admin_view($id = null) { | |
if (!$id) { | |
$this->Session->setFlash('Invalid usuario', true); | |
$this->redirect(array('action' => 'index')); | |
} | |
$this->set('usuario', $this->Usuario->read(null, $id)); | |
} | |
function admin_add() { | |
if (!empty($this->data)) { | |
$this->Usuario->create(); | |
if ($this->Usuario->save($this->data)) { | |
$this->Session->setFlash('O usuário foi cadastrado com sucesso.', 'flash_success'); | |
$this->redirect(array('action' => 'index')); | |
} else { | |
$this->Session->setFlash('O usuário não pôde ser cadastrado. Por favor, tente novamente.', 'flash_warning'); | |
} | |
} | |
} | |
function admin_edit($id = null) { | |
if (!$id && empty($this->data)) { | |
$this->Session->setFlash('Usuário inválido', 'flash_warning'); | |
$this->redirect(array('action' => 'index')); | |
} | |
if (!empty($this->data)) { | |
if ($this->Usuario->save($this->data)) { | |
$this->Session->setFlash('O usuário foi atualizado com sucesso.', 'flash_success'); | |
$this->redirect(array('action' => 'index')); | |
} else { | |
$this->Session->setFlash('O usuário não pôde ser salvo. Por favor, tente novamente.', 'flash_error'); | |
} | |
} | |
if (empty($this->data)) { | |
$this->data = $this->Usuario->read(null, $id); | |
} | |
} | |
function admin_delete($id = null) { | |
if (!$id) { | |
$this->Session->setFlash('ID de usuário inválida.', 'flash_warning'); | |
$this->redirect(array('action'=>'index')); | |
} | |
if ($this->Usuario->delete($id)) { | |
$this->Session->setFlash('O usuário foi excluído com sucesso.', 'flash_info'); | |
$this->redirect(array('action'=>'index')); | |
} | |
$this->Session->setFlash('O usuário não pôde ser apagado.', 'flash_error'); | |
$this->redirect(array('action' => 'index')); | |
} | |
/** | |
* Login | |
* | |
* @access public | |
*/ | |
function login() | |
{ | |
//var_dump($this->data); | |
// Aqui é tudo automagic. O Auth Component se encarrega de fazer tudo sozinho. | |
} | |
/** | |
* Logout | |
* | |
* @access public | |
*/ | |
function logout() | |
{ | |
$this->Session->setFlash(__('Sua sessão foi encerrada.', true)); // Mensagem de logout | |
$this->redirect($this->Auth->logout()); // Redireciona para tela de logout | |
} | |
function beforeFilter() { | |
parent::beforeFilter(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment