1 | <?php |
||
2 | |||
3 | namespace app\components; |
||
4 | |||
5 | use Yii; |
||
6 | use yii\rbac\ManagerInterface; |
||
7 | use yii\base\{Component, InvalidConfigException}; |
||
8 | use yii\db\ActiveRecordInterface; |
||
9 | use Itstructure\AdminModule\interfaces\ModelInterface; |
||
10 | use app\models\UserValidate; |
||
11 | use app\interfaces\UserValidateComponentInterface; |
||
12 | |||
13 | /** |
||
14 | * Class UserValidateComponent |
||
15 | * Component class for validation user fields. |
||
16 | * |
||
17 | * @property bool $changeRoles |
||
18 | * @property ManagerInterface $authManager |
||
19 | * |
||
20 | * @package app\components |
||
21 | */ |
||
22 | class UserValidateComponent extends Component implements UserValidateComponentInterface |
||
23 | { |
||
24 | /** |
||
25 | * Allow to change roles. |
||
26 | * |
||
27 | * @var bool |
||
28 | */ |
||
29 | public $changeRoles = true; |
||
30 | |||
31 | /** |
||
32 | * Auth manager. |
||
33 | * |
||
34 | * @var ManagerInterface |
||
35 | */ |
||
36 | private $authManager; |
||
37 | |||
38 | /** |
||
39 | * Initialize. |
||
40 | */ |
||
41 | public function init() |
||
42 | { |
||
43 | if (null === $this->authManager) { |
||
44 | $this->setAuthManager(Yii::$app->authManager); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
45 | } |
||
46 | |||
47 | if (null === $this->authManager) { |
||
48 | throw new InvalidConfigException('The authManager is not defined.'); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Set authManager (RBAC). |
||
54 | * |
||
55 | * @param ManagerInterface $authManager |
||
56 | */ |
||
57 | public function setAuthManager(ManagerInterface $authManager): void |
||
58 | { |
||
59 | $this->authManager = $authManager; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Get authManager (RBAC). |
||
64 | * |
||
65 | * @return ManagerInterface |
||
66 | */ |
||
67 | public function getAuthManager(): ManagerInterface |
||
68 | { |
||
69 | return $this->authManager; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Set a user model for UserValidateComponent. |
||
74 | * |
||
75 | * @param ActiveRecordInterface $model |
||
76 | * |
||
77 | * @throws InvalidConfigException |
||
78 | * |
||
79 | * @return ModelInterface |
||
80 | */ |
||
81 | public function setModel(ActiveRecordInterface $model): ModelInterface |
||
82 | { |
||
83 | /** @var ModelInterface $object */ |
||
84 | $object = Yii::createObject([ |
||
85 | 'class' => UserValidate::class, |
||
86 | 'userModel' => $model, |
||
87 | 'authManager' => $this->authManager, |
||
88 | 'changeRoles' => $this->changeRoles, |
||
89 | ]); |
||
90 | |||
91 | return $object; |
||
92 | } |
||
93 | } |
||
94 |