RoleController::actionAssign()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace yii2mod\user\commands;
4
5
use Yii;
6
use yii\console\Controller;
7
use yii\console\Exception;
8
use yii\helpers\Console;
9
use yii\rbac\Role;
10
use yii2mod\user\models\UserModel;
11
12
/**
13
 * Class RoleController
14
 *
15
 * @package yii2mod\user\commands
16
 */
17
class RoleController extends Controller
18
{
19
    /**
20
     * @var \yii\rbac\ManagerInterface
21
     */
22
    private $_authManager;
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function init()
28
    {
29
        parent::init();
30
31
        $this->_authManager = Yii::$app->authManager;
32
    }
33
34
    /**
35
     * Assign role to the user
36
     *
37
     * @param $roleName
38
     * @param $email
39
     *
40
     * @return int
41
     *
42
     * @throws Exception
43
     */
44 View Code Duplication
    public function actionAssign($roleName, $email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
45
    {
46
        $user = UserModel::findByEmail($email);
47
48
        if (empty($user)) {
49
            throw new Exception(Yii::t('yii2mod.user', 'User is not found.'));
50
        }
51
52
        $role = $this->findRole($roleName);
53
54
        if (in_array($roleName, array_keys($this->_authManager->getRolesByUser($user->id)))) {
55
            $this->stdout(Yii::t('yii2mod.user', 'This role already assigned to this user.') . "\n", Console::FG_RED);
56
57
            return self::EXIT_CODE_NORMAL;
0 ignored issues
show
Deprecated Code introduced by
The constant yii\console\Controller::EXIT_CODE_NORMAL has been deprecated with message: since 2.0.13. Use [[ExitCode::OK]] instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
58
        }
59
60
        $this->_authManager->assign($role, $user->id);
61
62
        $this->stdout(Yii::t('yii2mod.user', 'The role has been successfully assigned to the user.') . "\n", Console::FG_GREEN);
63
64
        return self::EXIT_CODE_NORMAL;
0 ignored issues
show
Deprecated Code introduced by
The constant yii\console\Controller::EXIT_CODE_NORMAL has been deprecated with message: since 2.0.13. Use [[ExitCode::OK]] instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
65
    }
66
67
    /**
68
     * Revoke role from the user
69
     *
70
     * @param $roleName
71
     * @param $email
72
     *
73
     * @return int
74
     *
75
     * @throws Exception
76
     */
77 View Code Duplication
    public function actionRevoke($roleName, $email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
78
    {
79
        $user = UserModel::findByEmail($email);
80
81
        if (empty($user)) {
82
            throw new Exception(Yii::t('yii2mod.user', 'User is not found.'));
83
        }
84
85
        $role = $this->findRole($roleName);
86
87
        if (!in_array($roleName, array_keys($this->_authManager->getRolesByUser($user->id)))) {
88
            $this->stdout(Yii::t('yii2mod.user', 'This role is not assigned to this user.') . "\n", Console::FG_RED);
89
90
            return self::EXIT_CODE_NORMAL;
0 ignored issues
show
Deprecated Code introduced by
The constant yii\console\Controller::EXIT_CODE_NORMAL has been deprecated with message: since 2.0.13. Use [[ExitCode::OK]] instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
91
        }
92
93
        $this->_authManager->revoke($role, $user->id);
94
95
        $this->stdout(Yii::t('yii2mod.user', 'The role has been successfully revoked from the user.') . "\n", Console::FG_GREEN);
96
97
        return self::EXIT_CODE_NORMAL;
0 ignored issues
show
Deprecated Code introduced by
The constant yii\console\Controller::EXIT_CODE_NORMAL has been deprecated with message: since 2.0.13. Use [[ExitCode::OK]] instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
98
    }
99
100
    /**
101
     * Returns the named role.
102
     *
103
     * @param string $roleName
104
     *
105
     * @return Role
106
     *
107
     * @throws Exception
108
     */
109
    protected function findRole($roleName)
110
    {
111
        if (($role = $this->_authManager->getRole($roleName)) !== null) {
112
            return $role;
113
        }
114
115
        throw new Exception(Yii::t('yii2mod.user', 'The role "{roleName}" is not found.', [
116
            'roleName' => $roleName,
117
        ]));
118
    }
119
}
120