Completed
Push — master ( c7544d...5e7707 )
by Igor
04:06
created

RolesController::prepareRolesToSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use Yii;
6
use yii\web\Response;
7
use yii\filters\VerbFilter;
8
use yii\helpers\Url;
9
use yii\helpers\ArrayHelper;
10
use app\traits\ModelTrait;
11
use app\models\AuthItem;
12
use app\modules\admin\models\search\AuthItemSearch;
13
14
class RolesController extends \yii\web\Controller
15
{
16
    use ModelTrait;
17
18 13
    public function behaviors()
19
    {
20
        return [
21
            'verbs' => [
22
                'class' => VerbFilter::class,
23
                'actions' => [
24
                    'delete' => ['post'],
25
                    'operations' => ['post'],
26
                ],
27 13
            ],
28
        ];
29
    }
30
31 13
    public function actions()
32
    {
33
        return [
34
            'operations' => [
35
                'class' => 'app\modules\admin\controllers\common\OperationsAction',
36
                'modelClass' => 'app\models\AuthItem',
37
                'operations' => [
38
                    'delete' => [],
39
                ]
40 13
            ],
41
            'delete' => [
42
                'class' => 'app\modules\admin\controllers\common\DeleteAction',
43
                'modelClass' => 'app\models\AuthItem',
44
            ],
45
        ];
46
    }
47
48 13
    public function actionIndex()
49
    {
50 13
        $authItemSearch = new AuthItemSearch();
51 13
        $dataProvider = $authItemSearch->search(Yii::$app->request->get());
52
53 13
        return $this->render('index', [
54 13
            'authItemSearch' => $authItemSearch,
55 13
            'dataProvider' => $dataProvider,
56
        ]);
57
    }
58
59 7
    public function actionEdit($name = null)
60
    {
61 7
        $model = new AuthItem();
62 7
        $auth = Yii::$app->authManager;
63
64 7
        $roles = ArrayHelper::index($auth->getRoles(), 'name');
65 7
        $permissions = ArrayHelper::index($auth->getPermissions(), 'name');
66
67 7
        if ($name) {
68 3
            $model = $this->findModel($model, $name);
0 ignored issues
show
Documentation introduced by
$model is of type object<app\models\AuthItem>, but the function expects a object<app\traits\ActiveRecord>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69 3
            $model = $this->preparePermissionsToSave($model);
70 3
            $model = $this->prepareRolesToSave($model);
71
72 3
            unset($roles[$model->name]);
73
        }
74
75 7
        if (Yii::$app->request->isPost) {
76 6
            $model->type = \yii\rbac\Item::TYPE_ROLE;
77 6
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
78 4
                if (!$model->isSuperUser()) {
79 4
                    $this->setRoles($model, $roles, $permissions);
80
                }
81
82 4
                Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully'));
83 4
                $urlToModel = Url::toRoute(['edit', 'name' => $model->name]);
84 4
                if (Yii::$app->request->isAjax) {
85 1
                    return $this->asJson(['redirect' => $urlToModel]);
86
                }
87 3
                return $this->redirect($urlToModel);
88
            }
89 2
            if (Yii::$app->request->isAjax) {
90 1
                return $this->asJson($this->collectErrors($model));
91
            }
92
        }
93
94 6
        return $this->render('edit', [
95 6
            'model' => $model,
96 6
            'roles' => $roles,
97 6
            'permissions' => $permissions
98
        ]);
99
    }
100
101 4
    private function setRoles($model, $roles, $permissions)
102
    {
103 4
        $auth = Yii::$app->authManager;
104
105 4
        $role = $auth->getRole($model->name);
106 4
        $auth->removeChildren($role);
107
108 4
        if (is_array($model->roles)) {
109 2
            foreach ($model->roles as $r) {
110 1
                $auth->addChild($role, $roles[$r]);
111
            }
112
        }
113
114 4
        if (is_array($model->permissions)) {
115 4
            $currPermissions = ArrayHelper::index(
116 4
                $auth->getPermissionsByRole($model->name),
117 4
                'name',
118 4
                []
119
            );
120 4
            foreach ($model->permissions as $permission) {
121 3
                if (!array_key_exists($permission, $currPermissions)) {
122 3
                    $auth->addChild($role, $permissions[$permission]);
123
                }
124
            }
125
        }
126 4
    }
127
128 3
    private function preparePermissionsToSave($model)
129
    {
130 3
        $permissions = Yii::$app->authManager->getPermissionsByRole($model->name);
131 3
        $model->permissions = ArrayHelper::index($permissions, 'name', []);
132 3
        $model->permissions = array_keys($model->permissions);
133
134 3
        return $model;
135
    }
136
137 3
    private function prepareRolesToSave($model)
138
    {
139 3
        $roles = Yii::$app->authManager->getChildren($model->name);
140 3
        $model->roles = ArrayHelper::index($roles, 'name', []);
141 3
        $model->roles = array_keys($model->roles);
142
143 3
        return $model;
144
    }
145
}
146