Pop\Auth\Acl::allow PHP Метод

allow() публичный Метод

Method to allow a user role permission to a resource or resources
public allow ( mixed $roles, mixed $resources = null, mixed $permissions = null ) : Acl
$roles mixed
$resources mixed
$permissions mixed
Результат Acl
    public function allow($roles, $resources = null, $permissions = null)
    {
        if (!is_array($roles)) {
            $roles = array($roles);
        }
        // Check if the roles has been added
        foreach ($roles as $role) {
            if (!isset($this->roles[$role])) {
                throw new Exception('Error: That role has not been added.');
            }
            if (!isset($this->allowed[$role])) {
                $this->allowed[$role] = array();
            }
            // Check if the resource(s) have been added
            if (null !== $resources) {
                if (!is_array($resources)) {
                    $resources = array($resources);
                }
                foreach ($resources as $resource) {
                    if (!isset($this->resources[$resource])) {
                        $this->addResource($resource);
                    }
                    $this->allowed[$role][$resource] = array();
                    if (null != $permissions) {
                        if (!is_array($permissions)) {
                            $permissions = array($permissions);
                        }
                        foreach ($permissions as $permission) {
                            if (!$this->roles[$role]->hasPermission($permission)) {
                                throw new Exception("Error: That role does not have the permission '" . $permission . "'.");
                            }
                            $this->allowed[$role][$resource][] = $permission;
                        }
                    }
                }
            }
        }
        return $this;
    }

Usage Example

Пример #1
0
 public function testBuildRoleException()
 {
     $_SERVER['REQUEST_URI'] = '/first';
     $this->setExpectedException('Pop\\Nav\\Exception');
     $page = new Auth\Resource('page');
     $user = new Auth\Resource('user');
     $basic = Auth\Role::factory('basic')->addPermission('add');
     $editor = Auth\Role::factory('editor')->addPermission('add')->addPermission('edit');
     $acl = new Auth\Acl();
     $acl->addRoles(array($basic, $editor));
     $acl->addResources(array($page, $user));
     $acl->allow('basic', 'page', array('add'))->allow('editor', 'page')->allow('editor', 'user');
     $tree = array(array('name' => 'Pages', 'href' => '/pages', 'children' => array(array('name' => 'Add Page', 'href' => 'add', 'acl' => array('resource' => 'page', 'permission' => 'add')), array('name' => 'Edit Page', 'href' => 'edit', 'acl' => array('resource' => 'page', 'permission' => 'edit')))), array('name' => 'Users', 'href' => '/users', 'acl' => array('resource' => 'user'), 'children' => array(array('name' => 'Add User', 'href' => 'add'), array('name' => 'Edit User', 'href' => 'edit'))));
     $n = new Nav($tree);
     $n->setAcl($acl);
     $r = $n->render(true);
 }
All Usage Examples Of Pop\Auth\Acl::allow