1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
7
|
|
|
use Spatie\Permission\Contracts\Role as RoleContract; |
8
|
|
|
use Spatie\Permission\Contracts\Permission as PermissionContract; |
9
|
|
|
|
10
|
|
|
class PermissionServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
public function boot(PermissionRegistrar $permissionLoader) |
13
|
|
|
{ |
14
|
|
|
$this->publishes([ |
15
|
|
|
__DIR__.'/../config/permission.php' => $this->app->configPath().'/permission.php', |
16
|
|
|
], 'config'); |
17
|
|
|
|
18
|
|
|
if (! class_exists('CreatePermissionTables')) { |
19
|
|
|
$timestamp = date('Y_m_d_His', time()); |
20
|
|
|
|
21
|
|
|
$this->publishes([ |
22
|
|
|
__DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_permission_tables.php", |
|
|
|
|
23
|
|
|
], 'migrations'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if ($this->app->runningInConsole()) { |
27
|
|
|
$this->commands([ |
28
|
|
|
Commands\CreateRole::class, |
29
|
|
|
Commands\CreatePermission::class, |
30
|
|
|
]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->registerModelBindings(); |
34
|
|
|
|
35
|
|
|
$permissionLoader->registerPermissions(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function register() |
39
|
|
|
{ |
40
|
|
|
$this->mergeConfigFrom( |
41
|
|
|
__DIR__.'/../config/permission.php', |
42
|
|
|
'permission' |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$this->registerBladeExtensions(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function registerModelBindings() |
49
|
|
|
{ |
50
|
|
|
$config = $this->app->config['permission.models']; |
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->app->bind(PermissionContract::class, $config['permission']); |
53
|
|
|
$this->app->bind(RoleContract::class, $config['role']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function registerBladeExtensions() |
57
|
|
|
{ |
58
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
59
|
|
View Code Duplication |
$bladeCompiler->directive('role', function ($arguments) { |
|
|
|
|
60
|
|
|
list($role, $guard) = explode(',', $arguments.','); |
61
|
|
|
|
62
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>"; |
63
|
|
|
}); |
64
|
|
|
$bladeCompiler->directive('endrole', function () { |
65
|
|
|
return '<?php endif; ?>'; |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
View Code Duplication |
$bladeCompiler->directive('hasrole', function ($arguments) { |
|
|
|
|
69
|
|
|
list($role, $guard) = explode(',', $arguments.','); |
70
|
|
|
|
71
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>"; |
72
|
|
|
}); |
73
|
|
|
$bladeCompiler->directive('endhasrole', function () { |
74
|
|
|
return '<?php endif; ?>'; |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
$bladeCompiler->directive('hasanyrole', function ($arguments) { |
78
|
|
|
list($roles, $guard) = explode(',', $arguments.','); |
79
|
|
|
|
80
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>"; |
81
|
|
|
}); |
82
|
|
|
$bladeCompiler->directive('endhasanyrole', function () { |
83
|
|
|
return '<?php endif; ?>'; |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
$bladeCompiler->directive('hasallroles', function ($arguments) { |
87
|
|
|
list($roles, $guard) = explode(',', $arguments.','); |
88
|
|
|
|
89
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>"; |
90
|
|
|
}); |
91
|
|
|
$bladeCompiler->directive('endhasallroles', function () { |
92
|
|
|
return '<?php endif; ?>'; |
93
|
|
|
}); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.