1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Spatie\Permission\Contracts\Role; |
7
|
|
|
use Illuminate\Contracts\Auth\Access\Gate; |
8
|
|
|
use Illuminate\Contracts\Cache\Repository; |
9
|
|
|
use Spatie\Permission\Contracts\Permission; |
10
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable; |
11
|
|
|
use Spatie\Permission\Exceptions\PermissionDoesNotExist; |
12
|
|
|
|
13
|
|
|
class PermissionRegistrar |
14
|
|
|
{ |
15
|
|
|
/** @var \Illuminate\Contracts\Auth\Access\Gate */ |
16
|
|
|
protected $gate; |
17
|
|
|
|
18
|
|
|
/** @var \Illuminate\Contracts\Cache\Repository */ |
19
|
|
|
protected $cache; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $permissionClass; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $roleClass; |
26
|
|
|
|
27
|
|
|
/** @var int */ |
28
|
|
|
public static $cacheExpirationTime; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
public static $cacheKey; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
public static $cacheModelKey; |
35
|
|
|
|
36
|
|
|
/** @var bool */ |
37
|
|
|
public static $cacheIsTaggable = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* PermissionRegistrar constructor. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Contracts\Auth\Access\Gate $gate |
43
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Gate $gate, Repository $cache) |
46
|
|
|
{ |
47
|
|
|
$this->gate = $gate; |
48
|
|
|
$this->permissionClass = config('permission.models.permission'); |
49
|
|
|
$this->roleClass = config('permission.models.role'); |
50
|
|
|
|
51
|
|
|
self::$cacheExpirationTime = config('permission.cache.expiration_time', config('permission.cache_expiration_time')); |
52
|
|
|
self::$cacheKey = config('permission.cache.key'); |
53
|
|
|
self::$cacheModelKey = config('permission.cache.model_key'); |
54
|
|
|
self::$cacheIsTaggable = ($cache->getStore() instanceof \Illuminate\Cache\TaggableStore); |
55
|
|
|
|
56
|
|
|
$this->cache = self::$cacheIsTaggable ? $cache->tags(self::$cacheKey) : $cache; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Register the permission check method on the gate. |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function registerPermissions(): bool |
65
|
|
|
{ |
66
|
|
|
$this->gate->before(function (Authorizable $user, string $ability) { |
67
|
|
|
try { |
68
|
|
|
if (method_exists($user, 'hasPermissionTo')) { |
69
|
|
|
return $user->hasPermissionTo($ability) ?: null; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} catch (PermissionDoesNotExist $e) { |
|
|
|
|
72
|
|
|
} |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Flush the cache. |
80
|
|
|
*/ |
81
|
|
|
public function forgetCachedPermissions() |
82
|
|
|
{ |
83
|
|
|
self::$cacheIsTaggable ? $this->cache->flush() : $this->cache->forget(self::$cacheKey); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the permissions based on the passed params. |
88
|
|
|
* |
89
|
|
|
* @param array $params |
90
|
|
|
* |
91
|
|
|
* @return \Illuminate\Support\Collection |
92
|
|
|
*/ |
93
|
|
|
public function getPermissions(array $params = []): Collection |
94
|
|
|
{ |
95
|
|
|
$permissions = $this->cache->remember($this->getKey($params), self::$cacheExpirationTime, |
96
|
|
|
function () use ($params) { |
97
|
|
|
return $this->getPermissionClass() |
|
|
|
|
98
|
|
|
->when(($params && self::$cacheIsTaggable), function ($query) use ($params) { |
|
|
|
|
99
|
|
|
return $query->where($params); |
100
|
|
|
}) |
101
|
|
|
->with('roles') |
102
|
|
|
->get(); |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
if (! self::$cacheIsTaggable) { |
106
|
|
|
foreach ($params as $attr => $value) { |
107
|
|
|
$permissions = $permissions->where($attr, $value); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $permissions; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the key for caching. |
116
|
|
|
* |
117
|
|
|
* @param $params |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getKey(array $params): string |
122
|
|
|
{ |
123
|
|
|
if ($params && self::$cacheIsTaggable) { |
|
|
|
|
124
|
|
|
return self::$cacheKey.'.'.implode('.', array_values($params)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return self::$cacheKey; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get an instance of the permission class. |
132
|
|
|
* |
133
|
|
|
* @return \Spatie\Permission\Contracts\Permission |
134
|
|
|
*/ |
135
|
|
|
public function getPermissionClass(): Permission |
136
|
|
|
{ |
137
|
|
|
return app($this->permissionClass); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get an instance of the role class. |
142
|
|
|
* |
143
|
|
|
* @return \Spatie\Permission\Contracts\Role |
144
|
|
|
*/ |
145
|
|
|
public function getRoleClass(): Role |
146
|
|
|
{ |
147
|
|
|
return app($this->roleClass); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the instance of the Cache Store |
152
|
|
|
* |
153
|
|
|
* @return \Illuminate\Contracts\Cache\Store |
154
|
|
|
*/ |
155
|
|
|
public function getCacheStore(): \Illuminate\Contracts\Cache\Store |
156
|
|
|
{ |
157
|
|
|
return $this->cache->getStore(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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.