1 | <?php |
||
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 | */ |
||
110 | public function clearClassPermissions() |
||
111 | { |
||
112 | $this->permissions = null; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get the permissions based on the passed params. |
||
117 | * |
||
118 | * @param array $params |
||
119 | * |
||
120 | * @return \Illuminate\Support\Collection |
||
121 | */ |
||
122 | public function getPermissions(array $params = []): Collection |
||
123 | { |
||
124 | if ($this->permissions === null) { |
||
125 | $this->permissions = $this->cache->remember(self::$cacheKey, self::$cacheExpirationTime, function () { |
||
126 | return $this->getPermissionClass() |
||
127 | ->with('roles') |
||
128 | ->get(); |
||
129 | }); |
||
130 | } |
||
131 | |||
132 | $permissions = clone $this->permissions; |
||
133 | |||
134 | foreach ($params as $attr => $value) { |
||
135 | $permissions = $permissions->where($attr, $value); |
||
136 | } |
||
137 | |||
138 | return $permissions; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Get an instance of the permission class. |
||
143 | * |
||
144 | * @return \Spatie\Permission\Contracts\Permission |
||
145 | */ |
||
146 | public function getPermissionClass(): Permission |
||
147 | { |
||
148 | return app($this->permissionClass); |
||
149 | } |
||
150 | |||
151 | public function setPermissionClass($permissionClass) |
||
152 | { |
||
153 | $this->permissionClass = $permissionClass; |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Get an instance of the role class. |
||
160 | * |
||
161 | * @return \Spatie\Permission\Contracts\Role |
||
162 | */ |
||
163 | public function getRoleClass(): Role |
||
167 | |||
168 | /** |
||
169 | * Get the instance of the Cache Store. |
||
170 | * |
||
171 | * @return \Illuminate\Contracts\Cache\Store |
||
172 | */ |
||
173 | public function getCacheStore(): \Illuminate\Contracts\Cache\Store |
||
177 | } |
||
178 |
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.