1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Model; |
13
|
|
|
|
14
|
|
|
use Da\User\Traits\AuthManagerAwareTrait; |
15
|
|
|
use Da\User\Validator\RbacItemsValidator; |
16
|
|
|
use Da\User\Validator\RbacRuleExistsValidator; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\base\Model; |
19
|
|
|
use yii\rbac\Item; |
20
|
|
|
|
21
|
|
|
abstract class AbstractAuthItem extends Model |
22
|
|
|
{ |
23
|
|
|
use AuthManagerAwareTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $itemName; |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $name; |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
public $description; |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $rule; |
41
|
|
|
/** |
42
|
|
|
* @var string[] |
43
|
|
|
*/ |
44
|
|
|
public $children = []; |
45
|
|
|
/** |
46
|
|
|
* @var \yii\rbac\Role|\yii\rbac\Permission |
47
|
|
|
*/ |
48
|
|
|
public $item; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function init() |
54
|
|
|
{ |
55
|
|
|
parent::init(); |
56
|
|
|
|
57
|
|
|
if ($this->item instanceof Item) { |
58
|
|
|
$this->itemName = $this->item->name; |
59
|
|
|
$this->name = $this->item->name; |
60
|
|
|
$this->description = $this->item->description; |
61
|
|
|
$this->children = array_keys($this->getAuthManager()->getChildren($this->item->name)); |
|
|
|
|
62
|
|
|
if ($this->item->ruleName !== null) { |
63
|
|
|
$this->rule = $this->item->ruleName; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function attributeLabels() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
'name' => Yii::t('usuario', 'Name'), |
75
|
|
|
'description' => Yii::t('usuario', 'Description'), |
76
|
|
|
'children' => Yii::t('usuario', 'Children'), |
77
|
|
|
'rule' => Yii::t('usuario', 'Rule'), |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function scenarios() |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
'create' => ['name', 'description', 'children', 'rule'], |
88
|
|
|
'update' => ['name', 'description', 'children', 'rule'], |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function rules() |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
['itemName', 'safe'], |
99
|
|
|
['name', 'required'], |
100
|
|
|
['name', 'match', 'pattern' => '/^\w[\w.:\-]+\w$/'], |
101
|
|
|
[['name', 'description', 'rule'], 'trim'], |
102
|
|
|
[ |
103
|
|
|
'name', |
104
|
|
|
function () { |
105
|
|
|
if ($this->getAuthManager()->getItem($this->name) !== null) { |
106
|
|
|
$this->addError('name', Yii::t('usuario', 'Auth item with such name already exists')); |
107
|
|
|
} |
108
|
|
|
}, |
109
|
|
|
'when' => function () { |
110
|
|
|
return $this->scenario === 'create' || $this->item->name !== $this->name; |
111
|
|
|
}, |
112
|
|
|
], |
113
|
|
|
['children', RbacItemsValidator::class], |
114
|
|
|
['rule', RbacRuleExistsValidator::class], |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function getIsNewRecord() |
122
|
|
|
{ |
123
|
|
|
return $this->item === null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return Item |
128
|
|
|
*/ |
129
|
|
|
abstract public function getType(); |
130
|
|
|
} |
131
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..