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
|
|
View Code Duplication |
$bladeCompiler->directive('hasanyrole', function ($arguments) { |
|
|
|
|
78
|
|
|
list($roles, $guard) = explode(',', $arguments.','); |
79
|
|
|
$roles = $this->convertPipeToArray($roles); |
80
|
|
|
|
81
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>"; |
82
|
|
|
}); |
83
|
|
|
$bladeCompiler->directive('endhasanyrole', function () { |
84
|
|
|
return '<?php endif; ?>'; |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
View Code Duplication |
$bladeCompiler->directive('hasallroles', function ($arguments) { |
|
|
|
|
88
|
|
|
list($roles, $guard) = explode(',', $arguments.','); |
89
|
|
|
$roles = $this->convertPipeToArray($roles); |
90
|
|
|
|
91
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>"; |
92
|
|
|
}); |
93
|
|
|
$bladeCompiler->directive('endhasallroles', function () { |
94
|
|
|
return '<?php endif; ?>'; |
95
|
|
|
}); |
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function convertPipeToArray(string $pipeString) |
100
|
|
|
{ |
101
|
|
|
$pipeString = trim($pipeString); |
102
|
|
|
|
103
|
|
|
if (strlen($pipeString) <= 2) { |
104
|
|
|
return $pipeString; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$quoteCharacter = substr($pipeString, 0, 1); |
108
|
|
|
|
109
|
|
|
if (! in_array($quoteCharacter, ["'", '"'])) { |
110
|
|
|
return $pipeString; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$endCharacter = substr($quoteCharacter, -1, 1); |
114
|
|
|
|
115
|
|
|
if ($quoteCharacter !== $endCharacter) { |
116
|
|
|
return $pipeString; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$roleString = '['.implode($quoteCharacter.','.$quoteCharacter, explode('|', $pipeString)).']'; |
120
|
|
|
|
121
|
|
|
return $roleString; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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.