|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\Acl; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Yajra\Acl\Models\Permission; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Contracts\Cache\Repository; |
|
9
|
|
|
use Illuminate\Contracts\Auth\Access\Gate as GateContract; |
|
10
|
|
|
|
|
11
|
|
|
class GateRegistrar |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var GateContract |
|
15
|
|
|
*/ |
|
16
|
|
|
private $gate; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Repository |
|
20
|
|
|
*/ |
|
21
|
|
|
private $cache; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* GateRegistrar constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param GateContract $gate |
|
27
|
|
|
* @param Repository $cache |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(GateContract $gate, Repository $cache) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->gate = $gate; |
|
32
|
|
|
$this->cache = $cache; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Handle permission gate registration. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function register() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->getPermissions()->each(function ($permission) { |
|
41
|
|
|
$ability = $permission->slug; |
|
42
|
|
|
$policy = function ($user) use ($permission) { |
|
43
|
|
|
return $user->hasRole($permission->roles); |
|
44
|
|
|
}; |
|
45
|
|
|
|
|
46
|
|
|
if (Str::contains($permission->slug, '@')) { |
|
47
|
|
|
$policy = $permission->slug; |
|
48
|
|
|
$ability = $permission->name; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->gate->define($ability, $policy); |
|
52
|
|
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get all permissions. |
|
57
|
|
|
* |
|
58
|
|
|
* @return Collection |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getPermissions() |
|
61
|
|
|
{ |
|
62
|
|
|
$key = config('acl.cache.key', 'permissions.policies'); |
|
63
|
|
|
try { |
|
64
|
|
|
if (config('acl.cache.enabled', true)) { |
|
65
|
|
|
return $this->cache->rememberForever($key, function () { |
|
66
|
|
|
return $this->getPermissionClass()->with('roles')->get(); |
|
|
|
|
|
|
67
|
|
|
}); |
|
68
|
|
|
} else { |
|
69
|
|
|
return $this->getPermissionClass()->with('roles')->get(); |
|
70
|
|
|
} |
|
71
|
|
|
} catch (\Exception $exception) { |
|
72
|
|
|
$this->cache->forget($key); |
|
73
|
|
|
|
|
74
|
|
|
return new Collection; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Permission |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getPermissionClass() |
|
82
|
|
|
{ |
|
83
|
|
|
return resolve(config('acl.permission', Permission::class)); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: