1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Helper; |
13
|
|
|
|
14
|
|
|
use Da\User\Model\AbstractAuthItem; |
15
|
|
|
use Da\User\Module; |
16
|
|
|
use Da\User\Traits\AuthManagerAwareTrait; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\helpers\ArrayHelper; |
19
|
|
|
use yii\rbac\Permission; |
20
|
|
|
use yii\rbac\Role; |
21
|
|
|
use yii\rbac\Rule; |
22
|
|
|
|
23
|
|
|
class AuthHelper |
24
|
|
|
{ |
25
|
|
|
use AuthManagerAwareTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Checks whether a user has certain role. |
29
|
|
|
* |
30
|
|
|
* @param $userId |
31
|
|
|
* @param $role |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public function hasRole($userId, $role) |
36
|
|
|
{ |
37
|
|
|
if ($this->getAuthManager()) { |
38
|
|
|
$roles = array_keys($this->getAuthManager()->getRolesByUser($userId)); |
39
|
|
|
|
40
|
|
|
return in_array($role, $roles, true); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $username |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
2 |
|
public function isAdmin($username) |
52
|
|
|
{ |
53
|
|
|
/** @var Module $module */ |
54
|
2 |
|
$module = Yii::$app->getModule('user'); |
55
|
2 |
|
$hasAdministratorPermissionName = $this->getAuthManager() && $module->administratorPermissionName |
56
|
|
|
? Yii::$app->getUser()->can($module->administratorPermissionName) |
|
|
|
|
57
|
2 |
|
: false; |
58
|
|
|
|
59
|
2 |
|
return $hasAdministratorPermissionName || in_array($username, $module->administrators, false); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $name |
64
|
|
|
* |
65
|
|
|
* @return null|\yii\rbac\Item|Permission |
66
|
|
|
*/ |
67
|
|
|
public function getPermission($name) |
68
|
|
|
{ |
69
|
|
|
return $this->getAuthManager()->getPermission($name); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $name |
74
|
|
|
* |
75
|
|
|
* @return null|\yii\rbac\Item|Role |
76
|
|
|
*/ |
77
|
|
|
public function getRole($name) |
78
|
|
|
{ |
79
|
|
|
return $this->getAuthManager()->getRole($name); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Removes a role, permission or rule from the RBAC system. |
84
|
|
|
* |
85
|
|
|
* @param Role|Permission|Rule $object |
86
|
|
|
* |
87
|
|
|
* @return bool whether the role, permission or rule is successfully removed |
88
|
|
|
*/ |
89
|
|
|
public function remove($object) |
90
|
|
|
{ |
91
|
|
|
return $this->getAuthManager()->remove($object); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param AbstractAuthItem $model |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function getUnassignedItems(AbstractAuthItem $model) |
100
|
|
|
{ |
101
|
|
|
$excludeItems = $model->item !== null ? [$model->item->name] : []; |
102
|
|
|
$type = $model->getType() === Permission::TYPE_PERMISSION ? Permission::TYPE_PERMISSION : null; |
103
|
|
|
$items = $this->getAuthManager()->getItems($type, $excludeItems); |
104
|
|
|
|
105
|
|
|
return ArrayHelper::map( |
106
|
|
|
$items, |
107
|
|
|
'name', |
108
|
|
|
function ($item) { |
109
|
|
|
return empty($item->description) ? $item->name : "{$item->name} ({$item->description})"; |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: