<?php
use Phalcon\Acl;
use Phalcon\Acl\Role;
use Phalcon\Acl\Resource;
use Phalcon\Events\Event;
use Phalcon\Mvc\User\Plugin;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Acl\Adapter\Memory as AclList;
/**
* SecurityPlugin
*
* This is the security plugin which controls that users only have access to the modules they're assigned to
*/
class SecurityPlugin extends Plugin
{
/**
* Returns an existing or new access control list
*
* @returns AclList
*/
public function getAcl()
{
if (!isset($this->persistent->acl)) {
$acl = new AclList();
$acl->setDefaultAction(Acl::DENY);
// Register roles
$roles = [
'salarie' => new Role(
'salarie',
'accès limité.'
),
'guests' => new Role(
'Guests',
'accès limité.'
),
'admin' => new Role(
'admin',
'accès à l\'ensemble de l\'application.'
),
'comptable' => new Role(
'comptable',
'accès au fonctions comptables de l\'application.'
)
];
foreach ($roles as $role) {
$acl->addRole($role);
}
//Admin area resources
$privateResources = array(
'index' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
'dashboard' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
'clients' => array('index', 'search', 'nouveau', 'edit', 'save', 'creation', 'delete'),
'articles' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
'parametres' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
'session' => array('index', 'register', 'start', 'end'),
);
foreach ($privateResources as $resource => $actions) {
$acl->addResource(new Resource($resource), $actions);
}
//Public area resources
$publicResources = array(
'index' => array('index'),
'about' => array('index'),
'errors' => array('show401', 'show404', 'show500'),
'session' => array('index', 'register', 'start', 'end'),
'contact' => array('index', 'send')
);
foreach ($publicResources as $resource => $actions) {
$acl->addResource(new Resource($resource), $actions);
}
//Grant access to public areas
foreach ($roles as $role) {
foreach ($publicResources as $resource => $actions) {
foreach ($actions as $action){
$acl->allow($role->getName(), $resource, $action);
}
}
}
//Grant access to private area to role Users
foreach ($privateResources as $resource => $actions) {
foreach ($actions as $action){
$acl->allow('admin', $resource, $action);
}
}
//The acl is stored in session, APC would be useful here too
$this->persistent->acl = $acl;
}
return $this->persistent->acl;
}
/**
* This action is executed before execute any action in the application
*
* @param Event $event
* @param Dispatcher $dispatcher
* @return bool
*/
public function beforeDispatch(Event $event, Dispatcher $dispatcher)
{
$auth = $this->session->get('auth');
if (!$auth){
$role = 'Guests';
} else {
$role = $auth['role'];
}
$controller = $dispatcher->getControllerName();
$action = $dispatcher->getActionName();
$acl = $this->getAcl();
if (!$acl->isResource($controller)) {
$dispatcher->forward([
'controller' => 'errors',
'action' => 'show404'
]);
return false;
}
$allowed = $acl->isAllowed($role, $controller, $action);
if ($allowed != Acl::ALLOW) {
$dispatcher->forward(array(
'controller' => 'errors',
'action' => 'show401'
));
$this->session->destroy();
return false;
}
}
}
I've now commented the '$this->session->destroy();' line and now it's works and ACL seems to are still working (i.e. protected pages are still not accessible as guest...)