Passed
Push — master ( 07b7c5...d13f2e )
by Alexey
05:08
created

AssignController::actionUpdate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 12
nc 5
nop 1
1
<?php
2
namespace modules\rbac\controllers\backend;
3
4
use Yii;
5
use modules\rbac\models\Assignment;
6
use yii\data\ArrayDataProvider;
7
use yii\web\Controller;
8
use yii\filters\VerbFilter;
9
use yii\filters\AccessControl;
10
use yii\base\InvalidConfigException;
11
use yii\web\NotFoundHttpException;
12
use modules\rbac\Module;
13
14
/**
15
 * Class AssignController
16
 * @package modules\rbac\controllers\backend
17
 */
18
class AssignController extends Controller
19
{
20
    /** @var $user object */
21
    private $_user = null;
22
23
    public function beforeAction($action)
24
    {
25
        if (empty(Yii::$app->controller->module->params['userClass'])) {
26
            throw new InvalidConfigException(Module::t('module', 'You must specify the User class in the module settings.'));
27
        }
28
        $this->_user = new Yii::$app->controller->module->params['userClass']();
29
        return parent::beforeAction($action);
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function behaviors()
36
    {
37
        return [
38
            'access' => [
39
                'class' => AccessControl::className(),
40
                'rules' => [
41
                    [
42
                        'allow' => true,
43
                        'roles' => ['managerRbac'],
44
                    ],
45
                ],
46
            ],
47
            'verbs' => [
48
                'class' => VerbFilter::className(),
49
                'actions' => [
50
                    'revoke' => ['POST'],
51
                ],
52
            ],
53
        ];
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function actionIndex()
60
    {
61
        $assignModel = new Assignment();
62
        $users = $this->_user->find()->all();
63
        $dataProvider = new ArrayDataProvider([
64
            'allModels' => $users,
65
            'sort' => [
66
                'attributes' => ['username', 'role'],
67
            ],
68
            'pagination' => [
69
                'pageSize' => 25,
70
            ],
71
        ]);
72
        return $this->render('index', [
73
            'dataProvider' => $dataProvider,
74
            'assignModel' => $assignModel,
75
        ]);
76
    }
77
78
    /**
79
     * @param $id
80
     * @return string
81
     * @throws NotFoundHttpException
82
     */
83
    public function actionView($id)
84
    {
85
        $assignModel = new Assignment();
86
        return $this->render('view', [
87
            'model' => $this->findModel($id),
88
            'assignModel' => $assignModel,
89
        ]);
90
    }
91
92
    /**
93
     * @param $id
94
     * @return string|\yii\web\Response
95
     * @throws NotFoundHttpException
96
     */
97
    public function actionUpdate($id)
98
    {
99
        $model = new Assignment([
100
            'user' => $this->findModel($id)
101
        ]);
102
        if ($model->load(Yii::$app->request->post())) {
103
            $auth = Yii::$app->authManager;
104
            $role = $auth->getRole($model->role);
105
            // отвязываем роли если есть
106
            if ($auth->getRolesByUser($model->user->id)) {
107
                $auth->revokeAll($model->user->id);
108
            }
109
            // Привязываем новую роль
110
            if ($auth->assign($role, $model->user->id)) {
111
                return $this->redirect(['view', 'id' => $model->user->id]);
112
            }
113
        }
114
        $model->role = $model->getRoleUser();
115
        return $this->render('update', [
116
            'model' => $model,
117
        ]);
118
    }
119
120
    /**
121
     * @param $id
122
     * @return \yii\web\Response
123
     * @throws NotFoundHttpException
124
     */
125
    public function actionRevoke($id)
126
    {
127
        $model = $this->findModel($id);
128
        $auth = Yii::$app->authManager;
129
        if ($auth->getRolesByUser($model->id)) {
130
            if ($auth->revokeAll($model->id)) {
131
                Yii::$app->session->setFlash('success', Module::t('module', 'User "{:username}" successfully unassigned.', [':username' => $model->username]));
0 ignored issues
show
Bug Best Practice introduced by
The property username does not exist on modules\rbac\controllers\backend\AssignController. Since you implemented __get, consider adding a @property annotation.
Loading history...
132
            } else {
133
                Yii::$app->session->setFlash('error', Module::t('module', 'Error!'));
134
            }
135
        } else {
136
            Yii::$app->session->setFlash('warning', Module::t('module', 'User "{:username}" is not attached to any role!', [':username' => $model->username]));
137
        }
138
        return $this->redirect(['index']);
139
    }
140
141
    /**
142
     * @param $id
143
     * @return static
144
     * @throws NotFoundHttpException
145
     */
146
    protected function findModel($id)
147
    {
148
        $userModel = $this->_user;
149
        if (($model = $userModel->findOne($id)) !== null) {
150
            return $model;
151
        } else {
152
            throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
153
        }
154
    }
155
}
156