-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathRule.php
169 lines (144 loc) · 3.98 KB
/
Rule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace dektrium\rbac\models;
use dektrium\rbac\components\DbManager;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\di\Instance;
/**
* Rule model.
*
* @author Dmitry Erofeev <[email protected]>
*/
class Rule extends Model
{
const SCENARIO_CREATE = 'create';
const SCENARIO_UPDATE = 'update';
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $class;
/**
* @var string|DbManager The auth manager component ID.
*/
public $authManager = 'authManager';
/**
* @var string
*/
private $_oldName;
/**
* @param string $oldName
*/
public function setOldName($oldName)
{
$this->_oldName = $oldName;
}
/**
* This method will set [[authManager]] to be the 'authManager' application component, if it is `null`.
*/
public function init()
{
parent::init();
$this->authManager = Instance::ensure($this->authManager, DbManager::className());
}
/**
* @return array
*/
public function scenarios()
{
return [
self::SCENARIO_CREATE => ['name', 'class'],
self::SCENARIO_UPDATE => ['name', 'class'],
];
}
/**
* @return array
*/
public function rules()
{
return [
[['name', 'class'], 'trim'],
[['name', 'class'], 'required'],
['name', 'match', 'pattern' => '/^[\w][\w-.:]+[\w]$/'],
['name', function () {
if ($this->name == $this->_oldName) {
return;
}
$rule = $this->authManager->getRule($this->name);
if ($rule instanceof \yii\rbac\Rule) {
$this->addError('name', \Yii::t('rbac', 'Name is already in use'));
}
}],
['class', function () {
if (!class_exists($this->class)) {
$this->addError('class', \Yii::t('rbac', 'Class "{0}" does not exist', $this->class));
} else {
try {
$class = '\yii\rbac\Rule';
$rule = \Yii::createObject($this->class);
if (!($rule instanceof $class)) {
$this->addError('class', \Yii::t('rbac', 'Rule class must extend "yii\rbac\Rule"'));
}
} catch (InvalidConfigException $e) {
$this->addError('class', \Yii::t('rbac', 'Rule class can not be instantiated'));
}
}
}],
];
}
/**
* Creates new auth rule.
*
* @return bool
* @throws InvalidConfigException
*/
public function create()
{
if ($this->scenario != self::SCENARIO_CREATE) {
return false;
}
if (!$this->validate()) {
return false;
}
$rule = \Yii::createObject([
'class' => $this->class,
'name' => $this->name,
]);
$this->authManager->add($rule);
$this->authManager->invalidateCache();
return true;
}
/**
* Updates existing auth rule.
*
* @return bool
* @throws InvalidConfigException
*/
public function update()
{
if ($this->scenario != self::SCENARIO_UPDATE) {
return false;
}
if (!$this->validate()) {
return false;
}
$rule = \Yii::createObject([
'class' => $this->class,
'name' => $this->name,
]);
$this->authManager->update($this->_oldName, $rule);
$this->authManager->invalidateCache();
return true;
}
}