Completed
Push — master ( c064eb...85769a )
by Igor
03:38
created

AuthItemForm::assignRolesAndPermissions()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
ccs 0
cts 18
cp 0
rs 8.439
cc 6
eloc 16
nc 8
nop 0
crap 42
1
<?php
2
3
namespace app\modules\admin\models\forms;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use app\models\AuthItem;
8
9
class AuthItemForm extends \yii\base\Model
10
{
11
    /**
12
     * @var string
13
     */
14
    public $name;
15
    /**
16
     * @var string
17
     */
18
    public $description;
19
    /**
20
     * @var array
21
     */
22
    public $roles;
23
    /**
24
     * @var array
25
     */
26
    public $permissions;
27
    /**
28
     * @var int
29
     */
30
    public $created_at;
31
    /**
32
     * @var int
33
     */
34
    public $updated_at;
35
    /**
36
     * @var \app\models\AuthItem
37
     */
38
    private $model;
39
40
   /**
41
    * @return array the validation rules.
42
    */
43 2
    public function rules()
44
    {
45
        return [
46
            [
47 2
                ['name', 'description'], 'required'
48
            ],
49
            [
50
                ['roles', 'permissions'], 'safe'
51
            ],
52
53
            ['name', 'unique', 'targetClass' => AuthItem::class, 'filter' => function ($query) {
54
                if (!$this->model()->isNewRecord) {
55
                    $query->andWhere(['not', ['name' => $this->model()->name]]);
56
                }
57 2
            }],
58
            ['name', 'string', 'max' => 64],
59
            ['name', 'match', 'pattern' => '/^[a-z]\w*$/i'],
60
        ];
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 2
    public function attributeLabels()
67
    {
68
        return [
69 2
            'name'        => Yii::t('app', 'Name'),
70 2
            'description' => Yii::t('app', 'Description'),
71 2
            'roles'       => Yii::t('app', 'Inherit role'),
72 2
            'permissions' => Yii::t('app', 'Permissions'),
73
        ];
74
    }
75
76
    /**
77
     * Set model
78
     *
79
     * @param AuthItem $model
80
     */
81 1
    public function setModel(AuthItem $model): void
82
    {
83 1
        $this->model = $model;
84
85 1
        $this->name = $model->name;
86 1
        $this->description = $model->description;
87 1
        $this->created_at = $model->created_at;
88 1
        $this->updated_at = $model->updated_at;
89
90 1
        $permissions = Yii::$app->authManager->getPermissionsByRole($this->name);
91 1
        $this->permissions = ArrayHelper::index($permissions, 'name', []);
92 1
        $this->permissions = array_keys($this->permissions);
93
94 1
        $roles = Yii::$app->authManager->getChildren($this->name);
95 1
        $this->roles = ArrayHelper::index($roles, 'name', []);
96 1
        $this->roles = array_keys($this->roles);
97 1
    }
98
99
    /**
100
     * Get model
101
     *
102
     * @return AuthItem
103
     */
104 2
    public function model(): AuthItem
105
    {
106 2
        if ($this->model === null) {
107 1
            $this->model = new AuthItem();
108
        }
109
110 2
        return $this->model;
111
    }
112
113
    public function save()
114
    {
115
        $model = $this->model();
116
117
        $model->name = $this->name;
118
        $model->description = $this->description;
119
        $model->type = \yii\rbac\Item::TYPE_ROLE;
120
121
        if (!$model->save()) {
122
            throw new \yii\base\Exception(Yii::t('app.msg', 'An error occurred while saving'));
123
        }
124
125
        if (!$model->isSuperUser()) {
126
            $this->assignRolesAndPermissions();
127
        }
128
129
        return $model;
130
    }
131
132 2
    public function permissionsList(): array
133
    {
134 2
        $permissions = Yii::$app->authManager->getPermissions();
135 2
        return ArrayHelper::map($permissions, 'name', function ($row) {
136 2
            return Yii::t('app', $row->description);
137 2
        });
138
    }
139
140 2
    public function rolesList(): array
141
    {
142 2
        $roles = Yii::$app->authManager->getRoles();
143 2
        unset($roles[$this->model->name]);
144
145 2
        return ArrayHelper::map($roles, 'name', 'description');
146
    }
147
148
    private function assignRolesAndPermissions(): void
149
    {
150
        $auth = Yii::$app->authManager;
151
152
        $role = $auth->getRole($this->model->name);
153
        $roles = $auth->getRoles();
154
        $permissions = $auth->getPermissions();
155
156
        $auth->removeChildren($role);
157
158
        if (is_array($this->roles)) {
159
            foreach ($this->roles as $r) {
160
                $auth->addChild($role, $roles[$r]);
161
            }
162
        }
163
164
        if (is_array($this->permissions)) {
165
            $currPermissions = ArrayHelper::index(
166
                $auth->getPermissionsByRole($this->model->name),
167
                'name',
168
                []
169
            );
170
            foreach ($this->permissions as $permission) {
171
                if (!array_key_exists($permission, $currPermissions)) {
172
                    $auth->addChild($role, $permissions[$permission]);
173
                }
174
            }
175
        }
176
    }
177
}
178