1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mdm\admin\models; |
4
|
|
|
|
5
|
|
|
use mdm\admin\components\Configs; |
6
|
|
|
use mdm\admin\components\Helper; |
7
|
|
|
use Yii; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Description of Assignment |
11
|
|
|
* |
12
|
|
|
* @author Misbahul D Munir <[email protected]> |
13
|
|
|
* @since 2.5 |
14
|
|
|
*/ |
15
|
|
|
class Assignment extends \mdm\admin\BaseObject |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var integer User id |
19
|
|
|
*/ |
20
|
|
|
public $id; |
21
|
|
|
/** |
22
|
|
|
* @var \yii\web\IdentityInterface User |
23
|
|
|
*/ |
24
|
|
|
public $user; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function __construct($id, $user = null, $config = array()) |
30
|
|
|
{ |
31
|
|
|
$this->id = $id; |
32
|
|
|
$this->user = $user; |
33
|
|
|
parent::__construct($config); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Grands a roles from a user. |
38
|
|
|
* @param array $items |
39
|
|
|
* @return integer number of successful grand |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function assign($items) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$manager = Configs::authManager(); |
44
|
|
|
$success = 0; |
45
|
|
|
|
46
|
|
|
$current_user_id = Yii::$app->getUser()->getId(); |
|
|
|
|
47
|
|
|
|
48
|
|
|
foreach ($items as $name) { |
49
|
|
|
try { |
50
|
|
|
$verify_result = $manager->checkAccess($current_user_id, $name); |
51
|
|
|
|
52
|
|
|
Yii::debug("verify role|permission: $name, result: " |
|
|
|
|
53
|
|
|
. ($verify_result ? "Y" : "N")); |
54
|
|
|
|
55
|
|
|
if ($verify_result) { |
56
|
|
|
$item = $manager->getRole($name); |
|
|
|
|
57
|
|
|
$item = $item ?: $manager->getPermission($name); |
58
|
|
|
$manager->assign($item, $this->id); |
|
|
|
|
59
|
|
|
$success++; |
60
|
|
|
} |
61
|
|
|
} catch (\Exception $exc) { |
62
|
|
|
Yii::error($exc->getMessage(), __METHOD__); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
Helper::invalidate(); |
66
|
|
|
return $success; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Revokes a roles from a user. |
71
|
|
|
* @param array $items |
72
|
|
|
* @return integer number of successful revoke |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function revoke($items) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$current_user_id = Yii::$app->getUser()->getId(); |
|
|
|
|
77
|
|
|
$manager = Configs::authManager(); |
78
|
|
|
$success = 0; |
79
|
|
|
foreach ($items as $name) { |
80
|
|
|
try { |
81
|
|
|
$verify_result = $manager->checkAccess($current_user_id, $name); |
82
|
|
|
|
83
|
|
|
Yii::debug("verify role|permission: $name, result: " |
|
|
|
|
84
|
|
|
. ($verify_result ? "Y" : "N")); |
85
|
|
|
|
86
|
|
|
if ($verify_result) { |
87
|
|
|
$item = $manager->getRole($name); |
|
|
|
|
88
|
|
|
$item = $item ?: $manager->getPermission($name); |
89
|
|
|
$manager->revoke($item, $this->id); |
|
|
|
|
90
|
|
|
$success++; |
91
|
|
|
} |
92
|
|
|
} catch (\Exception $exc) { |
93
|
|
|
Yii::error($exc->getMessage(), __METHOD__); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
Helper::invalidate(); |
97
|
|
|
return $success; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get all available and assigned roles/permission |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getItems() |
105
|
|
|
{ |
106
|
|
|
$current_user_id = Yii::$app->getUser()->getId(); |
|
|
|
|
107
|
|
|
$manager = Configs::authManager(); |
108
|
|
|
$available = []; |
109
|
|
|
|
110
|
|
|
$roles = $manager->getRolesByUser($current_user_id); |
111
|
|
|
|
112
|
|
|
foreach ($roles as $role) { |
113
|
|
|
$name = $role->name; |
114
|
|
|
$available[$name][0] = 'role'; |
115
|
|
|
$available[$name][1] = $role->description; |
116
|
|
|
|
117
|
|
|
$child_roles = $manager->getChildRoles($name); |
|
|
|
|
118
|
|
|
foreach ($child_roles as $childRole) |
119
|
|
|
{ |
120
|
|
|
$name = $childRole->name; |
121
|
|
|
$available[$name][0] = 'role'; |
122
|
|
|
$available[$name][1] = $childRole->description; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
$permissions = $manager->getPermissionsByUser($current_user_id); |
128
|
|
|
|
129
|
|
|
foreach ($permissions as $permission) { |
130
|
|
|
$name = $permission->name; |
131
|
|
|
if ($name[0] != '/') { |
132
|
|
|
$available[$name][0] = 'permission'; |
133
|
|
|
$available[$name][1] = $permission->description; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$assigned = []; |
138
|
|
|
foreach ($manager->getAssignments($this->id) as $item) { |
139
|
|
|
if(isset($available[$item->roleName])) { |
140
|
|
|
$assigned[$item->roleName] = $available[$item->roleName]; |
141
|
|
|
unset($available[$item->roleName]); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
ksort($available); |
146
|
|
|
ksort($assigned); |
147
|
|
|
return [ |
148
|
|
|
'available' => $available, |
149
|
|
|
'assigned' => $assigned, |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritdoc |
155
|
|
|
*/ |
156
|
|
|
public function __get($name) |
157
|
|
|
{ |
158
|
|
|
if ($this->user) { |
159
|
|
|
return $this->user->$name; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.