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

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

Method to deny a user role permission to a resource or resources
public deny ( mixed $roles, mixed $resources = null, mixed $permissions = null ) : Acl
$roles mixed
$resources mixed
$permissions mixed
Результат Acl
    public function deny($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->denied[$role])) {
                $this->denied[$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->denied[$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->denied[$role][$resource][] = $permission;
                        }
                    }
                }
            }
        }
        return $this;
    }

Usage Example

Пример #1
0
<?php

require_once '../../bootstrap.php';
use Pop\Auth\Acl;
use Pop\Auth\Role;
use Pop\Auth\Resource;
try {
    // Create some resources
    $page = new Resource('page');
    $template = new Resource('template');
    // Create some roles with permissions
    $reader = Role::factory('reader')->addPermission('read');
    $editor = Role::factory('editor')->addPermission('edit');
    $publisher = Role::factory('publisher')->addPermission('publish');
    $admin = Role::factory('admin')->addPermission('admin');
    // Add roles as child roles to demonstrate inheritance
    $reader->addChild($editor->addChild($publisher->addChild($admin)));
    $acl = new Acl();
    $acl->addRoles(array($reader, $editor, $publisher, $admin));
    $acl->addResources(array($page, $template));
    $acl->allow('reader', 'page', 'read')->allow('editor', 'page', array('read', 'edit'))->allow('publisher', 'page')->allow('publisher', 'template', 'read')->allow('admin');
    $acl->deny('editor', 'page', 'read');
    $user = $editor;
    if ($acl->isAllowed($user, 'page', 'edit')) {
        echo 'Yes.<br /><br />';
    } else {
        echo 'No.<br /><br />';
    }
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL . PHP_EOL;
}