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
|
|
|
$this->registerModelBindings(); |
27
|
|
|
|
28
|
|
|
$permissionLoader->registerPermissions(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function register() |
32
|
|
|
{ |
33
|
|
|
$this->mergeConfigFrom( |
34
|
|
|
__DIR__.'/../config/permission.php', |
35
|
|
|
'permission' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$this->registerBladeExtensions(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function registerModelBindings() |
42
|
|
|
{ |
43
|
|
|
$config = $this->app->config['permission.models']; |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->app->bind(PermissionContract::class, $config['permission']); |
46
|
|
|
$this->app->bind(RoleContract::class, $config['role']); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function registerBladeExtensions() |
50
|
|
|
{ |
51
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
52
|
|
View Code Duplication |
$bladeCompiler->directive('role', function ($arguments) { |
|
|
|
|
53
|
|
|
list($role, $guard) = explode(',', $arguments.','); |
54
|
|
|
|
55
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>"; |
56
|
|
|
}); |
57
|
|
|
$bladeCompiler->directive('endrole', function () { |
58
|
|
|
return '<?php endif; ?>'; |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
View Code Duplication |
$bladeCompiler->directive('hasrole', function ($arguments) { |
|
|
|
|
62
|
|
|
list($role, $guard) = explode(',', $arguments.','); |
63
|
|
|
|
64
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>"; |
65
|
|
|
}); |
66
|
|
|
$bladeCompiler->directive('endhasrole', function () { |
67
|
|
|
return '<?php endif; ?>'; |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
View Code Duplication |
$bladeCompiler->directive('hasanyrole', function ($arguments) { |
|
|
|
|
71
|
|
|
list($roles, $guard) = explode(',', $arguments.','); |
72
|
|
|
$roles = $this->convertPipeToArray($roles); |
73
|
|
|
|
74
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>"; |
75
|
|
|
}); |
76
|
|
|
$bladeCompiler->directive('endhasanyrole', function () { |
77
|
|
|
return '<?php endif; ?>'; |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
View Code Duplication |
$bladeCompiler->directive('hasallroles', function ($arguments) { |
|
|
|
|
81
|
|
|
list($roles, $guard) = explode(',', $arguments.','); |
82
|
|
|
$roles = $this->convertPipeToArray($roles); |
83
|
|
|
|
84
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>"; |
85
|
|
|
}); |
86
|
|
|
$bladeCompiler->directive('endhasallroles', function () { |
87
|
|
|
return '<?php endif; ?>'; |
88
|
|
|
}); |
89
|
|
|
}); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function convertPipeToArray(string $pipeString) |
93
|
|
|
{ |
94
|
|
|
$pipeString = trim($pipeString); |
95
|
|
|
if (strlen($pipeString) > 2) { |
96
|
|
|
$char = substr($pipeString, 0, 1); |
97
|
|
|
if (in_array($char, ["'", '"'])) { |
98
|
|
|
$endChar = substr($pipeString, -1, 1); |
99
|
|
|
if ($char == $endChar) { |
100
|
|
|
$pipeString = '['.implode($char.','.$char, explode('|', $pipeString)).']'; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $pipeString; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.