1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the MIT license. |
18
|
|
|
*/ |
19
|
|
|
namespace LearnZF2Acl\Model; |
20
|
|
|
|
21
|
|
|
use Zend\Permissions\Acl\Acl as BaseAcl; |
22
|
|
|
use Zend\Permissions\Acl\Resource\GenericResource as Resource; |
23
|
|
|
use Zend\Permissions\Acl\Role\GenericRole as Role; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Acl model. |
27
|
|
|
* |
28
|
|
|
* @author Abdul Malik Ikhsan <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class Acl extends BaseAcl |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Construct |
34
|
|
|
* Define Acl keys. |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->addRole(new Role('Guest')); |
39
|
|
|
$this->addRole(new Role('User'), 'Guest'); |
40
|
|
|
$this->addRole(new Role('Admin'), 'User'); |
41
|
|
|
|
42
|
|
|
$this->addResource(new Resource('HomeController')); |
43
|
|
|
$this->addResource(new Resource('UserController')); |
44
|
|
|
$this->addResource(new Resource('AdminController')); |
45
|
|
|
|
46
|
|
|
$this->allow('Guest', 'HomeController', 'ViewHome'); |
47
|
|
|
$this->allow('Guest', 'UserController', ['ViewUser', 'RegisterUser']); |
48
|
|
|
|
49
|
|
|
$this->allow('User', 'UserController', 'EditUser'); |
50
|
|
|
$this->deny('User', 'UserController', 'RegisterUser'); |
51
|
|
|
|
52
|
|
|
$this->allow('Admin', 'AdminController', ['DeleteUser', 'AddUser']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get Right lists. |
57
|
|
|
* |
58
|
|
|
* @param int $roleId |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function getRightLists($roleId = null) |
63
|
|
|
{ |
64
|
|
|
$rules = []; |
65
|
|
|
$currentRole = 'All'; |
66
|
|
|
foreach ($this->getResources() as $resource) { |
67
|
|
|
foreach ($this->getRoles() as $roleKey => $role) { |
68
|
|
|
$rules[] = $this->getRules(new Resource($resource), new Role($role)); |
69
|
|
|
if ($roleId === $roleKey) { |
70
|
|
|
$currentRole = $role; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$rights = []; |
76
|
|
|
foreach ($rules as $rule) { |
77
|
|
|
if (is_array($rule)) { |
78
|
|
|
foreach ($rule['byPrivilegeId'] as $right => $typeAndAssert) { |
79
|
|
|
if (!in_array($right, $rights)) { |
80
|
|
|
$rights[] = $right; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($roleId === null) { |
87
|
|
|
return $rights; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$rightList = []; |
91
|
|
|
foreach ($this->getResources() as $resource) { |
92
|
|
|
foreach ($rights as $key => $right) { |
93
|
|
|
if ($currentRole !== 'All' |
94
|
|
|
&& $this->isAllowed($currentRole, $resource, $right) && !in_array($right, $rightList) |
95
|
|
|
) { |
96
|
|
|
$rightList[] = $right; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $rightList; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get Resource by Role. |
106
|
|
|
* |
107
|
|
|
* @param int $roleId |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getResourceByRole($roleId) |
112
|
|
|
{ |
113
|
|
|
$selectedRole = 'Guest'; |
114
|
|
|
foreach ($this->getRoles() as $key => $role) { |
115
|
|
|
if ($roleId === $key) { |
116
|
|
|
$selectedRole = $role; |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$resources = []; |
122
|
|
|
foreach ($this->getResources() as $resource) { |
123
|
|
|
foreach ($this->getRightLists() as $right) { |
124
|
|
|
if ($this->isAllowed($selectedRole, $resource, $right) |
125
|
|
|
&& !in_array($resource, $resources) |
126
|
|
|
) { |
127
|
|
|
$resources[] = $resource; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $resources; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|