1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link http://www.letyii.com/ |
5
|
|
|
* @copyright Copyright (c) 2014 Let.,ltd |
6
|
|
|
* @license https://github.com/letyii/cms/blob/master/LICENSE |
7
|
|
|
* @author Ngua Go <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace fangface\rbac; |
11
|
|
|
|
12
|
|
|
use Yii; |
13
|
|
|
use yii\rbac\DbManager; |
14
|
|
|
use yii\helpers\ArrayHelper; |
15
|
|
|
|
16
|
|
|
class RbacCached extends DbManager { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var integer Lifetime of cached data in seconds |
20
|
|
|
*/ |
21
|
|
|
public $cacheDuration = 3600; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string cache key name |
25
|
|
|
*/ |
26
|
|
|
public $cacheKeyName = 'RbacCached'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array php cache |
30
|
|
|
*/ |
31
|
|
|
protected $cachedData = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function checkAccess($userId, $permissionName, $params = []) { |
37
|
|
|
if (!empty($params)) |
38
|
|
|
return parent::checkAccess($userId, $permissionName, $params); |
39
|
|
|
|
40
|
|
|
$cacheKey = 'checkAccess:' . $userId . ':' . $permissionName; |
41
|
|
|
$cached = $this->getCache($cacheKey); |
42
|
|
|
if (empty($cached)) { |
43
|
|
|
$cached = parent::checkAccess($userId, $permissionName); |
44
|
|
|
$this->setCache($cacheKey, $cached); |
45
|
|
|
} |
46
|
|
|
return $cached; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
protected function checkAccessRecursive($user, $itemName, $params, $assignments) { |
53
|
|
|
$cacheKey = 'checkAccessRecursive:' . $user . ':' . $itemName; |
54
|
|
|
if (!empty($params)) |
55
|
|
|
$cacheKey .= ':' . current($params)->primaryKey; |
56
|
|
|
|
57
|
|
|
$cached = $this->getCache($cacheKey); |
58
|
|
|
if (empty($cached)) { |
59
|
|
|
$cached = parent::checkAccessRecursive($user, $itemName, $params, $assignments); |
60
|
|
|
$this->setCache($cacheKey, $cached); |
61
|
|
|
} |
62
|
|
|
return $cached; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
protected function getItem($name) { |
69
|
|
|
$cacheKey = 'Item:' . $name; |
70
|
|
|
$cached = $this->getCache($cacheKey); |
71
|
|
|
if (empty($cached)) { |
72
|
|
|
$cached = parent::getItem($name); |
73
|
|
|
$this->setCache($cacheKey, $cached); |
74
|
|
|
} |
75
|
|
|
return $cached; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function getAssignments($userId) { |
82
|
|
|
if (empty($userId)) |
83
|
|
|
return parent::getAssignments($userId); |
84
|
|
|
|
85
|
|
|
$cacheKey = 'Assignments:' . $userId; |
86
|
|
|
$cached = $this->getCache($cacheKey); |
87
|
|
|
if (empty($cached)) { |
88
|
|
|
$cached = parent::getAssignments($userId); |
89
|
|
|
$this->setCache($cacheKey, $cached); |
90
|
|
|
} |
91
|
|
|
return $cached; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set a value in cache |
96
|
|
|
* @param $key |
97
|
|
|
* @param $value |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
protected function setCache($key, $value) { |
101
|
|
|
$this->cachedData = $this->resolveCacheComponent()->get($this->cacheKeyName); |
102
|
|
|
if (empty($this->cachedData)) |
103
|
|
|
$this->cachedData = []; |
104
|
|
|
$this->cachedData[$key] = $value; |
105
|
|
|
return $this->resolveCacheComponent()->set($this->cacheKeyName, $this->cachedData, $this->cacheDuration); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get cached value |
110
|
|
|
* @param $key |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
protected function getCache($key) { |
114
|
|
|
$cached = ArrayHelper::getValue($this->cachedData, $key); |
115
|
|
|
if (!isset($cached)) { |
116
|
|
|
$cacheData = $this->resolveCacheComponent()->get($this->cacheKeyName); |
117
|
|
|
$cached = $this->cachedData[$key] = ArrayHelper::getValue($cacheData, $key); |
118
|
|
|
} |
119
|
|
|
return $cached; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get cached value |
124
|
|
|
* @param $key |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
public function deleteAllCache() { |
128
|
|
|
return $this->resolveCacheComponent()->delete($this->cacheKeyName); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns cache component configured as in cacheId |
133
|
|
|
* @return Cache |
134
|
|
|
*/ |
135
|
|
|
protected function resolveCacheComponent() { |
136
|
|
|
return $this->cache; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.