/
HybridAuthManager.php
400 lines (361 loc) · 13.7 KB
/
HybridAuthManager.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
* HybridAuthManager
*
* This class is a combination of CDbAuthManager and CPhpAuthManager:
*
* * The authorization hierarchy is stored in a flat PHP file
* * Authorization assignments are stored in the database
*
* This is useful if the authorization hierarchy is almost static
* and not very complex.
*
* You can manage the authorization hierarchy in data/auth.php. To
* not loose the comments there, you should avoid to call any method
* to create auth items or add child items - even though it's supported.
*
* It also allows caching of auth assignments.
*/
class HybridAuthManager extends CPhpAuthManager
{
/**
* @var string the ID of the {@link CDbConnection} application component. Defaults to 'db'.
* The database must have the tables as declared in "framework/web/auth/*.sql".
*/
public $connectionID = 'db';
/**
* @var string the ID of the cache application component. Defaults to 'cache'. Set to `null` to disable caching.
*/
public $cacheID = 'cache';
/**
* @var string the name of the table storing authorization item assignments. Defaults to 'auth_assignment'.
*/
public $assignmentTable='auth_assignment';
/**
* @var int|boolean number of seconds to cache auth assignments. Default is 0 which means, that
* authassignments are only cached during the current request. To completely disable caching
* set this property to false.
*/
public $assignmentCachingDuration = 0;
/**
* @var int number of seconds to cache the content of the auth hierarchy file. Default is 3600.
* Set to 0 to disable caching
*/
public $hierarchyCachingDuration = 3600;
/**
* @var array assignments indexed by user id
*/
protected $_assignments = array();
protected $_db;
protected $_loading=false;
/**
* Performs access check for the specified user.
* @param string $itemName the name of the operation that need access check
* @param mixed $userId the user ID. This should can be either an integer and a string representing
* the unique identifier of a user. See {@link IWebUser::getId}.
* @param array $params name-value pairs that would be passed to biz rules associated
* with the tasks and roles assigned to the user.
* @return boolean whether the operations can be performed by the user.
*/
public function checkAccess($itemName,$userId,$params=array())
{
$items = parent::getAuthItems();
if(!isset($items[$itemName])) {
return false;
}
$item = $items[$itemName];
$assignments = $this->getAuthAssignments($userId);
Yii::trace('Checking permission "'.$item->getName().'"','application.components.authmanager');
if($this->executeBizRule($item->getBizRule(),$params,$item->getData())) {
if(in_array($itemName,$this->defaultRoles)) {
return true;
}
if(isset($assignments[$itemName])) {
$assignment=$assignments[$itemName];
if($this->executeBizRule($assignment->getBizRule(),$params,$assignment->getData()))
return true;
}
// Even if the user was not assigned to the item directly, he could
// have been assigned to a parent item. $this->_children is private
// so we have to use this workaround:
foreach($items as $parentName => $item) {
if($this->hasItemChild($parentName, $itemName) && $this->checkAccess($parentName, $userId, $params)) {
return true;
}
}
}
return false;
}
/**
* Assigns an authorization item to a user.
* @param string $itemName the item name
* @param mixed $userId the user ID (see {@link IWebUser::getId})
* @param string $bizRule the business rule to be executed when {@link checkAccess} is called
* for this particular authorization item.
* @param mixed $data additional data associated with this assignment
* @return CAuthAssignment the authorization assignment information.
* @throws CException if the item does not exist or if the item has already been assigned to the user
*/
public function assign($itemName,$userId,$bizRule=null,$data=null)
{
$this->getDbConnection()
->createCommand()
->insert($this->assignmentTable, array(
'itemname' => $itemName,
'userid' => $userId,
'bizrule' => $bizRule,
'data' => serialize($data)
));
$this->flushAssignmentCache($userId);
return new CAuthAssignment($this,$itemName,$userId,$bizRule,$data);
}
/**
* Revokes an authorization assignment from a user.
* @param string $itemName the item name
* @param mixed $userId the user ID (see {@link IWebUser::getId})
* @return boolean whether removal is successful
*/
public function revoke($itemName,$userId)
{
$rows = $this->getDbConnection()
->createCommand()
->delete($this->assignmentTable, 'itemname=:itemname AND userid=:userid', array(
':itemname' => $itemName,
':userid' => $userId
));
return $rows > 0;
}
/**
* Returns a value indicating whether the item has been assigned to the user.
* @param string $itemName the item name
* @param mixed $userId the user ID (see {@link IWebUser::getId})
* @return boolean whether the item has been assigned to the user.
*/
public function isAssigned($itemName,$userId)
{
$value = $this->getDbConnection()
->createCommand()
->select('itemname')
->from($this->assignmentTable)
->where('itemname=:itemname AND userid=:userid', array(
':itemname' => $itemName,
':userid' => $userId))
->queryScalar();
return $value!==false;
}
/**
* Returns the item assignment information.
* @param string $itemName the item name
* @param mixed $userId the user ID (see {@link IWebUser::getId})
* @return CAuthAssignment the item assignment information. Null is returned if
* the item is not assigned to the user.
*/
public function getAuthAssignment($itemName,$userId)
{
$row = $this->getDbConnection()
->createCommand()
->select()
->from($this->assignmentTable)
->where('itemname=:itemname AND userid=:userid', array(
':itemname' => $itemName,
':userid' => $userId))
->queryRow();
if($row!==false) {
if(($data = @unserialize($row['data']))===false) {
$data = null;
}
return new CAuthAssignment($this,$row['itemname'],$row['userid'],$row['bizrule'],$data);
} else {
return null;
}
}
/**
* Returns the item assignments for the specified user.
* @param mixed $userId the user ID (see {@link IWebUser::getId})
* @return array the item assignment information for the user. An empty array will be
* returned if there is no item assigned to the user.
*/
public function getAuthAssignments($userId)
{
$useCache = $this->assignmentCachingDuration!==false;
if($useCache) {
if(isset($this->_assignments[$userId])) {
return $this->_assignments[$userId];
} else {
$cacheKey = $this->getAssignmentCacheKey($userId);
$cache = Yii::app()->getComponent($this->cacheID);
if(!$cache || ($assignments = $cache->get($cacheKey))!==false) {
return $this->_assignments[$userId] = $assignments;
}
}
}
$rows = $this->getDbConnection()
->createCommand()
->select()
->from($this->assignmentTable)
->where('userid=:userid', array(':userid'=>$userId))
->queryAll();
$assignments = array();
foreach($rows as $row)
{
if(($data = @unserialize($row['data']))===false) {
$data = null;
}
$assignments[$row['itemname']] = new CAuthAssignment($this,$row['itemname'],$row['userid'],$row['bizrule'],$data);
}
if($useCache) {
$this->_assignments[$userId] = $assignments;
if($cache && $this->assignmentCachingDuration!==0) {
$cache->set($cacheKey, $assignments);
}
}
return $assignments;
}
/**
* @param mixed $userId the user ID (see {@link IWebUser::getId})
* @return string the cache key used to store auth assignments for this user
*/
public function getAssignmentCacheKey($userId)
{
return '__authassignments__'.$userId.'_'.Yii::app()->id;
}
/**
* Saves the changes to an authorization assignment.
* @param CAuthAssignment $assignment the assignment that has been changed.
*/
public function saveAuthAssignment($assignment)
{
$userId = $assignment->getUserId();
$this->getDbConnection()
->createCommand()
->update($this->assignmentTable,
array(
'bizrule' => $assignment->getBizRule(),
'data' => serialize($assignment->getData()),
),
'itemname=:itemname AND userid=:userid',
array(
'itemname' => $assignment->getItemName(),
'userid' => $userId,
)
);
$this->flushAssignmentCache($userId);
}
/**
* Returns the authorization items of the specific type and user.
* @param integer $type the item type (0: operation, 1: task, 2: role). Defaults to null,
* meaning returning all items regardless of their type.
* @param mixed $userId the user ID. Defaults to null, meaning returning all items even if
* they are not assigned to a user.
* @return array the authorization items of the specific type.
*/
public function getAuthItems($type=null,$userId=null)
{
$authItems = parent::getAuthItems();
if($type===null && $userId===null) {
return $authItems;
}
$items = array();
if($userId===null) {
foreach($authItems as $name=>$item) {
if($item->getType()==$type) {
$items[$name] = $item;
}
}
} else {
foreach($this->getAuthAssignments($userId) as $assignment) {
$name = $assignment->getItemName();
if(isset($authItems[$name]) && ($type===null || $authItems[$name]->getType()==$type)) {
$items[$name]=$authItems[$name];
}
}
}
return $items;
}
/**
* Removes all authorization assignments.
*/
public function clearAuthAssignments() {
// Hack: prevent auth assignments to be cleared during init
if(!$this->_loading) {
$this->getDbConnection()->createCommand()->delete($this->assignmentTable);
}
}
/**
* Flush assignments for specified user from cache
*
* @param mixed $userId the user ID (see {@link IWebUser::getId})
*/
public function flushAssignmentCache($userId)
{
if($this->assignmentCachingDuration===false) {
return;
}
if($cache = Yii::app()->getComponent($this->cacheID)) {
$cache->delete($this->getAssignmentCacheKey($userId));
}
}
/**
* @return CDbConnection the DB connection instance
* @throws CException if {@link connectionID} does not point to a valid application component.
*/
protected function getDbConnection()
{
if($this->_db===null) {
$this->_db =Yii::app()->getComponent($this->connectionID);
if(!($this->_db instanceof CDbConnection)) {
throw new CException("CDbAuthManager.connectionID '{$this->connectionID}' is invalid.");
}
}
return $this->_db;
}
/**
* Override parent class to prevent the call to clearAll()
*
* @return void
*/
public function load()
{
$this->_loading=true;
parent::load();
$this->_loading=false;
}
/**
* Loads the authorization data from a PHP script file.
* File content is cached
* @param string $file the file path.
* @return array the authorization data
* @see saveToFile
*/
protected function loadFromFile($file)
{
$app = Yii::app();
$cache = ($this->cacheID && $app->hasComponent($this->cacheID)) ? $app->getComponent($this->cacheID) : null;
if($cache) {
$key = '__authfile_'.Yii::app()->id;
if(($content = $cache->get($key))!==false) {
return $content;
}
}
if(!is_file($file)) {
return array();
}
$content = require($file);
// Make some keys optional, to not bloat auth.php
foreach($content as $name => $value) {
if(!isset($value['description'])) {
$content[$name]['description'] = null;
}
if(!isset($value['bizRule'])) {
$content[$name]['bizRule'] = null;
}
if(!isset($value['data'])) {
$content[$name]['data'] = null;
}
}
if($cache && $this->hierarchyCachingDuration!==0) {
$cache->set($key,$content, $this->hierarchyCachingDuration, new CFileCacheDependency($file));
}
return $content;
}
}