1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable; |
6
|
|
|
use Illuminate\Contracts\Auth\Access\Gate; |
7
|
|
|
use Illuminate\Contracts\Cache\Repository; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Spatie\Permission\Exceptions\PermissionDoesNotExist; |
10
|
|
|
|
11
|
|
|
class PermissionRegistrar |
12
|
|
|
{ |
13
|
|
|
/** @var \Illuminate\Contracts\Auth\Access\Gate */ |
14
|
|
|
protected $gate; |
15
|
|
|
|
16
|
|
|
/** @var \Illuminate\Contracts\Cache\Repository */ |
17
|
|
|
protected $cache; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $cacheKey; |
21
|
|
|
/** @var string */ |
22
|
|
|
protected $permissionClass; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $roleClass; |
26
|
|
|
/** @var boolean */ |
27
|
|
|
public static $cacheIsTaggable = false; |
28
|
|
|
|
29
|
|
|
public function __construct(Gate $gate, Repository $cache) |
30
|
|
|
{ |
31
|
|
|
$this->gate = $gate; |
32
|
|
|
$this->cache = $cache; |
33
|
|
|
$this->permissionClass = config('permission.models.permission'); |
34
|
|
|
$this->roleClass = config('permission.models.role'); |
35
|
|
|
$this->cacheKey = config('permission.cache.key'); |
36
|
|
|
|
37
|
|
|
self::$cacheIsTaggable = ($cache->getStore() instanceOf \Illuminate\Cache\TaggableStore); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function registerPermissions(): bool |
41
|
|
|
{ |
42
|
|
|
$this->gate->before(function (Authorizable $user, string $ability) { |
43
|
|
|
try { |
44
|
|
|
if (method_exists($user, 'hasPermissionTo')) { |
45
|
|
|
return $user->hasPermissionTo($ability) ?: null; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
} catch (PermissionDoesNotExist $e) { |
|
|
|
|
48
|
|
|
} |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function forgetCachedPermissions() |
55
|
|
|
{ |
56
|
|
|
if (self::$cacheIsTaggable) { |
57
|
|
|
$this->cache->tags($this->cacheKey)->flush(); |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->cache->forget($this->cacheKey); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getPermissions($params = null): Collection |
65
|
|
|
{ |
66
|
|
|
$cache = self::$cacheIsTaggable ? $this->cache->tags($this->cacheKey) : $this->cache; |
67
|
|
|
|
68
|
|
|
if (self::$cacheIsTaggable) { |
69
|
|
|
return $cache->remember($this->getCacheKey($params), config('permission.cache.expiration_time'), |
70
|
|
|
function () use ($params) { |
71
|
|
|
return $this->getPermissionClass()->when($params, function ($query) use ($params) { |
72
|
|
|
return $query->where($params); |
73
|
|
|
})->with('roles')->get(); |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$permissions = $cache->remember($this->getCacheKey($params), config('permission.cache.expiration_time'), |
78
|
|
|
function () use ($params) { |
79
|
|
|
return $this->getPermissionClass()->with('roles')->get(); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
foreach ($params as $attr => $value) { |
83
|
|
|
$permissions = $permissions->where($attr, $value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $permissions; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getCacheKey($params) |
90
|
|
|
{ |
91
|
|
|
if ($params && self::$cacheIsTaggable) { |
92
|
|
|
return $this->cacheKey . ($params ? '.' . implode('.', array_values($params)) : ''); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->cacheKey; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getPermissionClass() |
99
|
|
|
{ |
100
|
|
|
return app($this->permissionClass); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getRoleClass() |
104
|
|
|
{ |
105
|
|
|
return app($this->roleClass); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.