|
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
|
|
|
public function boot() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->publishes([ |
|
18
|
|
|
__DIR__.'/../resources/config/laravel-permission.php' => $this->app->configPath().'/'.'laravel-permission.php', |
|
19
|
|
|
], 'config'); |
|
20
|
|
|
|
|
21
|
|
|
if (!class_exists('CreatePermissionTables')) { |
|
22
|
|
|
// Publish the migration |
|
23
|
|
|
$timestamp = date('Y_m_d_His', time()); |
|
24
|
|
|
$this->publishes([ |
|
25
|
|
|
__DIR__.'/../resources/migrations/create_permission_tables.php.stub' => $this->app->databasePath().'/migrations/'.$timestamp.'_create_permission_tables.php', |
|
|
|
|
|
|
26
|
|
|
], 'migrations'); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Finish configuring the application. |
|
32
|
|
|
* |
|
33
|
|
|
* @param PermissionRegistrar $permissionLoader |
|
34
|
|
|
*/ |
|
35
|
|
|
public function booted(PermissionRegistrar $permissionLoader) |
|
36
|
|
|
{ |
|
37
|
|
|
$permissionLoader->registerPermissions(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Register the application services. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function register() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->mergeConfigFrom(__DIR__.'/../resources/config/laravel-permission.php', 'laravel-permission'); |
|
46
|
|
|
|
|
47
|
|
|
$this->registerModelBindings(); |
|
48
|
|
|
|
|
49
|
|
|
$this->registerBladeExtensions(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Bind the Permission and Role model into the IoC. |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function registerModelBindings() |
|
56
|
|
|
{ |
|
57
|
|
|
$config = $this->app->config['laravel-permission.models']; |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$this->app->bind(PermissionContract::class, $config['permission']); |
|
60
|
|
|
$this->app->bind(RoleContract::class, $config['role']); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Register the blade extensions. |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function registerBladeExtensions() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
|
69
|
|
|
$bladeCompiler->directive('role', function ($role) { |
|
70
|
|
|
return "<?php if(auth()->check() && auth()->user()->hasRole({$role})): ?>"; |
|
71
|
|
|
}); |
|
72
|
|
|
$bladeCompiler->directive('endrole', function () { |
|
73
|
|
|
return '<?php endif; ?>'; |
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
$bladeCompiler->directive('hasrole', function ($role) { |
|
77
|
|
|
return "<?php if(auth()->check() && auth()->user()->hasRole({$role})): ?>"; |
|
78
|
|
|
}); |
|
79
|
|
|
$bladeCompiler->directive('endhasrole', function () { |
|
80
|
|
|
return '<?php endif; ?>'; |
|
81
|
|
|
}); |
|
82
|
|
|
|
|
83
|
|
|
$bladeCompiler->directive('hasanyrole', function ($roles) { |
|
84
|
|
|
return "<?php if(auth()->check() && auth()->user()->hasAnyRole({$roles})): ?>"; |
|
85
|
|
|
}); |
|
86
|
|
|
$bladeCompiler->directive('endhasanyrole', function () { |
|
87
|
|
|
return '<?php endif; ?>'; |
|
88
|
|
|
}); |
|
89
|
|
|
|
|
90
|
|
|
$bladeCompiler->directive('hasallroles', function ($roles) { |
|
91
|
|
|
return "<?php if(auth()->check() && auth()->user()->hasAllRoles({$roles})): ?>"; |
|
92
|
|
|
}); |
|
93
|
|
|
$bladeCompiler->directive('endhasallroles', function () { |
|
94
|
|
|
return '<?php endif; ?>'; |
|
95
|
|
|
}); |
|
96
|
|
|
}); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
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.