Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Created February 15, 2012 04:32
Show Gist options
  • Save patrickmaciel/1833211 to your computer and use it in GitHub Desktop.
Save patrickmaciel/1833211 to your computer and use it in GitHub Desktop.
Erro ACL | login
<?php
App::uses('Sanitize', 'Utility');
class AppController extends Controller {
public $helpers = array('Util', 'Html', 'Form', 'Session', 'Text', 'Paginator');
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel'=>'Usuario',
'fields' => array(
'username' => 'login',
'password' => 'senha'
),
'scope' => array(
'Usuario.ativo' => 1
)
)
),
'loginAction' => array('controller' => 'usuarios', 'action' => 'login', 'admin' => true),
'logoutAction' => array('controller' => 'usuarios', 'action' => 'logout', 'admin' => true),
'authError' => 'Você não tem permissão para acessar essa área.',
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
),
'Acl',
'Session'
);
public function beforeFilter() {
debug($this->request->data);
// $this->Auth->allow('*');
}
public function beforeRender() {
if( (isset($this->params['action'])) && ($this->params['action'] == 'admin_login') ) {
$this->layout = 'login';
} else if($this->isPrefix('admin')) {
$this->layout = 'admin';
} else if($this->isPrefix('painel')) {
$this->layout = 'painel';
}
}
public function isPrefix($prefixo) {
return isset($this->request->params['prefix']) && $this->request->params['prefix'] == $prefixo;
}
}
?>
<?php
App::uses('AppModel', 'Model');
/**
* Grupo Model
*
* @property Usuario $Usuario
*/
class Grupo extends AppModel {
const ADMINISTRADORES = 3;
const MODERADORES = 4;
public $actsAs = array('Acl' => array('type' => 'requester'));
/**
* Display field
*
* @var string
*/
public $displayField = 'nome';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'nome' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'descricao' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'created' => array(
'datetime' => array(
'rule' => array('datetime'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'modified' => array(
'datetime' => array(
'rule' => array('datetime'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'ativo' => array(
'boolean' => array(
'rule' => array('boolean'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Usuario' => array(
'className' => 'Usuario',
'foreignKey' => 'grupo_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
/**
* Necessário para o funcionamento do Acl
*/
public function parentNode() {
return null;
}
}
<?php
App::uses('AppController', 'Controller');
/**
* Grupos Controller
*
* @property Grupo $Grupo
*/
class GruposController extends AppController {
/**
* index method
*
* @return void
*/
public function index() {
$this->Grupo->recursive = 0;
$this->set('grupos', $this->paginate());
}
/**
* view method
*
* @param string $id
* @return void
*/
public function view($id = null) {
$this->Grupo->id = $id;
if (!$this->Grupo->exists()) {
throw new NotFoundException(__('Invalid grupo'));
}
$this->set('grupo', $this->Grupo->read(null, $id));
}
/**
* add method
*
* @return void
*/
public function admin_adicionar() {
if ($this->request->is('post')) {
$this->Grupo->create();
if ($this->Grupo->save($this->request->data)) {
$this->Session->setFlash(__('The grupo has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The grupo could not be saved. Please, try again.'));
}
}
}
/**
* edit method
*
* @param string $id
* @return void
*/
public function edit($id = null) {
$this->Grupo->id = $id;
if (!$this->Grupo->exists()) {
throw new NotFoundException(__('Invalid grupo'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Grupo->save($this->request->data)) {
$this->Session->setFlash(__('The grupo has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The grupo could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->Grupo->read(null, $id);
}
}
/**
* delete method
*
* @param string $id
* @return void
*/
public function delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Grupo->id = $id;
if (!$this->Grupo->exists()) {
throw new NotFoundException(__('Invalid grupo'));
}
if ($this->Grupo->delete()) {
$this->Session->setFlash(__('Grupo deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Grupo was not deleted'));
$this->redirect(array('action' => 'index'));
}
/**
* admin_index method
*
* @return void
*/
public function admin_index() {
$this->Grupo->recursive = 0;
$this->set('grupos', $this->paginate());
}
/**
* admin_view method
*
* @param string $id
* @return void
*/
public function admin_view($id = null) {
$this->Grupo->id = $id;
if (!$this->Grupo->exists()) {
throw new NotFoundException(__('Invalid grupo'));
}
$this->set('grupo', $this->Grupo->read(null, $id));
}
/**
* admin_add method
*
* @return void
*/
public function admin_add() {
if ($this->request->is('post')) {
$this->Grupo->create();
if ($this->Grupo->save($this->request->data)) {
$this->Session->setFlash(__('The grupo has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The grupo could not be saved. Please, try again.'));
}
}
}
/**
* admin_edit method
*
* @param string $id
* @return void
*/
public function admin_edit($id = null) {
$this->Grupo->id = $id;
if (!$this->Grupo->exists()) {
throw new NotFoundException(__('Invalid grupo'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Grupo->save($this->request->data)) {
$this->Session->setFlash(__('The grupo has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The grupo could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->Grupo->read(null, $id);
}
}
/**
* admin_delete method
*
* @param string $id
* @return void
*/
public function admin_delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Grupo->id = $id;
if (!$this->Grupo->exists()) {
throw new NotFoundException(__('Invalid grupo'));
}
if ($this->Grupo->delete()) {
$this->Session->setFlash(__('Grupo deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Grupo was not deleted'));
$this->redirect(array('action' => 'index'));
}
}
<?php
App::uses('AppModel', 'Model');
/**
* Usuario Model
*
* @property Pessoa $Pessoa
* @property Log $Log
*/
class Usuario extends AppModel {
public $actsAs = array('Acl' => array('type' => 'requester'));
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'login' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'senha' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'ativo' => array(
'boolean' => array(
'rule' => array('boolean'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Pessoa' => array(
'className' => 'Pessoa',
'foreignKey' => 'pessoa_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Grupo' => array(
'className' => 'Grupo',
'foreignKey' => 'grupo_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Log' => array(
'className' => 'Log',
'foreignKey' => 'usuario_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
/**
* beforeSave convert password to sha1 + salt
*/
public function beforeSave($options = array()) {
if(!empty($this->data['Usuario']['senha'])) {
$senha = $this->data['Usuario']['senha'];
$senha = AuthComponent::password($senha);
$this->data['Usuario']['senha'] = $senha;
}
return parent::beforeSave($options);
}
/**
* Necessário para o Acl funcionar corretamente
*/
public function parentNode() {
if(!$this->id && empty($this->data)) {
return null;
}
if(isset($this->data['Usuario']['grupo_id'])) {
$grupoID = $this->data['Usuario']['grupo_id'];
} else {
$grupoID = $this->field('grupo_id');
}
if(!$grupoID) {
return null;
} else {
return array('Grupo' => array('id' => $grupoID));
}
}
/**
* Necessário para o Acl funcionar corretamente
*/
public function bindNode($usuario) {
return array(
'model' => 'Grupo',
'foreign_key' => $usuario['Usuario']['grupo_id']
);
}
}
<?php
App::uses('AppController', 'Controller');
/**
* Usuarios Controller
*
* @property Usuario $Usuario
*/
class UsuariosController extends AppController {
/**
* beforeFilter
* Utilizado até então para setar as permissões públicas do sistema
*/
public function beforeFilter() {
$this->Auth->allow('admin_login','admin_logout');
parent::beforeFilter();
}
public function admin_login() {
if($this->request->is('post')) {
if($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Usuário e/ou senha inválido.', 'default', array(), 'auth');
}
}
}
public function admin_logout() {
$this->redirect($this->Auth->logout());
}
/**
* index method
*
* @return void
*/
public function admin_index() {
$this->Usuario->recursive = 0;
$this->set('usuarios', $this->paginate());
}
/**
* view method
*
* @param string $id
* @return void
*/
public function admin_visualizar($id = null) {
$this->Usuario->id = $id;
if (!$this->Usuario->exists()) {
throw new NotFoundException(__('Invalid usuario'));
}
$this->set('usuario', $this->Usuario->read(null, $id));
}
/**
* add method
*
* @return void
*/
public function admin_adicionar() {
if ($this->request->is('post')) {
$this->Usuario->create();
if ($this->Usuario->save($this->request->data)) {
$this->Session->setFlash(__('The usuario has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The usuario could not be saved. Please, try again.'));
}
}
$pessoas = $this->Usuario->Pessoa->find('list');
$grupos = $this->Usuario->Grupo->find('list');
$this->set(compact('pessoas','grupos'));
}
/**
* edit method
*
* @param string $id
* @return void
*/
public function admin_editar($id = null) {
$this->Usuario->id = $id;
if (!$this->Usuario->exists()) {
throw new NotFoundException(__('Invalid usuario'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Usuario->save($this->request->data)) {
$this->Session->setFlash(__('The usuario has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The usuario could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->Usuario->read(null, $id);
}
$pessoas = $this->Usuario->Pessoa->find('list');
$grupos = $this->Usuario->Grupo->find('list');
$this->set(compact('pessoas','grupos'));
}
/**
* delete method
*
* @param string $id
* @return void
*/
public function admin_deletar($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Usuario->id = $id;
if (!$this->Usuario->exists()) {
throw new NotFoundException(__('Invalid usuario'));
}
if ($this->Usuario->delete()) {
$this->Session->setFlash(__('Usuario deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Usuario was not deleted'));
$this->redirect(array('action' => 'index'));
}
/**
* Setar permissões aos grupos do sistema
*/
public function admin_accessManager() {
$Grupo = $this->Usuario->Grupo;
$Grupo->id = $Grupo::ADMINISTRADORES;
$this->Acl->allow($Grupo, 'controllers');
$Grupo->id = $Grupo::MODERADORES;
$this->Acl->deny($Grupo, 'controllers');
$this->Acl->allow($Grupo, 'controllers/Incorporadoras/adicionar');
$this->Acl->allow($Grupo, 'controllers/Incorporadoras/visualizar');
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment