1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
7
|
|
|
use Spatie\Permission\Contracts\Permission as PermissionContract; |
8
|
|
|
use Spatie\Permission\Contracts\Role as RoleContract; |
9
|
|
|
|
10
|
|
|
class PermissionServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Bootstrap the application services. |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
public function boot() |
17
|
|
|
{ |
18
|
|
|
$this->publishes([ |
19
|
|
|
__DIR__.'/../resources/config/laravel-permission.php' => $this->app->configPath().'/'.'laravel-permission.php', |
20
|
|
|
], 'config'); |
21
|
|
|
|
22
|
|
|
if (!class_exists('CreatePermissionTables')) { |
23
|
|
|
// Publish the migration |
24
|
|
|
$timestamp = date('Y_m_d_His', time()); |
25
|
|
|
$this->publishes([ |
26
|
|
|
__DIR__.'/../resources/migrations/create_permission_tables.php.stub' => $this->app->databasePath().'/migrations/'.$timestamp.'_create_permission_tables.php', |
|
|
|
|
27
|
|
|
], 'migrations'); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Finish configuring the application. |
33
|
|
|
* |
34
|
|
|
* @param PermissionRegistrar $permissionLoader |
35
|
|
|
*/ |
36
|
|
|
public function booted(PermissionRegistrar $permissionLoader) |
37
|
|
|
{ |
38
|
|
|
$permissionLoader->registerPermissions(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Register the application services. |
43
|
|
|
*/ |
44
|
|
|
public function register() |
45
|
|
|
{ |
46
|
|
|
$this->mergeConfigFrom(__DIR__.'/../resources/config/laravel-permission.php', 'laravel-permission'); |
47
|
|
|
|
48
|
|
|
$this->registerModelBindings(); |
49
|
|
|
|
50
|
|
|
$this->registerBladeExtensions(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Bind the Permission and Role model into the IoC. |
55
|
|
|
*/ |
56
|
|
|
protected function registerModelBindings() |
57
|
|
|
{ |
58
|
|
|
$config = $this->app->config['laravel-permission.models']; |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->app->bind(PermissionContract::class, $config['permission']); |
61
|
|
|
$this->app->bind(RoleContract::class, $config['role']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Register the blade extensions. |
66
|
|
|
*/ |
67
|
|
|
protected function registerBladeExtensions() |
68
|
|
|
{ |
69
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
70
|
|
|
$bladeCompiler->directive('role', function ($role) { |
71
|
|
|
return "<?php if(auth()->check() && auth()->user()->hasRole({$role})): ?>"; |
72
|
|
|
}); |
73
|
|
|
$bladeCompiler->directive('endrole', function () { |
74
|
|
|
return '<?php endif; ?>'; |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
$bladeCompiler->directive('hasrole', function ($role) { |
78
|
|
|
return "<?php if(auth()->check() && auth()->user()->hasRole({$role})): ?>"; |
79
|
|
|
}); |
80
|
|
|
$bladeCompiler->directive('endhasrole', function () { |
81
|
|
|
return '<?php endif; ?>'; |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
$bladeCompiler->directive('hasanyrole', function ($roles) { |
85
|
|
|
return "<?php if(auth()->check() && auth()->user()->hasAnyRole({$roles})): ?>"; |
86
|
|
|
}); |
87
|
|
|
$bladeCompiler->directive('endhasanyrole', function () { |
88
|
|
|
return '<?php endif; ?>'; |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
$bladeCompiler->directive('hasallroles', function ($roles) { |
92
|
|
|
return "<?php if(auth()->check() && auth()->user()->hasAllRoles({$roles})): ?>"; |
93
|
|
|
}); |
94
|
|
|
$bladeCompiler->directive('endhasallroles', function () { |
95
|
|
|
return '<?php endif; ?>'; |
96
|
|
|
}); |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.