1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* RBAC implementation for HiPanel. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-rbac |
6
|
|
|
* @package hipanel-rbac |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\rbac; |
12
|
|
|
|
13
|
|
|
use yii\base\InvalidParamException; |
14
|
|
|
use yii\rbac\Item; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Smart setters for AuthManager. |
18
|
|
|
* |
19
|
|
|
* @author Andrii Vasyliev <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
trait SetterTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Set permission. |
25
|
|
|
* @param string $name |
26
|
|
|
* @param string $description |
27
|
|
|
* @return Item |
28
|
|
|
*/ |
29
|
7 |
|
public function setPermission($name, $description = null) |
30
|
|
|
{ |
31
|
7 |
|
$permission = $this->getPermission($name) ?: $this->createPermission($name); |
|
|
|
|
32
|
7 |
|
if ($description) { |
|
|
|
|
33
|
|
|
$permission->description = $description; |
34
|
|
|
} |
35
|
7 |
|
$this->add($permission); |
|
|
|
|
36
|
|
|
|
37
|
7 |
|
return $permission; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set role. |
42
|
|
|
* @param string $name |
43
|
|
|
* @param string $description |
44
|
|
|
* @return Item |
45
|
|
|
*/ |
46
|
7 |
|
public function setRole($name, $description = null) |
47
|
|
|
{ |
48
|
7 |
|
$role = $this->getRole($name) ?: $this->createRole($name); |
|
|
|
|
49
|
7 |
|
if ($description) { |
|
|
|
|
50
|
|
|
$role->description = $description; |
51
|
|
|
} |
52
|
7 |
|
$this->add($role); |
|
|
|
|
53
|
|
|
|
54
|
7 |
|
return $role; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set child. |
59
|
|
|
* @param string|Item $parent |
60
|
|
|
* @param string|Item $child |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
7 |
|
public function setChild($parent, $child) |
64
|
|
|
{ |
65
|
7 |
View Code Duplication |
if (is_string($parent)) { |
|
|
|
|
66
|
7 |
|
$name = $parent; |
67
|
7 |
|
$parent = $this->getItem($parent); |
|
|
|
|
68
|
7 |
|
if (is_null($parent)) { |
69
|
|
|
throw new InvalidParamException("Unknown parent:$name at setChild"); |
70
|
|
|
} |
71
|
7 |
|
} |
72
|
7 |
View Code Duplication |
if (is_string($child)) { |
|
|
|
|
73
|
|
|
$name = $child; |
74
|
|
|
$child = $this->getItem($child); |
|
|
|
|
75
|
|
|
if (is_null($child)) { |
76
|
|
|
throw new InvalidParamException("Unknown child:$name at setChild"); |
77
|
|
|
} |
78
|
|
|
} |
79
|
7 |
|
if (isset($this->children[$parent->name][$child->name])) { |
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
7 |
|
return $this->addChild($parent, $child); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Assigns an item (role or permission) to a user. |
88
|
|
|
* @param string|Item $item |
89
|
|
|
* @param string|integer $userId the user ID (see [[\yii\web\User::id]]) |
90
|
|
|
* @throws \Exception when given wrong item name |
91
|
|
|
* @return Assignment the assignment object |
92
|
|
|
*/ |
93
|
14 |
|
public function setAssignment($item, $userId) |
94
|
|
|
{ |
95
|
14 |
|
if (is_string($item)) { |
96
|
14 |
|
$item = $this->findItem($item); |
97
|
14 |
|
} |
98
|
14 |
|
if (isset($this->assignments[$userId][$item->name])) { |
99
|
|
|
return $this->assignments[$userId][$item->name]; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
14 |
|
return $this->assign($item, $userId); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
14 |
|
protected function findItem($name, $description = null) |
106
|
|
|
{ |
107
|
14 |
|
$item = $this->getItem($name); |
|
|
|
|
108
|
14 |
|
if ($item) { |
109
|
14 |
|
return $item; |
110
|
|
|
} |
111
|
1 |
|
if (strncmp($name, 'deny:', 5) === 0) { |
112
|
1 |
|
return $this->setPermission($name, $description); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
throw new InvalidParamException("Unknown item:$name at findItem"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Assigns items to a user. |
120
|
|
|
* @param string|array $items |
121
|
|
|
* @param string|integer $userId |
122
|
|
|
*/ |
123
|
4 |
|
public function setAssignments($items, $userId) |
124
|
|
|
{ |
125
|
4 |
|
if (is_string($items)) { |
126
|
4 |
|
$items = explode(',', $items); |
127
|
4 |
|
} |
128
|
4 |
|
foreach ($items as $item) { |
129
|
4 |
|
$this->setAssignment($item, $userId); |
130
|
4 |
|
} |
131
|
4 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns all assignments in the system. |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function getAllAssignments() |
138
|
|
|
{ |
139
|
|
|
return $this->assignments; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns all items in the system. |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
14 |
|
public function getAllItems() |
147
|
|
|
{ |
148
|
14 |
|
return $this->items; |
|
|
|
|
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.