1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Acl; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\Access\Gate as GateContract; |
6
|
|
|
use Illuminate\Database\QueryException; |
7
|
|
|
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
10
|
|
|
use Yajra\Acl\Models\Permission; |
11
|
|
|
|
12
|
|
|
class AclServiceProvider extends ServiceProvider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Register any application authentication / authorization services. |
16
|
|
|
* |
17
|
|
|
* @param \Illuminate\Contracts\Auth\Access\Gate $gate |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function boot(GateContract $gate) |
21
|
|
|
{ |
22
|
|
|
$this->publishConfig(); |
23
|
|
|
$this->publishMigrations(); |
24
|
|
|
$this->registerPolicies($gate); |
25
|
|
|
$this->registerPermissions($gate); |
26
|
|
|
$this->registerCacheListener(); |
27
|
|
|
$this->registerBladeDirectives(); |
28
|
|
|
$this->loadTranslations(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Publish package config file. |
33
|
|
|
*/ |
34
|
|
|
protected function publishConfig() |
35
|
|
|
{ |
36
|
|
|
$this->publishes([ |
37
|
|
|
__DIR__ . '/../config/acl.php' => config_path('acl.php'), |
38
|
|
|
], 'laravel-acl'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Publish package migration files. |
43
|
|
|
*/ |
44
|
|
|
protected function publishMigrations() |
45
|
|
|
{ |
46
|
|
|
$this->publishes([ |
47
|
|
|
__DIR__ . '/../migrations/' => database_path('migrations'), |
48
|
|
|
], 'laravel-acl'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Load package language files. |
53
|
|
|
*/ |
54
|
|
|
protected function loadTranslations() |
55
|
|
|
{ |
56
|
|
|
$this->loadTranslationsFrom(__DIR__. '/../lang', 'laravel-acl'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Register defined permissions from database. |
61
|
|
|
* |
62
|
|
|
* @param \Illuminate\Contracts\Auth\Access\Gate $gate |
63
|
|
|
*/ |
64
|
|
|
protected function registerPermissions(GateContract $gate) |
65
|
|
|
{ |
66
|
|
|
try { |
67
|
|
|
foreach ($this->getPermissions() as $permission) { |
68
|
|
|
$ability = $permission->slug; |
69
|
|
|
$policy = function ($user) use ($permission) { |
70
|
|
|
return $user->hasRole($permission->roles); |
71
|
|
|
}; |
72
|
|
|
|
73
|
|
|
if (Str::contains($permission->slug, '@')) { |
74
|
|
|
$policy = $permission->slug; |
75
|
|
|
$ability = $permission->name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$gate->define($ability, $policy); |
79
|
|
|
} |
80
|
|
|
} catch (QueryException $e) { |
81
|
|
|
// \\_(",)_// |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get lists of permissions. |
87
|
|
|
* |
88
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
89
|
|
|
*/ |
90
|
|
|
protected function getPermissions() |
91
|
|
|
{ |
92
|
|
|
return $this->app['cache.store']->rememberForever('permissions.policies', function () { |
93
|
|
|
return Permission::with('roles')->get(); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Register ACL models cache listener. |
99
|
|
|
*/ |
100
|
|
|
protected function registerCacheListener() |
101
|
|
|
{ |
102
|
|
|
Permission::saved(function () { |
103
|
|
|
$this->app['cache.store']->forget('permissions.policies'); |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
Permission::deleted(function () { |
107
|
|
|
$this->app['cache.store']->forget('permissions.policies'); |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Register custom blade directives. |
113
|
|
|
*/ |
114
|
|
|
protected function registerBladeDirectives() |
115
|
|
|
{ |
116
|
|
|
/** @var BladeCompiler $blade */ |
117
|
|
|
$blade = $this->app['blade.compiler']; |
118
|
|
|
$blade->directive('canAtLeast', function ($expression) { |
119
|
|
|
return "<?php echo app('Yajra\\Acl\\CanAtLeastDirective')->handle{$expression}; ?>"; |
120
|
|
|
}); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Register providers. |
125
|
|
|
*/ |
126
|
|
|
public function register() |
127
|
|
|
{ |
128
|
|
|
$this->app->singleton(CanAtLeastDirective::class, CanAtLeastDirective::class); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|