1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaPermission; |
4
|
|
|
|
5
|
|
|
use BBSLab\NovaPermission\Console\Commands\GenerateResourcePermissions; |
6
|
|
|
use BBSLab\NovaPermission\Contracts\CanOverridePermission; |
7
|
|
|
use BBSLab\NovaPermission\Http\Middleware\Authorize; |
8
|
|
|
use BBSLab\NovaPermission\Resources\Permission; |
9
|
|
|
use BBSLab\NovaPermission\Resources\Role; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Filesystem\Filesystem; |
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
use Illuminate\Support\Facades\Gate; |
14
|
|
|
use Illuminate\Support\Facades\Route; |
15
|
|
|
use Illuminate\Support\ServiceProvider; |
16
|
|
|
use Laravel\Nova\Nova; |
17
|
|
|
use Spatie\Permission\PermissionRegistrar; |
18
|
|
|
|
19
|
|
|
class NovaPermissionServiceProvider extends ServiceProvider |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Bootstrap any application services. |
23
|
|
|
* |
24
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
public function boot(Filesystem $filesystem) |
28
|
|
|
{ |
29
|
|
|
$this->publishes([ |
30
|
|
|
__DIR__.'/../config/permission.php' => config_path('permission.php'), |
31
|
|
|
__DIR__.'/../config/nova-permission.php' => config_path('nova-permission.php'), |
32
|
|
|
], 'config'); |
33
|
|
|
|
34
|
|
|
$this->publishes([ |
35
|
|
|
__DIR__.'/../resources/lang' => resource_path('lang/vendor/nova-permission'), |
36
|
|
|
], 'translations'); |
37
|
|
|
|
38
|
|
|
$this->publishMigrations($filesystem); |
39
|
|
|
|
40
|
|
|
$this->loadViewsFrom(__DIR__.'/../resources/views', 'nova-permission'); |
41
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'nova-permission'); |
42
|
|
|
|
43
|
|
|
$this->app->booted(function () { |
44
|
|
|
$this->routes(); |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
if ($this->app->runningInConsole()) { |
48
|
|
|
$this->commands([ |
49
|
|
|
GenerateResourcePermissions::class, |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
Gate::after(function ($user, $ability) { |
54
|
|
|
if ($user instanceof CanOverridePermission) { |
55
|
|
|
return $user->canOverridePermission(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return false; |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
$this->registerResources(app(PermissionRegistrar::class)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function registerResources(PermissionRegistrar $registrar) |
65
|
|
|
{ |
66
|
|
|
Model::unguard(true); |
67
|
|
|
Permission::$model = get_class($registrar->getPermissionClass()); |
68
|
|
|
Role::$model = get_class($registrar->getRoleClass()); |
69
|
|
|
Model::unguard(false); |
70
|
|
|
|
71
|
|
|
Nova::resources([ |
72
|
|
|
Permission::class, |
73
|
|
|
Role::class, |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function publishMigrations(Filesystem $filesystem): void |
78
|
|
|
{ |
79
|
|
|
$migrations = collect([ |
80
|
|
|
'add_authorizable_and_group_to_permissions_table.php', |
81
|
|
|
'add_override_permission_to_roles_table.php', |
82
|
|
|
])->mapWithKeys(function ($file) use ($filesystem) { |
83
|
|
|
$key = __DIR__."/../database/migrations/{$file}.stub"; |
84
|
|
|
|
85
|
|
|
return [$key => $this->getMigrationFileName($filesystem, $file)]; |
86
|
|
|
})->toArray(); |
87
|
|
|
|
88
|
|
|
$this->publishes($migrations, 'migrations'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Register the tool's routes. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
protected function routes() |
97
|
|
|
{ |
98
|
|
|
if ($this->app->routesAreCached()) { |
|
|
|
|
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
Route::middleware(['nova', Authorize::class]) |
103
|
|
|
->prefix('nova-vendor/nova-permission') |
104
|
|
|
->group(__DIR__.'/../routes/api.php'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Register any application services. |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function register() |
113
|
|
|
{ |
114
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/permission.php', 'permission'); |
115
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/nova-permission.php', 'nova-permission'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns existing migration file if found, else uses the current timestamp. |
120
|
|
|
* |
121
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
122
|
|
|
* @param string $file |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
protected function getMigrationFileName(Filesystem $filesystem, string $file): string |
126
|
|
|
{ |
127
|
|
|
$timestamp = date('Y_m_d_His'); |
128
|
|
|
|
129
|
|
|
return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR) |
130
|
|
|
->flatMap(function ($path) use ($filesystem, $file) { |
131
|
|
|
return $filesystem->glob("{$path}*_{$file}"); |
132
|
|
|
})->push("{$this->app->databasePath()}/migrations/{$timestamp}_{$file}") |
133
|
|
|
->first(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.