1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\rbac\console; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\console\Controller; |
7
|
|
|
use yii\console\Exception; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
use modules\users\models\User; |
10
|
|
|
use console\components\helpers\Console; |
11
|
|
|
use modules\rbac\Module; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class RolesController |
15
|
|
|
* @package modules\rbac\console |
16
|
|
|
*/ |
17
|
|
|
class RolesController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Color |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
public $color = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Adds role to user |
27
|
|
|
* @throws Exception |
28
|
|
|
* @throws \Exception |
29
|
|
|
*/ |
30
|
|
|
public function actionAssign() |
31
|
|
|
{ |
32
|
|
|
$authManager = Yii::$app->authManager; |
33
|
|
|
$username = $this->prompt(Module::translate('module', 'Username:'), ['required' => true]); |
34
|
|
|
$user = $this->findModel($username); |
35
|
|
|
|
36
|
|
|
$roles = Yii::$app->authManager->getRoles(); |
37
|
|
|
$select = ArrayHelper::map($roles, 'name', 'description'); |
38
|
|
|
/*$encodingArray = Console::convertEncoding($array); |
39
|
|
|
$select = is_array($encodingArray) ? $encodingArray : $array;*/ |
40
|
|
|
$roleName = $this->select(Module::translate('module', 'Role:'), $select); |
41
|
|
|
$role = $authManager->getRole($roleName); |
42
|
|
|
|
43
|
|
|
// Проверяем есть ли уже такая роль у пользователя |
44
|
|
|
$userRoles = $this->getUserRoleValue($user->id); |
45
|
|
|
if ($userRoles === null) { |
46
|
|
|
$authManager->assign($role, $user->id); |
47
|
|
|
$this->stdout( |
48
|
|
|
Module::translate('module', 'Success!'), |
49
|
|
|
Console::FG_GREEN, |
50
|
|
|
Console::BOLD |
51
|
|
|
); |
52
|
|
|
$this->stdout(PHP_EOL); |
53
|
|
|
} else { |
54
|
|
|
$this->stdout( |
55
|
|
|
Module::translate('module', 'The user already has a role.'), |
56
|
|
|
Console::FG_RED, |
57
|
|
|
Console::BOLD |
58
|
|
|
); |
59
|
|
|
$this->stdout(PHP_EOL); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Removes role from user |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
|
|
public function actionRevoke() |
68
|
|
|
{ |
69
|
|
|
$authManager = Yii::$app->authManager; |
70
|
|
|
$username = $this->prompt(Module::translate('module', 'Username:'), ['required' => true]); |
71
|
|
|
$user = $this->findModel($username); |
72
|
|
|
$roleName = $this->select( |
73
|
|
|
Module::translate('module', 'Role:'), |
74
|
|
|
ArrayHelper::merge( |
75
|
|
|
['all' => Module::translate('module', 'All Roles')], |
76
|
|
|
ArrayHelper::map($authManager->getRolesByUser($user->id), 'name', 'description') |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
if ($roleName === 'all') { |
80
|
|
|
$authManager->revokeAll($user->id); |
81
|
|
|
} else { |
82
|
|
|
$role = $authManager->getRole($roleName); |
83
|
|
|
$authManager->revoke($role, $user->id); |
84
|
|
|
} |
85
|
|
|
$this->stdout(Module::translate('module', 'Success!'), Console::FG_GREEN, Console::BOLD); |
86
|
|
|
$this->stdout(PHP_EOL); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string|int $user_id |
91
|
|
|
* @return mixed|null |
92
|
|
|
*/ |
93
|
|
|
public function getUserRoleValue($user_id) |
94
|
|
|
{ |
95
|
|
|
$authManager = Yii::$app->authManager; |
96
|
|
|
if ($role = $authManager->getRolesByUser($user_id)) { |
97
|
|
|
return array_key_first($role); |
98
|
|
|
} |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Finds the User model based on its primary key value. |
104
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
105
|
|
|
* |
106
|
|
|
* @param string $username |
107
|
|
|
* @return User the loaded model |
108
|
|
|
* @throws Exception if the model cannot be found |
109
|
|
|
*/ |
110
|
|
|
private function findModel($username) |
111
|
|
|
{ |
112
|
|
|
if (!$model = User::findOne(['username' => $username])) { |
113
|
|
|
throw new Exception( |
114
|
|
|
Module::translate('module', 'User "{:Username}" not found', [':Username' => $username]) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
return $model; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|