Skip to content

Instantly share code, notes, and snippets.

@andrebian
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrebian/fbcd0b674680e65621b3 to your computer and use it in GitHub Desktop.
Save andrebian/fbcd0b674680e65621b3 to your computer and use it in GitHub Desktop.
<?php
//core.php
Configure::write('Acl.classname', 'PhpAcl');
// acl.php
$config['map'] = array(
'User' => 'User/username',
'Role' => 'User/role_id',
);
$config['alias'] = array(
'Role/1' => 'Role/equipe-x',
'Role/2' => 'Role/validador-x',
'Role/3' => 'Role/validador-y',
'Role/4' => 'Role/validador-z',
'Role/5' => 'Role/gestor',
'Role/6' => 'Role/geral',
'Role/7' => 'Role/nao-cadastrado',
);
/**
* role configuration
*/
$config['roles'] = array(
'Role/equipe-x' => null,
'Role/validador-x' => null,
'Role/validador-y' => null,
'Role/validador-z' => null,
'Role/gestor' => null,
'Role/geral' => null,
'Role/nao-cadastrado' => null,
);
/**
* rule configuration
*/
$config['rules'] = array(
'allow' => array(
//liberando tudo à todos
'*' => 'Role/equipe-x, Role/validador-x, Role/validador-y, Role/validador-z, Role/gestor, Role/geral, Role/nao-cadastrado',
),
'deny' => array(
// bloqueando somente as poucas actions que nem todos tem acesso
'controllers/users/(add|edit|delete)' => 'Role/validador-y',
),
);
// AppController
public $components = array('Acl', 'Auth' => array('authorize' => 'Controller'), 'Session');
public function beforeFilter()
{
//...
if( !$this->isAuthorized() ) {
$this->Session->setFlash($this->Auth->authError);
$this->redirect($this->Auth->redirectUrl());
}
//...
}
protected function isAuthorized()
{
$aco = 'controllers/'.$this->params['controller'];
$aro = $this->Auth->user('role_id');
return $this->Acl->check($aro, $aco, $this->params['action']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment