toir427 /
yii2-admin
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace toir427\admin\models; |
||||
| 4 | |||||
| 5 | use toir427\admin\components\Configs; |
||||
| 6 | use toir427\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 \toir427\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 | public function assign($items) |
||||
| 42 | { |
||||
| 43 | $manager = Configs::authManager(); |
||||
| 44 | $success = 0; |
||||
| 45 | foreach ($items as $name) { |
||||
| 46 | try { |
||||
| 47 | $item = $manager->getRole($name); |
||||
| 48 | $item = $item ?: $manager->getPermission($name); |
||||
| 49 | $manager->assign($item, $this->id); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 50 | $success++; |
||||
| 51 | } catch (\Exception $exc) { |
||||
| 52 | Yii::error($exc->getMessage(), __METHOD__); |
||||
| 53 | } |
||||
| 54 | } |
||||
| 55 | Helper::invalidate(); |
||||
| 56 | return $success; |
||||
| 57 | } |
||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * Revokes a roles from a user. |
||||
| 61 | * @param array $items |
||||
| 62 | * @return integer number of successful revoke |
||||
| 63 | */ |
||||
| 64 | public function revoke($items) |
||||
| 65 | { |
||||
| 66 | $manager = Configs::authManager(); |
||||
| 67 | $success = 0; |
||||
| 68 | foreach ($items as $name) { |
||||
| 69 | try { |
||||
| 70 | $item = $manager->getRole($name); |
||||
| 71 | $item = $item ?: $manager->getPermission($name); |
||||
| 72 | $manager->revoke($item, $this->id); |
||||
|
0 ignored issues
–
show
It seems like
$item can also be of type yii\rbac\Permission; however, parameter $role of yii\rbac\ManagerInterface::revoke() does only seem to accept yii\rbac\Role, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 73 | $success++; |
||||
| 74 | } catch (\Exception $exc) { |
||||
| 75 | Yii::error($exc->getMessage(), __METHOD__); |
||||
| 76 | } |
||||
| 77 | } |
||||
| 78 | Helper::invalidate(); |
||||
| 79 | return $success; |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | /** |
||||
| 83 | * Get all available and assigned roles/permission |
||||
| 84 | * @return array |
||||
| 85 | */ |
||||
| 86 | public function getItems() |
||||
| 87 | { |
||||
| 88 | $manager = Configs::authManager(); |
||||
| 89 | $available = []; |
||||
| 90 | foreach (array_keys($manager->getRoles()) as $name) { |
||||
| 91 | $available[$name] = 'role'; |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | foreach (array_keys($manager->getPermissions()) as $name) { |
||||
| 95 | if ($name[0] != '/') { |
||||
| 96 | $available[$name] = 'permission'; |
||||
| 97 | } |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | $assigned = []; |
||||
| 101 | foreach ($manager->getAssignments($this->id) as $item) { |
||||
| 102 | $assigned[$item->roleName] = $available[$item->roleName]; |
||||
| 103 | unset($available[$item->roleName]); |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | return [ |
||||
| 107 | 'available' => $available, |
||||
| 108 | 'assigned' => $assigned, |
||||
| 109 | ]; |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | /** |
||||
| 113 | * @inheritdoc |
||||
| 114 | */ |
||||
| 115 | public function __get($name) |
||||
| 116 | { |
||||
| 117 | if ($this->user) { |
||||
| 118 | return $this->user->$name; |
||||
| 119 | } |
||||
| 120 | } |
||||
| 121 | } |
||||
| 122 |