Passed
Push — master ( 1f9581...d48de4 )
by Chauncey
02:35 queued 29s
created

Authorizer::getDefaultResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\User;
4
5
use InvalidArgumentException;
6
7
// From 'laminas/laminas-permissions-acl'
8
use Laminas\Permissions\Acl\Exception\ExceptionInterface as AclExceptionInterface;
9
use Laminas\Permissions\Acl\Resource\ResourceInterface as AclResourceInterface;
10
11
// From 'charcoal-user'
12
use Charcoal\User\UserInterface;
13
14
/**
15
 * User Authorization Checker
16
 *
17
 * The authorizer service provides support, upon creation, for a default ACL resource.
18
 *
19
 * ## Checking permissions
20
 *
21
 * To check if a given ACL (passed in constructor) allows a list of permissions (aka privileges):
22
 *
23
 * - `userAllowed(UserInterface $user, string[] $aclPermissions)`
24
 * - `rolesAllowed(string[] $roles, string[] $aclPermissions)`
25
 */
26
class Authorizer extends AbstractAuthorizer
27
{
28
    const DEFAULT_RESOURCE = 'DEFAULT_RESOURCE';
29
30
    /**
31
     * The default ACL resource identifier.
32
     *
33
     * @var string|null
34
     */
35
    private $defaultResource;
36
37
    /**
38
     * @param array $data Class dependencies.
39
     */
40
    public function __construct(array $data)
41
    {
42
        parent::__construct($data);
43
44
        if (isset($data['resource'])) {
45
            $this->setDefaultResource($data['resource']);
46
        }
47
    }
48
49
    /**
50
     * Determine if access is granted by checking the role(s) for permission(s).
51
     *
52
     * @deprecated In favour of AbstractAuthorizer::anyRolesGrantedAll()
53
     *
54
     * @param  string[] $aclRoles       The ACL role(s) to check.
55
     * @param  string[] $aclPermissions The ACL privilege(s) to check.
56
     * @return boolean Returns TRUE if and only if the $aclPermissions are granted against one of the $aclRoles.
57
     *     Returns TRUE if an empty array of permissions is given.
58
     *     Returns NULL if no applicable roles or permissions could be checked.
59
     */
60
    public function rolesAllowed(array $aclRoles, array $aclPermissions)
61
    {
62
        if (empty($aclPermissions)) {
63
            return true;
64
        }
65
66
        return $this->anyRolesGrantedAll($aclRoles, static::DEFAULT_RESOURCE, $aclPermissions);
67
    }
68
69
    /**
70
     * Determine if access is granted by checking the user's role(s) for permission(s).
71
     *
72
     * @deprecated In favour of AbstractAuthorizer::isUserGranted()
73
     *
74
     * @param  UserInterface $user           The user to check.
75
     * @param  string[]      $aclPermissions The ACL privilege(s) to check.
76
     * @return boolean
77
     *     Returns TRUE if and only if the $aclPermissions are granted against one of the roles of the $user.
78
     *     Returns TRUE if an empty array of permissions is given.
79
     *     Returns NULL if no applicable roles or permissions could be checked.
80
     */
81
    public function userAllowed(UserInterface $user, array $aclPermissions)
82
    {
83
        if (empty($aclPermissions)) {
84
            return true;
85
        }
86
87
        return $this->isUserGranted($user, static::DEFAULT_RESOURCE, $aclPermissions);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     *
93
     * This method overrides {@see AbstractAuthorizer::isAllowed()}
94
     * as proxy to {@see \Laminas\Permissions\Acl\Acl::isAllowed()}
95
     * to provide support for the special class constant `Authorizer::DEFAULT_RESOURCE`.
96
     *
97
     * @param  AclRoleInterface|string     $role      The ACL role to check.
98
     * @param  AclResourceInterface|string $resource  The ACL resource to check.
99
     * @param  string                      $privilege The ACL privilege to check.
100
     * @return boolean Returns TRUE if and only if the $role has access to the $resource.
101
     */
102
    public function isAllowed($role = null, $resource = null, $privilege = null)
103
    {
104
        if ($resource === static::DEFAULT_RESOURCE) {
105
            $resource = $this->getDefaultResource();
106
        }
107
108
        return parent::isAllowed($role, $resource, $privilege);
109
    }
110
111
    /**
112
     * @return string|null
113
     */
114
    protected function getDefaultResource()
115
    {
116
        return $this->defaultResource;
117
    }
118
119
    /**
120
     * @param  string|null $resource The ACL resource identifier.
121
     * @throws InvalidArgumentException If the resource identifier is not a string.
122
     * @return void
123
     */
124
    private function setDefaultResource($resource)
125
    {
126
        if (!is_string($resource) && $resource !== null) {
127
            throw new InvalidArgumentException(
128
                'ACL resource identifier must be a string or NULL'
129
            );
130
        }
131
132
        $this->defaultResource = $resource;
133
    }
134
}
135