|
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
|
|
|
* AuthManager class. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class AuthManager extends \yii\rbac\PhpManager |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Set permission. |
|
26
|
|
|
* @param string $name |
|
27
|
|
|
* @param string $description |
|
28
|
|
|
* @return Item |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function setPermission($name, $description = null) |
|
31
|
|
|
{ |
|
32
|
1 |
|
return $this->setItem('permission', $name, $description); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Set role. |
|
37
|
|
|
* @param string $name |
|
38
|
|
|
* @param string $description |
|
39
|
|
|
* @return Item |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function setRole($name, $description = null) |
|
42
|
|
|
{ |
|
43
|
1 |
|
return $this->setItem('role', $name, $description); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Set item by type and name. |
|
48
|
|
|
* Created if not exists else updates. |
|
49
|
|
|
* @param string $type |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* @param string $description |
|
52
|
|
|
* @return Item |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function setItem($type, $name, $description = null) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$item = $this->getItem($name) ?: $this->createItem($type, $name); |
|
57
|
1 |
|
if ($description) { |
|
|
|
|
|
|
58
|
|
|
$item->description = $description; |
|
59
|
|
|
} |
|
60
|
1 |
|
$this->add($item); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
1 |
|
return $item; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Create item by type and name. |
|
67
|
|
|
* @param string $type |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* @throws InvalidParamException |
|
70
|
|
|
* @return Item |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function createItem($type, $name) |
|
73
|
|
|
{ |
|
74
|
1 |
|
if ('role' === $type) { |
|
75
|
1 |
|
return $this->createRole($name); |
|
76
|
1 |
|
} elseif ('permission' === $type) { |
|
77
|
1 |
|
return $this->createPermission($name); |
|
78
|
|
|
} else { |
|
79
|
|
|
throw new InvalidParamException('Creating unsupported item type: ' . $type); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set child. |
|
85
|
|
|
* @param string|Item $parent |
|
86
|
|
|
* @param string|Item $child |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function setChild($parent, $child) |
|
90
|
|
|
{ |
|
91
|
1 |
View Code Duplication |
if (is_string($parent)) { |
|
|
|
|
|
|
92
|
1 |
|
$name = $parent; |
|
93
|
1 |
|
$parent = $this->getItem($parent); |
|
94
|
1 |
|
if (is_null($parent)) { |
|
95
|
|
|
throw new InvalidParamException("Unknown parent:$name at setChild"); |
|
96
|
|
|
} |
|
97
|
1 |
|
} |
|
98
|
1 |
View Code Duplication |
if (is_string($child)) { |
|
|
|
|
|
|
99
|
1 |
|
$name = $child; |
|
100
|
1 |
|
$child = $this->getItem($child); |
|
101
|
1 |
|
if (is_null($child)) { |
|
102
|
|
|
throw new InvalidParamException("Unknown child:$name at setChild"); |
|
103
|
|
|
} |
|
104
|
1 |
|
} |
|
105
|
1 |
|
if (isset($this->children[$parent->name][$child->name])) { |
|
106
|
|
|
return false; |
|
107
|
1 |
|
} |
|
108
|
1 |
|
return $this->addChild($parent, $child); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
public function setAssignment($role, $userId) |
|
112
|
|
|
{ |
|
113
|
1 |
View Code Duplication |
if (is_string($role)) { |
|
|
|
|
|
|
114
|
1 |
|
$name = $role; |
|
115
|
1 |
|
$role = $this->getRole($role); |
|
116
|
1 |
|
if (is_null($role)) { |
|
117
|
|
|
throw new InvalidParamException("Unknown role:$name at setAssignment"); |
|
118
|
|
|
} |
|
119
|
1 |
|
} |
|
120
|
1 |
|
if (isset($this->assignments[$userId][$role->name])) { |
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
1 |
|
return $this->assign($role, $userId); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: