1 | <?php |
||
11 | class PermissionRegistrar |
||
12 | { |
||
13 | /** |
||
14 | * @var Gate |
||
15 | */ |
||
16 | protected $gate; |
||
17 | |||
18 | /** |
||
19 | * @var Repository |
||
20 | */ |
||
21 | protected $cache; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $cacheKey = 'spatie.permission.cache'; |
||
27 | |||
28 | /** |
||
29 | * @param Gate $gate |
||
30 | * @param Repository $cache |
||
31 | */ |
||
32 | public function __construct(Gate $gate, Repository $cache) |
||
37 | |||
38 | /** |
||
39 | * Register the permissions. |
||
40 | * |
||
41 | * @return bool |
||
42 | */ |
||
43 | public function registerPermissions() |
||
44 | { |
||
45 | try { |
||
46 | $this->getPermissions()->map(function ($permission) { |
||
47 | $this->gate->define($permission->name, function ($user) use ($permission) { |
||
48 | return $user->hasPermissionTo($permission); |
||
49 | }); |
||
50 | }); |
||
51 | |||
52 | return true; |
||
53 | } catch (Exception $exception) { |
||
54 | if ($this->shouldLogException()) { |
||
55 | Log::alert( |
||
56 | "Could not register permissions because {$exception->getMessage()}".PHP_EOL |
||
57 | .$exception->getTraceAsString()); |
||
58 | } |
||
59 | |||
60 | return false; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Forget the cached permissions. |
||
66 | */ |
||
67 | public function forgetCachedPermissions() |
||
71 | |||
72 | /** |
||
73 | * Get the current permissions. |
||
74 | * |
||
75 | * @return \Illuminate\Database\Eloquent\Collection |
||
76 | */ |
||
77 | protected function getPermissions() |
||
83 | |||
84 | /** |
||
85 | * @return bool |
||
86 | */ |
||
87 | protected function shouldLogException() |
||
97 | } |
||
98 |