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