1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SlFomin\Roles; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
|
7
|
|
|
class RolesServiceProvider extends ServiceProvider |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Bootstrap any application services. |
11
|
|
|
* |
12
|
|
|
* @return void |
13
|
|
|
*/ |
14
|
|
|
public function boot() |
15
|
|
|
{ |
16
|
|
|
$this->publishes([ |
17
|
|
|
__DIR__ . '/../config/roles.php' => config_path('roles.php'), |
18
|
|
|
], 'config'); |
19
|
|
|
|
20
|
|
|
$this->publishes([ |
21
|
|
|
__DIR__ . '/../migrations/' => base_path('/database/migrations'), |
22
|
|
|
], 'migrations'); |
23
|
|
|
|
24
|
|
|
$this->registerBladeExtensions(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Register any application services. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function register() |
33
|
|
|
{ |
34
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../config/roles.php', 'roles'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Register Blade extensions. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
protected function registerBladeExtensions() |
43
|
|
|
{ |
44
|
|
|
$blade = $this->app['view']->getEngineResolver()->resolve('blade')->getCompiler(); |
45
|
|
|
|
46
|
|
|
$blade->directive('role', function ($expression) { |
47
|
|
|
return "<?php if (Auth::check() && Auth::user()->hasRole({$expression})): ?>"; |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
$blade->directive('endrole', function () { |
51
|
|
|
return '<?php endif; ?>'; |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
$blade->directive('permission', function ($expression) { |
55
|
|
|
return "<?php if (Auth::check() && Auth::user()->hasPermission({$expression})): ?>"; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$blade->directive('endpermission', function () { |
59
|
|
|
return '<?php endif; ?>'; |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
$blade->directive('allowed', function ($expression) { |
63
|
|
|
return "<?php if (Auth::check() && Auth::user()->allowed({$expression})): ?>"; |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
$blade->directive('endallowed', function () { |
67
|
|
|
return '<?php endif; ?>'; |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|