Issues (42)

src/models/ProfileValidate.php (3 issues)

1
<?php
2
3
namespace Itstructure\RbacModule\models;
4
5
use yii\rbac\{ManagerInterface, Role as BaseRole};
6
use yii\base\{Model, InvalidConfigException};
7
use Itstructure\RbacModule\interfaces\{ModelInterface, RbacIdentityInterface};
8
use Itstructure\RbacModule\Module;
9
10
/**
11
 * Class for validation user(profile) roles.
12
 *
13
 * @property string $userUame
14
 * @property BaseRole[] $roles
15
 * @property RbacIdentityInterface $profileModel
16
 * @property ManagerInterface $authManager
17
 *
18
 * @package Itstructure\RbacModule\models
19
 */
20
class ProfileValidate extends Model implements ModelInterface
21
{
22
    /**
23
     * Current profile (user) model.
24
     *
25
     * @var RbacIdentityInterface
26
     */
27
    private $profileModel;
28
29
    /**
30
     * Auth manager.
31
     *
32
     * @var ManagerInterface
33
     */
34
    private $authManager;
35
36
    /**
37
     * Initialize.
38
     */
39
    public function init()
40
    {
41
        if (null === $this->authManager) {
42
            throw new InvalidConfigException('The authManager is not defined.');
43
        }
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function rules()
50
    {
51
        return [
52
            [
53
                'roles',
54
                'required',
55
            ],
56
            [
57
                'roles',
58
                'validateRoles',
59
            ],
60
        ];
61
    }
62
63
    /**
64
     * List if attributes.
65
     *
66
     * @return array
67
     */
68
    public function attributes()
69
    {
70
        return [
71
            'roles',
72
        ];
73
    }
74
75
    /**
76
     * List if attribute labels.
77
     *
78
     * @inheritdoc
79
     */
80
    public function attributeLabels()
81
    {
82
        return [
83
            'roles' => Module::t('roles', 'Roles'),
84
        ];
85
    }
86
87
    /**
88
     * Get user name.
89
     *
90
     * @return string
91
     */
92
    public function getUserName(): string
93
    {
94
        return $this->profileModel->getUserName();
95
    }
96
97
    /**
98
     * Set new roles values.
99
     *
100
     * @param mixed $roles
101
     */
102
    public function setRoles($roles)
103
    {
104
        $this->roles = $roles;
105
    }
106
107
    /**
108
     * List of profile assigned roles.
109
     *
110
     * @return string[]
111
     */
112
    public function getRoles()
113
    {
114
        return array_keys($this->profileModel->getRoles());
115
    }
116
117
    /**
118
     * Set profile (user) model.
119
     *
120
     * @param RbacIdentityInterface $model.
121
     *
122
     * @throws InvalidConfigException
123
     *
124
     * @return void
125
     */
126
    public function setProfileModel(RbacIdentityInterface $model): void
127
    {
128
        $this->profileModel = $model;
129
    }
130
131
    /**
132
     * Returns profile (user) model.
133
     *
134
     * @return RbacIdentityInterface
135
     */
136
    public function getProfileModel(): RbacIdentityInterface
137
    {
138
        return $this->profileModel;
139
    }
140
141
    /**
142
     * Set auth manager.
143
     *
144
     * @param ManagerInterface $authManager
145
     */
146
    public function setAuthManager(ManagerInterface $authManager)
147
    {
148
        $this->authManager = $authManager;
149
    }
150
151
    /**
152
     * Get auth manager.
153
     *
154
     * @return ManagerInterface
155
     */
156
    public function getAuthManager(): ManagerInterface
157
    {
158
        return $this->authManager;
159
    }
160
161
    /**
162
     * Validate roles data format.
163
     *
164
     * @param $attribute
165
     *
166
     * @return bool
167
     */
168
    public function validateRoles($attribute): bool
169
    {
170
        if (!is_array($this->roles)) {
0 ignored issues
show
The condition is_array($this->roles) is always true.
Loading history...
171
            $this->addError($attribute, 'Incorrect roles data format.');
172
            return false;
173
        }
174
175
        return true;
176
    }
177
178
    /**
179
     * Save user data.
180
     *
181
     * @return bool
182
     */
183
    public function save(): bool
184
    {
185
        if (!$this->validate()) {
186
            return false;
187
        }
188
189
        $this->assignRoles();
190
191
        return true;
192
    }
193
194
    /**
195
     * Returns current model id.
196
     *
197
     * @return int
198
     */
199
    public function getId(): int
200
    {
201
        return $this->profileModel->getId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->profileModel->getId() could return the type string which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
202
    }
203
204
    /**
205
     * Assign roles.
206
     *
207
     * @return void
208
     */
209
    private function assignRoles(): void
210
    {
211
        if (!$this->profileModel->getIsNewRecord()) {
212
            $this->authManager->revokeAll(
213
                $this->profileModel->getId()
214
            );
215
        }
216
217
        foreach ($this->roles as $role) {
218
            $roleObject = $this->authManager->getRole($role);
0 ignored issues
show
$role of type yii\rbac\Role is incompatible with the type string expected by parameter $name of yii\rbac\ManagerInterface::getRole(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

218
            $roleObject = $this->authManager->getRole(/** @scrutinizer ignore-type */ $role);
Loading history...
219
220
            if (null === $roleObject){
221
                continue;
222
            }
223
224
            $this->authManager->assign($roleObject, $this->profileModel->getId());
225
        }
226
    }
227
}
228