1 | <?php |
||
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 \Illuminate\Cache\CacheManager */ |
||
22 | protected $cacheManager; |
||
23 | |||
24 | /** @var string */ |
||
25 | protected $permissionClass; |
||
26 | |||
27 | /** @var string */ |
||
28 | protected $roleClass; |
||
29 | |||
30 | /** @var \Illuminate\Support\Collection */ |
||
31 | protected $permissions; |
||
32 | |||
33 | /** @var DateInterval|int */ |
||
34 | public static $cacheExpirationTime; |
||
35 | |||
36 | /** @var string */ |
||
37 | public static $cacheKey; |
||
38 | |||
39 | /** @var string */ |
||
40 | public static $cacheModelKey; |
||
41 | |||
42 | /** |
||
43 | * PermissionRegistrar constructor. |
||
44 | * |
||
45 | * @param \Illuminate\Contracts\Auth\Access\Gate $gate |
||
46 | * @param \Illuminate\Cache\CacheManager $cacheManager |
||
47 | */ |
||
48 | public function __construct(Gate $gate, CacheManager $cacheManager) |
||
49 | { |
||
50 | $this->gate = $gate; |
||
51 | $this->permissionClass = config('permission.models.permission'); |
||
52 | $this->roleClass = config('permission.models.role'); |
||
53 | |||
54 | $this->cacheManager = $cacheManager; |
||
55 | $this->initializeCache(); |
||
56 | } |
||
57 | |||
58 | protected function initializeCache() |
||
59 | { |
||
60 | self::$cacheExpirationTime = config('permission.cache.expiration_time', config('permission.cache_expiration_time')); |
||
61 | |||
62 | if (app()->version() <= '5.5') { |
||
63 | if (self::$cacheExpirationTime instanceof \DateInterval) { |
||
64 | $interval = self::$cacheExpirationTime; |
||
65 | self::$cacheExpirationTime = $interval->m * 30 * 60 * 24 + $interval->d * 60 * 24 + $interval->h * 60 + $interval->i; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | self::$cacheKey = config('permission.cache.key'); |
||
70 | self::$cacheModelKey = config('permission.cache.model_key'); |
||
71 | |||
72 | $this->cache = $this->getCacheStoreFromConfig(); |
||
73 | } |
||
74 | |||
75 | protected function getCacheStoreFromConfig(): \Illuminate\Contracts\Cache\Repository |
||
76 | { |
||
77 | // the 'default' fallback here is from the permission.php config file, where 'default' means to use config(cache.default) |
||
78 | $cacheDriver = config('permission.cache.store', 'default'); |
||
79 | |||
80 | // when 'default' is specified, no action is required since we already have the default instance |
||
81 | if ($cacheDriver === 'default') { |
||
82 | return $this->cacheManager->store(); |
||
83 | } |
||
84 | |||
85 | // if an undefined cache store is specified, fallback to 'array' which is Laravel's closest equiv to 'none' |
||
86 | if (! \array_key_exists($cacheDriver, config('cache.stores'))) { |
||
87 | $cacheDriver = 'array'; |
||
88 | } |
||
89 | |||
90 | return $this->cacheManager->store($cacheDriver); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Register the permission check method on the gate. |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function registerPermissions(): bool |
||
99 | { |
||
100 | $this->gate->before(function (Authorizable $user, string $ability) { |
||
101 | try { |
||
102 | if (method_exists($user, 'hasPermissionTo')) { |
||
103 | return $user->hasPermissionTo($ability) ?: null; |
||
|
|||
104 | } |
||
105 | } catch (PermissionDoesNotExist $e) { |
||
106 | } |
||
107 | }); |
||
108 | |||
109 | return true; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Flush the cache. |
||
114 | */ |
||
115 | public function forgetCachedPermissions() |
||
116 | { |
||
117 | $this->permissions = null; |
||
118 | |||
119 | return $this->cache->forget(self::$cacheKey); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Get the permissions based on the passed params. |
||
124 | * |
||
125 | * @param array $params |
||
126 | * |
||
127 | * @return \Illuminate\Support\Collection |
||
128 | */ |
||
129 | public function getPermissions(array $params = []): Collection |
||
130 | { |
||
131 | if ($this->permissions === null) { |
||
132 | $this->permissions = $this->cache->remember(self::$cacheKey, self::$cacheExpirationTime, function () { |
||
133 | return $this->getPermissionClass() |
||
134 | ->with('roles') |
||
135 | ->get(); |
||
136 | }); |
||
137 | } |
||
138 | |||
139 | $permissions = clone $this->permissions; |
||
140 | |||
141 | foreach ($params as $attr => $value) { |
||
142 | $permissions = $permissions->where($attr, $value); |
||
143 | } |
||
144 | |||
145 | return $permissions; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Get an instance of the permission class. |
||
150 | * |
||
151 | * @return \Spatie\Permission\Contracts\Permission |
||
152 | */ |
||
153 | public function getPermissionClass(): Permission |
||
154 | { |
||
155 | return app($this->permissionClass); |
||
156 | } |
||
157 | |||
158 | public function setPermissionClass($permissionClass) |
||
159 | { |
||
160 | $this->permissionClass = $permissionClass; |
||
161 | |||
162 | return $this; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Get an instance of the role class. |
||
167 | * |
||
168 | * @return \Spatie\Permission\Contracts\Role |
||
169 | */ |
||
170 | public function getRoleClass(): Role |
||
174 | |||
175 | /** |
||
176 | * Get the instance of the Cache Store. |
||
177 | * |
||
178 | * @return \Illuminate\Contracts\Cache\Store |
||
179 | */ |
||
180 | public function getCacheStore(): \Illuminate\Contracts\Cache\Store |
||
184 | } |
||
185 |
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.