|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maklad\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 Maklad\Permission\Contracts\PermissionInterface as Permission; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class PermissionRegistrar |
|
13
|
|
|
* @package Maklad\Permission |
|
14
|
|
|
*/ |
|
15
|
|
|
class PermissionRegistrar |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var \Illuminate\Contracts\Auth\Access\Gate */ |
|
18
|
|
|
protected $gate; |
|
19
|
|
|
|
|
20
|
|
|
/** @var \Illuminate\Contracts\Cache\Repository */ |
|
21
|
|
|
protected $cache; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $cacheKey = 'maklad.permission.cache'; |
|
25
|
|
|
|
|
26
|
123 |
|
/** @var string */ |
|
27
|
|
|
protected $permissionClass; |
|
28
|
123 |
|
|
|
29
|
123 |
|
/** @var string */ |
|
30
|
123 |
|
protected $roleClass; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* PermissionRegistrar constructor. |
|
34
|
|
|
* @param Gate $gate |
|
35
|
123 |
|
* @param Repository $cache |
|
36
|
7 |
|
*/ |
|
37
|
123 |
|
public function __construct(Gate $gate, Repository $cache) |
|
38
|
123 |
|
{ |
|
39
|
|
|
$this->gate = $gate; |
|
40
|
123 |
|
$this->cache = $cache; |
|
41
|
|
|
$this->permissionClass = config('permission.models.permission'); |
|
42
|
|
|
$this->roleClass = config('permission.models.role'); |
|
43
|
123 |
|
} |
|
44
|
|
|
|
|
45
|
123 |
|
/** |
|
46
|
123 |
|
* Register Permissions |
|
47
|
|
|
* |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
123 |
|
public function registerPermissions(): bool |
|
51
|
123 |
|
{ |
|
52
|
123 |
|
$this->getPermissions()->map(function (Permission $permission) { |
|
53
|
|
|
$this->gate->define($permission->name, function (Authorizable $user) use ($permission) { |
|
|
|
|
|
|
54
|
|
|
return $user->hasPermissionTo($permission) ?: null; |
|
55
|
|
|
}); |
|
56
|
|
|
}); |
|
57
|
|
|
|
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Forget cached permission |
|
63
|
|
|
*/ |
|
64
|
|
|
public function forgetCachedPermissions() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->cache->forget($this->cacheKey); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get Permissions |
|
71
|
|
|
* |
|
72
|
|
|
* @return Collection |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getPermissions(): Collection |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->cache->remember($this->cacheKey, config('permission.cache_expiration_time'), function () { |
|
77
|
|
|
return $this->getPermissionClass()->with('roles')->get(); |
|
|
|
|
|
|
78
|
|
|
}); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get Permission class |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Illuminate\Foundation\Application|mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getPermissionClass() |
|
87
|
|
|
{ |
|
88
|
|
|
return app($this->permissionClass); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get Role class |
|
93
|
|
|
* |
|
94
|
|
|
* @return \Illuminate\Foundation\Application|mixed |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getRoleClass() |
|
97
|
|
|
{ |
|
98
|
|
|
return app($this->roleClass); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|