AccessServiceProvider.php
2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace App\Access\Provider;
use App\Access\Service\Access;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
/**
* Class AccessServiceProvider.
*/
class AccessServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Package boot method.
*/
public function boot()
{
$this->registerBladeExtensions();
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerAccess();
$this->registerFacade();
}
/**
* Register the application bindings.
*
* @return void
*/
private function registerAccess()
{
$this->app->bind('access', function ($app) {
return new Access($app);
});
}
/**
* Register the vault facade without the user having to add it to the app.php file.
*
* @return void
*/
public function registerFacade()
{
$this->app->booting(function () {
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Access', \App\Access\Service\Facades\Access::class);
});
}
/**
* Register the blade extender to use new blade sections.
*/
protected function registerBladeExtensions()
{
/*
* Role based blade extensions
* Accepts either string of Role Name or Role ID
*/
Blade::directive('role', function ($role) {
return "<?php if (access()->hasRole({$role})): ?>";
});
/*
* Accepts array of names or id's
*/
Blade::directive('roles', function ($roles) {
return "<?php if (access()->hasRoles({$roles})): ?>";
});
Blade::directive('needsroles', function ($roles) {
return '<?php if (access()->hasRoles('.$roles.', true)): ?>';
});
/*
* Permission based blade extensions
* Accepts wither string of Permission Name or Permission ID
*/
Blade::directive('permission', function ($permission) {
return "<?php if (access()->allow({$permission})): ?>";
});
/*
* Accepts array of names or id's
*/
Blade::directive('permissions', function ($permissions) {
return "<?php if (access()->allowMultiple({$permissions})): ?>";
});
Blade::directive('needspermissions', function ($permissions) {
return '<?php if (access()->allowMultiple('.$permissions.', true)): ?>';
});
/*
* Generic if closer to not interfere with built in blade
*/
Blade::directive('endauth', function () {
return '<?php endif; ?>';
});
}
}