8ec727c1
曹明
初始化代码提交
|
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
|
<?php
namespace mdm\admin\models;
use Yii;
use yii\rbac\Rule;
use mdm\admin\components\Configs;
/**
* BizRule
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class BizRule extends \yii\base\Model
{
/**
* @var string name of the rule
*/
public $name;
/**
* @var integer UNIX timestamp representing the rule creation time
*/
public $createdAt;
/**
* @var integer UNIX timestamp representing the rule updating time
*/
public $updatedAt;
/**
* @var string Rule classname.
*/
public $className;
/**
* @var Rule
*/
private $_item;
/**
* Initilaize object
* @param \yii\rbac\Rule $item
* @param array $config
*/
public function __construct($item, $config = [])
{
$this->_item = $item;
if ($item !== null) {
$this->name = $item->name;
$this->className = get_class($item);
}
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'className'], 'required'],
[['className'], 'string'],
[['className'], 'classExists']
];
}
/**
* Validate class exists
*/
public function classExists()
{
if (!class_exists($this->className)) {
$message = Yii::t('rbac-admin', "Unknown class '{class}'", ['class' => $this->className]);
$this->addError('className', $message);
return;
}
if (!is_subclass_of($this->className, Rule::className())) {
$message = Yii::t('rbac-admin', "'{class}' must extend from 'yii\rbac\Rule' or its child class", [
'class' => $this->className]);
$this->addError('className', $message);
}
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'name' => Yii::t('rbac-admin', 'Name'),
'className' => Yii::t('rbac-admin', 'Class Name'),
];
}
/**
* Check if new record.
* @return boolean
*/
public function getIsNewRecord()
{
return $this->_item === null;
}
/**
* Find model by id
* @param type $id
* @return null|static
*/
public static function find($id)
{
$item = Configs::authManager()->getRule($id);
if ($item !== null) {
return new static($item);
}
return null;
}
/**
* Save model to authManager
* @return boolean
*/
public function save()
{
if ($this->validate()) {
$manager = Configs::authManager();
$class = $this->className;
if ($this->_item === null) {
$this->_item = new $class();
$isNew = true;
} else {
$isNew = false;
$oldName = $this->_item->name;
}
$this->_item->name = $this->name;
if ($isNew) {
$manager->add($this->_item);
} else {
$manager->update($oldName, $this->_item);
}
return true;
} else {
return false;
}
}
/**
* Get item
* @return Item
*/
public function getItem()
{
return $this->_item;
}
}
|