AuthorizedRule   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 108
ccs 21
cts 21
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setResourceFactory() 0 5 1
A __invoke() 0 14 2
A isWhitelisted() 0 7 2
A newResource() 0 4 1
1
<?php
2
/**
3
 * Cheka - Authorization
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (C) 2016 Jake Johns
8
 *
9
 * This software may be modified and distributed under the terms
10
 * of the MIT license.  See the LICENSE file for details.
11
 *
12
 * @category  Router
13
 * @package   Jnjxp\Cheka
14
 * @author    Jake Johns <[email protected]>
15
 * @copyright 2016 Jake Johns
16
 * @license   http://jnj.mit-license.org/2016 MIT License
17
 * @link      https://github.com/jnjxp/jnjxp.cheka
18
 */
19
20
namespace Jnjxp\Cheka;
21
22
use Aura\Router\Rule\RuleInterface;
23
use Aura\Router\Route;
24
25
use Zend\Permissions\Acl\AclInterface;
26
use Zend\Permissions\Acl\Resource\ResourceInterface;
27
28
use Psr\Http\Message\ServerRequestInterface as Request;
29
30
/**
31
 * Require Authorization based on ACL
32
 *
33
 * @category Router
34
 * @package  Jnjxp\Cheka
35
 * @author   Jake Johns <[email protected]>
36
 * @license  http://jnj.mit-license.org/2016 MIT License
37
 * @link     https://github.com/jnjxp/jnjxp.cheka
38
 *
39
 * @see RuleInterface
40
 */
41
class AuthorizedRule implements RuleInterface
42
{
43
    use ResourceRouteAwareTrait;
44
    use RoleRequestAwareTrait;
45
46
    /**
47
     * Acl
48
     *
49
     * @var AclInterface
50
     *
51
     * @access protected
52
     */
53
    protected $acl;
54
55
    /**
56
     * Resource factory
57
     *
58
     * @var callable
59
     *
60
     * @access protected
61
     */
62
    protected $resourceFactory;
63
64
    /**
65
     * Create an ACL router rule
66
     *
67
     * @param AclInterface $acl Configured acl to check against
68
     *
69
     * @access public
70
     */
71 10
    public function __construct(AclInterface $acl)
72
    {
73 10
        $this->acl = $acl;
74 10
        $this->resourceFactory = [$this, 'newResource'];
75 10
    }
76
77
    /**
78
     * Set resource factory
79
     *
80
     * @param callable $factory to create resource from Route and Request
81
     *
82
     * @return $this
83
     *
84
     * @access public
85
     */
86 1
    public function setResourceFactory(callable $factory)
87
    {
88 1
        $this->resourceFactory = $factory;
89 1
        return $this;
90
    }
91
92
    /**
93
     * Check that role has access to routes resource and privilege
94
     *
95
     * @param Request $request PSR7 Server Request
96
     * @param Route   $route   Route
97
     *
98
     * @return bool
99
     *
100
     * @access public
101
     */
102 6
    public function __invoke(Request $request, Route $route)
103
    {
104 6
        $factory = $this->resourceFactory;
105 6
        $resource = $factory($route, $request);
106
107 6
        if ($this->isWhitelisted($resource)) {
108 1
            return true;
109
        }
110
111 5
        $role = $this->getRole($request);
112 4
        $privilege = $this->getPrivilegeFromRoute($route);
113
114 4
        return $this->acl->isAllowed($role, $resource, $privilege);
115
    }
116
117
    /**
118
     * Is Whitelisted?
119
     *
120
     * @param ResourceInterface $resource Route resource
121
     *
122
     * @return bool
123
     *
124
     * @access protected
125
     */
126 6
    protected function isWhitelisted(ResourceInterface $resource)
127
    {
128 6
        if (! $resource->getResourceId()) {
129 1
            return true;
130
        }
131 5
        return false;
132
    }
133
134
    /**
135
     * New resource from Route and Request
136
     *
137
     * @param Route   $route   Route
138
     * @param Request $request Request
139
     *
140
     * @return RouteRequestResource
141
     *
142
     * @access protected
143
     */
144 5
    protected function newResource(Route $route, Request $request)
145
    {
146 5
        return new Acl\Resource($route, $request);
147
    }
148
}
149