Dominus77 /
yii2-basic-start
| 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 app\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 | * Adds role to user |
||||
| 21 | * @throws Exception |
||||
| 22 | * @throws \Exception |
||||
| 23 | */ |
||||
| 24 | public function actionAssign() |
||||
| 25 | { |
||||
| 26 | $authManager = Yii::$app->authManager; |
||||
| 27 | $username = $this->prompt(Console::convertEncoding(Module::t( |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 28 | 'module', |
||||
| 29 | 'Username:' |
||||
| 30 | )), ['required' => true]); |
||||
| 31 | $user = $this->findModel($username); |
||||
| 32 | |||||
| 33 | $roles = Yii::$app->authManager->getRoles(); |
||||
| 34 | $array = ArrayHelper::map($roles, 'name', 'description'); |
||||
| 35 | $encodingArray = Console::convertEncoding($array); |
||||
| 36 | $select = is_array($encodingArray) ? $encodingArray : $array; |
||||
|
0 ignored issues
–
show
|
|||||
| 37 | $roleName = $this->select(Console::convertEncoding(Module::t('module', 'Role:')), $select); |
||||
|
0 ignored issues
–
show
It seems like
app\components\helpers\C...::t('module', 'Role:')) can also be of type array; however, parameter $prompt of yii\console\Controller::select() does only seem to accept string, 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...
|
|||||
| 38 | $role = $authManager->getRole($roleName); |
||||
| 39 | |||||
| 40 | // Проверяем есть ли уже такая роль у пользователя |
||||
| 41 | $userRoles = $this->getUserRoleValue($user->id); |
||||
| 42 | if ($userRoles === null) { |
||||
| 43 | $authManager->assign($role, $user->id); |
||||
| 44 | $this->stdout(Console::convertEncoding(Module::t( |
||||
| 45 | 'module', |
||||
| 46 | 'Success!' |
||||
| 47 | )), Console::FG_GREEN, Console::BOLD); |
||||
| 48 | $this->stdout(PHP_EOL); |
||||
| 49 | } else { |
||||
| 50 | $this->stdout(Console::convertEncoding(Module::t( |
||||
| 51 | 'module', |
||||
| 52 | 'The user already has a role.' |
||||
| 53 | )), Console::FG_RED, Console::BOLD); |
||||
| 54 | $this->stdout(PHP_EOL); |
||||
| 55 | } |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | /** |
||||
| 59 | * Removes role from user |
||||
| 60 | * @throws Exception |
||||
| 61 | */ |
||||
| 62 | public function actionRevoke() |
||||
| 63 | { |
||||
| 64 | $authManager = Yii::$app->authManager; |
||||
| 65 | $username = $this->prompt(Console::convertEncoding(Yii::t( |
||||
|
0 ignored issues
–
show
It seems like
app\components\helpers\C...:t('app', 'Username:')) can also be of type array; however, parameter $text of yii\console\Controller::prompt() does only seem to accept string, 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...
|
|||||
| 66 | 'app', |
||||
| 67 | 'Username:' |
||||
| 68 | )), ['required' => true]); |
||||
| 69 | $user = $this->findModel($username); |
||||
| 70 | $roleName = $this->select( |
||||
| 71 | Console::convertEncoding(Module::t('module', 'Role:')), |
||||
|
0 ignored issues
–
show
It seems like
app\components\helpers\C...::t('module', 'Role:')) can also be of type array; however, parameter $prompt of yii\console\Controller::select() does only seem to accept string, 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...
|
|||||
| 72 | ArrayHelper::merge( |
||||
| 73 | ['all' => Console::convertEncoding(Module::t('module', 'All Roles'))], |
||||
| 74 | Console::convertEncoding( |
||||
| 75 | ArrayHelper::map($authManager->getRolesByUser($user->id), 'name', 'description') |
||||
| 76 | ) |
||||
| 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(Console::convertEncoding(Module::t( |
||||
| 86 | 'module', |
||||
| 87 | 'Done!' |
||||
| 88 | )), Console::FG_GREEN, Console::BOLD); |
||||
| 89 | $this->stdout(PHP_EOL); |
||||
| 90 | } |
||||
| 91 | |||||
| 92 | /** |
||||
| 93 | * @param string|int $user_id |
||||
| 94 | * @return mixed|null |
||||
| 95 | * @throws \Exception |
||||
| 96 | */ |
||||
| 97 | public function getUserRoleValue($user_id) |
||||
| 98 | { |
||||
| 99 | $authManager = Yii::$app->authManager; |
||||
| 100 | if ($role = $authManager->getRolesByUser($user_id)) { |
||||
| 101 | return ArrayHelper::getValue($role, function ($role) { |
||||
| 102 | foreach ($role as $key => $value) { |
||||
| 103 | return $value->name; |
||||
| 104 | } |
||||
| 105 | return null; |
||||
| 106 | }); |
||||
| 107 | } |
||||
| 108 | return null; |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | /** |
||||
| 112 | * Finds the User model based on its primary key value. |
||||
| 113 | * If the model is not found, a 404 HTTP exception will be thrown. |
||||
| 114 | * |
||||
| 115 | * @param string $username |
||||
| 116 | * @return null|User the loaded model |
||||
| 117 | * @throws Exception if the model cannot be found |
||||
| 118 | */ |
||||
| 119 | private function findModel($username) |
||||
| 120 | { |
||||
| 121 | if (!$model = User::findOne(['username' => $username])) { |
||||
| 122 | throw new Exception( |
||||
| 123 | Console::convertEncoding( |
||||
|
0 ignored issues
–
show
It seems like
app\components\helpers\C...ername' => $username))) can also be of type array; however, parameter $message of yii\console\Exception::__construct() does only seem to accept string, 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...
|
|||||
| 124 | Module::t('module', 'User "{:Username}" not found', [':Username' => $username]) |
||||
| 125 | ) |
||||
| 126 | ); |
||||
| 127 | } |
||||
| 128 | return $model; |
||||
| 129 | } |
||||
| 130 | } |
||||
| 131 |