Completed
Push — master ( c83b97...9053c3 )
by Alexey
04:03
created

PermissionsController::detectLoop()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 12
rs 10
cc 4
nc 4
nop 2
1
<?php
2
3
namespace modules\rbac\controllers;
4
5
use Yii;
6
use yii\data\ArrayDataProvider;
7
use yii\web\Controller;
8
use yii\filters\VerbFilter;
9
use yii\filters\AccessControl;
10
use yii\web\BadRequestHttpException;
11
use yii\widgets\ActiveForm;
12
use yii\web\Response;
13
use Exception;
14
use modules\rbac\models\Permission;
15
use modules\rbac\Module;
16
use modules\rbac\traits\ModuleTrait;
17
18
/**
19
 * Class PermissionsController
20
 * @package modules\rbac\controllers
21
 */
22
class PermissionsController extends Controller
23
{
24
    use ModuleTrait;
25
26
    /**
27
     * @inheritdoc
28
     * @return array
29
     */
30
    public function behaviors()
31
    {
32
        return [
33
            'access' => [
34
                'class' => AccessControl::class,
35
                'rules' => [
36
                    [
37
                        'allow' => true,
38
                        'roles' => ['managerRbac']
39
                    ]
40
                ]
41
            ],
42
            'verbs' => [
43
                'class' => VerbFilter::class,
44
                'actions' => [
45
                    'delete' => ['POST']
46
                ]
47
            ]
48
        ];
49
    }
50
51
    /**
52
     * Lists all Permission models.
53
     * @return mixed
54
     */
55
    public function actionIndex()
56
    {
57
        $auth = Yii::$app->authManager;
58
        $dataProvider = new ArrayDataProvider([
59
            'allModels' => $auth->getPermissions(),
60
            'sort' => [
61
                'attributes' => ['name', 'description', 'ruleName']
62
            ],
63
            'pagination' => [
64
                'pageSize' => 15
65
            ]
66
        ]);
67
        return $this->render('index', [
68
            'dataProvider' => $dataProvider
69
        ]);
70
    }
71
72
    /**
73
     * Displays a single Permission model.
74
     * @param string|int $id
75
     * @return mixed
76
     */
77
    public function actionView($id)
78
    {
79
        $auth = Yii::$app->authManager;
80
        /** @var $permission */
81
        $permission = $auth->getPermission($id);
82
83
        $model = new Permission(['name' => $permission->name]);
84
        return $this->render('view', [
85
            'permission' => $permission,
86
            'model' => $model
87
        ]);
88
    }
89
90
    /**
91
     * Creates Permission a new Permission model.
92
     * If creation is successful, the browser will be redirected to the 'view' page.
93
     * @return string|Response
94
     * @throws Exception
95
     */
96
    public function actionCreate()
97
    {
98
        $model = new Permission(['scenario' => Permission::SCENARIO_CREATE]);
99
        $model->isNewRecord = true;
100
101
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
102
            $auth = Yii::$app->authManager;
103
            $perm = $auth->createPermission($model->name);
104
            $perm->description = $model->description;
105
            if ($auth->add($perm)) {
106
                return $this->redirect(['view', 'id' => $model->name]);
107
            }
108
        }
109
        return $this->render('create', [
110
            'model' => $model
111
        ]);
112
    }
113
114
    /**
115
     * @return array|bool
116
     */
117
    public function actionAjaxValidateForm()
118
    {
119
        $model = new Permission(['scenario' => Permission::SCENARIO_CREATE]);
120
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
121
            Yii::$app->response->format = Response::FORMAT_JSON;
122
            return ActiveForm::validate($model);
123
        }
124
        return false;
125
    }
126
127
    /**
128
     * Updates an existing Permission model.
129
     * If update is successful, the browser will be redirected to the 'view' page.
130
     * @param string|int $id
131
     * @return string|Response
132
     * @throws Exception
133
     */
134
    public function actionUpdate($id)
135
    {
136
        $auth = Yii::$app->authManager;
137
        /** @var  $perm */
138
        $perm = $auth->getPermission($id);
139
140
        $model = new Permission([
141
            'scenario' => Permission::SCENARIO_UPDATE,
142
            'name' => $perm->name,
143
            'description' => $perm->description
144
        ]);
145
        if ($model->load(Yii::$app->request->post())) {
146
            $perm->description = $model->description;
147
            if ($auth->update($id, $perm)) {
148
                return $this->redirect(['view', 'id' => $id]);
149
            }
150
        }
151
        return $this->render('update', [
152
            'model' => $model
153
        ]);
154
    }
155
156
    /**
157
     * Привязываем разрешение
158
     * @return array|Response
159
     * @throws BadRequestHttpException
160
     * @throws Exception
161
     */
162
    public function actionAddPermissions()
163
    {
164
        $model = new Permission([
165
            'scenario' => Permission::SCENARIO_UPDATE
166
        ]);
167
        if ($model->load(Yii::$app->request->post())) {
168
            $auth = Yii::$app->authManager;
169
            /** @var  $permission */
170
            $permission = $auth->getPermission($model->name);
171
            self::addChild($model->permissionItems, $permission);
172
            return $this->redirect(['update', 'id' => $model->name, '#' => 'assign-container-permissions']);
173
        }
174
        throw new BadRequestHttpException(Module::t('module', 'Not a valid request to the method!'));
175
    }
176
177
    /**
178
     * Отвязываем разрешение
179
     * @return array|Response
180
     * @throws BadRequestHttpException
181
     */
182
    public function actionRemovePermissions()
183
    {
184
        $model = new Permission([
185
            'scenario' => Permission::SCENARIO_UPDATE
186
        ]);
187
        if ($model->load(Yii::$app->request->post())) {
188
            $auth = Yii::$app->authManager;
189
            $permission = $auth->getPermission($model->name);
190
            foreach ($model->permissions as $perm) {
191
                $remove = $auth->getPermission($perm);
192
                $auth->removeChild($permission, $remove);
193
            }
194
            return $this->redirect(['update', 'id' => $model->name, '#' => 'assign-container-permissions']);
195
        }
196
        throw new BadRequestHttpException(Module::t('module', 'Not a valid request to the method!'));
197
    }
198
199
    /**
200
     * Deletes an existing Permission model.
201
     * If deletion is successful, the browser will be redirected to the 'index' page.
202
     * @param string|int $id
203
     * @return Response
204
     */
205
    public function actionDelete($id)
206
    {
207
        $auth = Yii::$app->authManager;
208
        /** @var  $perm */
209
        $perm = $auth->getPermission($id);
210
        /** @var yii\web\Session $session */
211
        $session = Yii::$app->session;
212
        if ($auth->remove($perm)) {
213
            $session->setFlash('success', Module::t('module', 'The permission "{:name}" have been successfully deleted.', [':name' => $perm->name]));
214
        } else {
215
            $session->setFlash('error', Module::t('module', 'Error!'));
216
        }
217
        return $this->redirect(['index']);
218
    }
219
}
220