Пример #1
0
$acl->setDefaultAction(Phalcon\Acl::DENY);
// 默认不允许访问
$acl->addRole(new Phalcon\Acl\Role('User'));
$acl->addRole(new Phalcon\Acl\Role('Admin'));
// 这里是继承,第一个参数是儿子,第二个参数是父亲
//$acl->addInherit('User','Guest');
/*
 * 资源,定义访问的接口
 * */
$arrResources = ['User' => ['UserController' => ['login', 'logout']], 'Admin' => []];
foreach ($arrResources as $arrResource) {
    foreach ($arrResource as $controller => $arrMethods) {
        $acl->addResource(new Phalcon\Acl\Resource($controller), $arrMethods);
    }
}
foreach ($acl->getRoles() as $objRole) {
    $roleName = $objRole->getName();
    if ($roleName == 'Admin') {
        foreach ($arrResources['Admin'] as $resource => $method) {
            $acl->allow($roleName, $resource, $method);
        }
    }
    if ($roleName == 'User') {
        foreach ($arrResources['User'] as $resource => $method) {
            $acl->allow($roleName, $resource, $method);
        }
    }
}
$app->before(function () use($app, $acl) {
    $arrHandler = $app->getActiveHandler();
    $controller = str_replace('Controller\\', '', get_class($arrHandler[0]));
Пример #2
0
 /**
  * Events Manager Interaction
  */
 public function testEventsManagerInteraction()
 {
     $events = new Phalcon\Events\Manager();
     $self = $this;
     $events->attach('acl:beforeCheckAccess', function ($event, $component, $object) use($self) {
         $self->assertEquals($component->getActiveRole(), 'Role');
         $self->assertEquals($component->getActiveResource(), 'Resource');
         $self->assertEquals($component->getActiveAccess(), 'index');
         return false;
     });
     $acl = new Phalcon\Acl\Adapter\Memory();
     $acl->setDefaultAction(Phalcon\Acl::DENY);
     $this->assertEquals($acl->getDefaultAction(), Phalcon\Acl::DENY);
     $acl->setEventsManager($events);
     $this->assertEquals($acl->getEventsManager(), $events);
     $resource = new Phalcon\Acl\Resource('Resource');
     $role = new Phalcon\Acl\Role('Role');
     $acl->addResource($resource, array('index'));
     $acl->addRole($role);
     $acl->allow('Role', 'Resource', 'index');
     $this->assertEquals($acl->getResources(), array($resource));
     $this->assertEquals($acl->getRoles(), array($role));
     $this->assertEquals($acl->isAllowed('Role', 'Resource', 'index'), false);
 }