forked from crisu83/yii-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCachedDbAuthManager.php
101 lines (90 loc) · 3.16 KB
/
CachedDbAuthManager.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
<?php
/**
* CachedDbAuthManager class file.
* @author Christoffer Niska <[email protected]>
* @copyright Copyright © Christoffer Niska 2012-
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @package auth.components
*/
Yii::import('auth.components.ICachedAuthManager');
/**
* Caching layer for CDbAuthManager that allows for caching access checks.
*/
class CachedDbAuthManager extends CDbAuthManager implements ICachedAuthManager
{
const CACHE_KEY_PREFIX = 'Auth.CachedDbAuthManager.';
/**
* @var integer the time in seconds that the messages can remain valid in cache.
* Defaults to 0, meaning the caching is disabled.
*/
public $cachingDuration = 0;
/**
* @var string the ID of the cache application component that is used to cache the messages.
* Defaults to 'cache' which refers to the primary cache application component.
* Set this property to false if you want to disable caching.
*/
public $cacheID = 'cache';
/**
* Performs access check for the specified user.
* @param string $itemName the name of the operation that need access check.
* @param integer $userId the user id.
* @param array $params name-value pairs that would be passed to biz rules associated
* with the tasks and roles assigned to the user.
* @param boolean $allowCaching whether to allow caching the result of access check.
* @return boolean whether the operations can be performed by the user.
*/
public function checkAccess($itemName, $userId, $params = array(), $allowCaching = true)
{
$cacheKey = $this->resolveCacheKey($itemName, $userId);
$key = serialize($params);
if ($allowCaching && ($cache = $this->getCache()) !== null)
{
if (($data = $cache->get($cacheKey)) !== false)
{
$data = unserialize($data);
if (isset($data[$key]))
return $data[$key];
}
}
else
$data = array();
$result = $data[$key] = parent::checkAccess($itemName, $userId, $params);
if (isset($cache))
$cache->set($cacheKey, serialize($data), $this->cachingDuration);
return $result;
}
/**
* Flushes the access cache for the specified user.
* @param string $itemName the name of the operation that need access check.
* @param integer $userId the user id.
* @return boolean whether access was flushed.
*/
public function flushAccess($itemName, $userId)
{
if (($cache = $this->getCache()) !== null)
{
$cacheKey = $this->resolveCacheKey($itemName, $userId);
return $cache->delete($cacheKey);
}
return false;
}
/**
* Returns the key to use when caching.
* @param string $itemName the name of the operation that need access check.
* @param array $params name-value pairs that would be passed to biz rules associated
* with the tasks and roles assigned to the user.
* @return string the key.
*/
protected function resolveCacheKey($itemName, $userId)
{
return self::CACHE_KEY_PREFIX . 'checkAccess.' . $itemName . '.' . $userId;
}
/**
* Returns the caching component for this component.
* @return CCache|null the caching component.
*/
protected function getCache()
{
return $this->cachingDuration > 0 && $this->cacheID !== false ? Yii::app()->getComponent($this->cacheID) : null;
}
}