1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Route; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
10
|
|
|
use Spatie\Permission\Contracts\Role as RoleContract; |
11
|
|
|
use Spatie\Permission\Contracts\Permission as PermissionContract; |
12
|
|
|
|
13
|
|
|
class PermissionServiceProvider extends ServiceProvider |
14
|
|
|
{ |
15
|
|
|
public function boot(PermissionRegistrar $permissionLoader, Filesystem $filesystem) |
16
|
|
|
{ |
17
|
|
|
if (isNotLumen()) { |
18
|
|
|
$this->publishes([ |
19
|
|
|
__DIR__.'/../config/permission.php' => config_path('permission.php'), |
20
|
|
|
], 'config'); |
21
|
|
|
|
22
|
|
|
if (! class_exists('CreatePermissionTables') && ! $this->migrationAlreadyPublished($filesystem)) { |
23
|
|
|
$timestamp = date('Y_m_d_His', time()); |
24
|
|
|
|
25
|
|
|
$this->publishes([ |
26
|
|
|
__DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_permission_tables.php", |
|
|
|
|
27
|
|
|
], 'migrations'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (app()->version() >= '5.5') { |
31
|
|
|
$this->registerMacroHelpers(); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ($this->app->runningInConsole()) { |
36
|
|
|
$this->commands([ |
37
|
|
|
Commands\CacheReset::class, |
38
|
|
|
Commands\CreateRole::class, |
39
|
|
|
Commands\CreatePermission::class, |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->registerModelBindings(); |
44
|
|
|
|
45
|
|
|
$permissionLoader->registerPermissions(); |
46
|
|
|
|
47
|
|
|
$this->app->singleton(PermissionRegistrar::class, function ($app) use ($permissionLoader) { |
|
|
|
|
48
|
|
|
return $permissionLoader; |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function register() |
53
|
|
|
{ |
54
|
|
|
if (isNotLumen()) { |
55
|
|
|
$this->mergeConfigFrom( |
56
|
|
|
__DIR__.'/../config/permission.php', |
57
|
|
|
'permission' |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->registerBladeExtensions(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function registerModelBindings() |
65
|
|
|
{ |
66
|
|
|
$config = $this->app->config['permission.models']; |
|
|
|
|
67
|
|
|
|
68
|
|
|
$this->app->bind(PermissionContract::class, $config['permission']); |
69
|
|
|
$this->app->bind(RoleContract::class, $config['role']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function registerBladeExtensions() |
73
|
|
|
{ |
74
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
75
|
|
View Code Duplication |
$bladeCompiler->directive('role', function ($arguments) { |
|
|
|
|
76
|
|
|
list($role, $guard) = explode(',', $arguments.','); |
77
|
|
|
|
78
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>"; |
79
|
|
|
}); |
80
|
|
View Code Duplication |
$bladeCompiler->directive('elserole', function ($arguments) { |
|
|
|
|
81
|
|
|
list($role, $guard) = explode(',', $arguments.','); |
82
|
|
|
|
83
|
|
|
return "<?php elseif(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>"; |
84
|
|
|
}); |
85
|
|
|
$bladeCompiler->directive('endrole', function () { |
86
|
|
|
return '<?php endif; ?>'; |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
View Code Duplication |
$bladeCompiler->directive('hasrole', function ($arguments) { |
|
|
|
|
90
|
|
|
list($role, $guard) = explode(',', $arguments.','); |
91
|
|
|
|
92
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>"; |
93
|
|
|
}); |
94
|
|
|
$bladeCompiler->directive('endhasrole', function () { |
95
|
|
|
return '<?php endif; ?>'; |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
View Code Duplication |
$bladeCompiler->directive('hasanyrole', function ($arguments) { |
|
|
|
|
99
|
|
|
list($roles, $guard) = explode(',', $arguments.','); |
100
|
|
|
|
101
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>"; |
102
|
|
|
}); |
103
|
|
|
$bladeCompiler->directive('endhasanyrole', function () { |
104
|
|
|
return '<?php endif; ?>'; |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
View Code Duplication |
$bladeCompiler->directive('hasallroles', function ($arguments) { |
|
|
|
|
108
|
|
|
list($roles, $guard) = explode(',', $arguments.','); |
109
|
|
|
|
110
|
|
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>"; |
111
|
|
|
}); |
112
|
|
|
$bladeCompiler->directive('endhasallroles', function () { |
113
|
|
|
return '<?php endif; ?>'; |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
View Code Duplication |
$bladeCompiler->directive('unlessrole', function ($arguments) { |
|
|
|
|
117
|
|
|
list($role, $guard) = explode(',', $arguments.','); |
118
|
|
|
|
119
|
|
|
return "<?php if(!auth({$guard})->check() || ! auth({$guard})->user()->hasRole({$role})): ?>"; |
120
|
|
|
}); |
121
|
|
|
$bladeCompiler->directive('endunlessrole', function () { |
122
|
|
|
return '<?php endif; ?>'; |
123
|
|
|
}); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function registerMacroHelpers() |
128
|
|
|
{ |
129
|
|
View Code Duplication |
Route::macro('role', function ($roles = []) { |
|
|
|
|
130
|
|
|
if (! is_array($roles)) { |
131
|
|
|
$roles = [$roles]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$roles = implode('|', $roles); |
135
|
|
|
|
136
|
|
|
$this->middleware("role:$roles"); |
|
|
|
|
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
}); |
140
|
|
|
|
141
|
|
View Code Duplication |
Route::macro('permission', function ($permissions = []) { |
|
|
|
|
142
|
|
|
if (! is_array($permissions)) { |
143
|
|
|
$permissions = [$permissions]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$permissions = implode('|', $permissions); |
147
|
|
|
|
148
|
|
|
$this->middleware("permission:$permissions"); |
|
|
|
|
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
}); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param Filesystem $filesystem |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
protected function migrationAlreadyPublished(Filesystem $filesystem): bool |
159
|
|
|
{ |
160
|
|
|
return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR) |
|
|
|
|
161
|
|
|
->flatMap(function ($path) use ($filesystem) { |
162
|
|
|
return $filesystem->glob($path.'*_create_permission_tables.php'); |
163
|
|
|
}) |
164
|
|
|
->count() > 0; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: