1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission; |
4
|
|
|
|
5
|
|
|
use Illuminate\Cache\CacheManager; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\Permission\Contracts\Role; |
8
|
|
|
use Illuminate\Contracts\Auth\Access\Gate; |
9
|
|
|
use Spatie\Permission\Contracts\Permission; |
10
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable; |
11
|
|
|
|
12
|
|
|
class PermissionRegistrar |
13
|
|
|
{ |
14
|
|
|
/** @var \Illuminate\Contracts\Cache\Repository */ |
15
|
|
|
protected $cache; |
16
|
|
|
|
17
|
|
|
/** @var \Illuminate\Cache\CacheManager */ |
18
|
|
|
protected $cacheManager; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $permissionClass; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected $roleClass; |
25
|
|
|
|
26
|
|
|
/** @var \Illuminate\Support\Collection */ |
27
|
|
|
protected $permissions; |
28
|
|
|
|
29
|
|
|
/** @var \DateInterval|int */ |
30
|
|
|
public static $cacheExpirationTime; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
public static $cacheKey; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
public static $cacheModelKey; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* PermissionRegistrar constructor. |
40
|
|
|
* |
41
|
|
|
* @param \Illuminate\Cache\CacheManager $cacheManager |
42
|
|
|
*/ |
43
|
|
|
public function __construct(CacheManager $cacheManager) |
44
|
|
|
{ |
45
|
|
|
$this->permissionClass = config('permission.models.permission'); |
46
|
|
|
$this->roleClass = config('permission.models.role'); |
47
|
|
|
|
48
|
|
|
$this->cacheManager = $cacheManager; |
49
|
|
|
$this->initializeCache(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function initializeCache() |
53
|
|
|
{ |
54
|
|
|
self::$cacheExpirationTime = config('permission.cache.expiration_time', config('permission.cache_expiration_time')); |
55
|
|
|
|
56
|
|
|
self::$cacheKey = config('permission.cache.key'); |
57
|
|
|
self::$cacheModelKey = config('permission.cache.model_key'); |
58
|
|
|
|
59
|
|
|
$this->cache = $this->getCacheStoreFromConfig(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function getCacheStoreFromConfig(): \Illuminate\Contracts\Cache\Repository |
63
|
|
|
{ |
64
|
|
|
// the 'default' fallback here is from the permission.php config file, where 'default' means to use config(cache.default) |
65
|
|
|
$cacheDriver = config('permission.cache.store', 'default'); |
66
|
|
|
|
67
|
|
|
// when 'default' is specified, no action is required since we already have the default instance |
68
|
|
|
if ($cacheDriver === 'default') { |
69
|
|
|
return $this->cacheManager->store(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// if an undefined cache store is specified, fallback to 'array' which is Laravel's closest equiv to 'none' |
73
|
|
|
if (! \array_key_exists($cacheDriver, config('cache.stores'))) { |
74
|
|
|
$cacheDriver = 'array'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->cacheManager->store($cacheDriver); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Register the permission check method on the gate. |
82
|
|
|
* We resolve the Gate fresh here, for benefit of long-running instances. |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function registerPermissions(): bool |
87
|
|
|
{ |
88
|
|
|
app(Gate::class)->before(function (Authorizable $user, string $ability) { |
89
|
|
|
if (method_exists($user, 'checkPermissionTo')) { |
90
|
|
|
return $user->checkPermissionTo($ability) ?: null; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Flush the cache. |
99
|
|
|
*/ |
100
|
|
|
public function forgetCachedPermissions() |
101
|
|
|
{ |
102
|
|
|
$this->permissions = null; |
103
|
|
|
|
104
|
|
|
return $this->cache->forget(self::$cacheKey); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Clear class permissions. |
109
|
|
|
* This is only intended to be called by the PermissionServiceProvider on boot, |
110
|
|
|
* so that long-running instances like Swoole don't keep old data in memory. |
111
|
|
|
*/ |
112
|
|
|
public function clearClassPermissions() |
113
|
|
|
{ |
114
|
|
|
$this->permissions = null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the permissions based on the passed params. |
119
|
|
|
* |
120
|
|
|
* @param array $params |
121
|
|
|
* |
122
|
|
|
* @return \Illuminate\Support\Collection |
123
|
|
|
*/ |
124
|
|
|
public function getPermissions(array $params = []): Collection |
125
|
|
|
{ |
126
|
|
|
if ($this->permissions === null) { |
127
|
|
|
$this->permissions = $this->cache->remember(self::$cacheKey, self::$cacheExpirationTime, function () { |
128
|
|
|
return $this->getPermissionClass() |
129
|
|
|
->with('roles') |
130
|
|
|
->get(); |
131
|
|
|
}); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$permissions = clone $this->permissions; |
135
|
|
|
|
136
|
|
|
foreach ($params as $attr => $value) { |
137
|
|
|
$permissions = $permissions->where($attr, $value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $permissions; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get an instance of the permission class. |
145
|
|
|
* |
146
|
|
|
* @return \Spatie\Permission\Contracts\Permission |
147
|
|
|
*/ |
148
|
|
|
public function getPermissionClass(): Permission |
149
|
|
|
{ |
150
|
|
|
return app($this->permissionClass); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setPermissionClass($permissionClass) |
154
|
|
|
{ |
155
|
|
|
$this->permissionClass = $permissionClass; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get an instance of the role class. |
162
|
|
|
* |
163
|
|
|
* @return \Spatie\Permission\Contracts\Role |
164
|
|
|
*/ |
165
|
|
|
public function getRoleClass(): Role |
166
|
|
|
{ |
167
|
|
|
return app($this->roleClass); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the instance of the Cache Store. |
172
|
|
|
* |
173
|
|
|
* @return \Illuminate\Contracts\Cache\Store |
174
|
|
|
*/ |
175
|
|
|
public function getCacheStore(): \Illuminate\Contracts\Cache\Store |
176
|
|
|
{ |
177
|
|
|
return $this->cache->getStore(); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.