PR CI / ledger (pull_request) Successful in 4s
PR CI / flow (pull_request) Successful in 4s
PR CI / test (stable) (pull_request) Successful in 45s
PR CI / ui (pull_request) Successful in 48s
PR CI / test (lowest) (pull_request) Successful in 50s
PR CI / security (pull_request) Successful in 54s
PR CI / pr-ci (pull_request) Successful in 2s
PR CI / release-automerge (pull_request) Successful in 3s
110 lines
4.3 KiB
PHP
110 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager;
|
|
|
|
use Illuminate\Cache\RateLimiting\Limit;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use IriksIT\Manager\Support\ModuleRegistry;
|
|
|
|
class ManagerServiceProvider extends ServiceProvider
|
|
{
|
|
/** Only a super admin: the groups decide what everyone else may do. Not a permission of a group. */
|
|
public const MANAGE_GROUPS = 'groups.manage';
|
|
|
|
public function register(): void
|
|
{
|
|
$this->mergeConfigFrom(__DIR__.'/../config/manager.php', 'manager');
|
|
|
|
$this->app->singleton(Support\MediaUsage::class);
|
|
$this->app->singleton(Support\AuditTrail::class);
|
|
$this->app->singleton(Modules\Posts\PostLabels::class);
|
|
|
|
$this->app->singleton(ModuleRegistry::class, fn ($app) => new ModuleRegistry(
|
|
$app['config']->get('manager.available', []),
|
|
$app['config']->get('manager.modules', []),
|
|
));
|
|
}
|
|
|
|
public function boot(ModuleRegistry $modules): void
|
|
{
|
|
$modules->validate();
|
|
|
|
$this->publishes([__DIR__.'/../config/manager.php' => config_path('manager.php')], 'manager-config');
|
|
$this->loadViewsFrom(__DIR__.'/../resources/views', 'manager');
|
|
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
|
|
|
|
$this->app['router']->aliasMiddleware('manager.auth', Http\Middleware\Authenticate::class);
|
|
$this->app['router']->aliasMiddleware('manager.shell', Http\Middleware\ServeShell::class);
|
|
RateLimiter::for('manager-login', fn (Request $request) => Limit::perMinute(5)->by(strtolower((string) $request->input('email')).'|'.$request->ip()));
|
|
|
|
foreach ($modules->active() as $module) {
|
|
if ($module->migrations() !== null) {
|
|
$this->loadMigrationsFrom($module->migrations());
|
|
}
|
|
|
|
if ($this->app->runningInConsole() && $module->commands() !== []) {
|
|
$this->commands($module->commands());
|
|
}
|
|
|
|
$module->boot();
|
|
}
|
|
|
|
if ($this->app->runningInConsole()) {
|
|
$this->commands([Console\InstallCommand::class, Console\DoctorCommand::class, Console\MakeThemeCommand::class]);
|
|
}
|
|
|
|
$this->registerGates($modules);
|
|
$this->registerRoutes($modules);
|
|
}
|
|
|
|
/**
|
|
* One gate per permission, and no Gate::before: a package must never answer for abilities
|
|
* of the host app. A super admin passes inside hasManagerPermission, for these gates only.
|
|
*/
|
|
private function registerGates(ModuleRegistry $modules): void
|
|
{
|
|
foreach ($modules->permissionValues() as $permission) {
|
|
Gate::define($permission, fn ($user) => method_exists($user, 'hasManagerPermission') && $user->hasManagerPermission($permission));
|
|
}
|
|
|
|
Gate::define(self::MANAGE_GROUPS, fn ($user) => method_exists($user, 'isManagerSuperAdmin') && $user->isManagerSuperAdmin());
|
|
}
|
|
|
|
private function registerRoutes(ModuleRegistry $modules): void
|
|
{
|
|
$screens = config('manager.ui') === 'package';
|
|
|
|
if ($screens) {
|
|
// Outside `auth`, but behind the bans of the modules.
|
|
Route::middleware(['web', ...$modules->middleware('before'), 'manager.shell'])
|
|
->prefix(config('manager.path'))
|
|
->name('manager.')
|
|
->group(fn () => $this->loadRoutesFrom(__DIR__.'/../routes/guest.php'));
|
|
}
|
|
|
|
Route::middleware([...$modules->middleware('before'), ...config('manager.middleware'), ...($screens ? ['manager.shell'] : []), ...$modules->middleware('after')])
|
|
->prefix(config('manager.path'))
|
|
->name('manager.')
|
|
->group(function () use ($modules) {
|
|
$this->loadRoutesFrom(__DIR__.'/../routes/manager.php');
|
|
|
|
foreach ($modules->active() as $module) {
|
|
if ($module->routes() !== null) {
|
|
$this->loadRoutesFrom($module->routes());
|
|
}
|
|
}
|
|
});
|
|
|
|
// Files for visitors: outside the manager, no prefix and none of its middleware.
|
|
foreach ($modules->active() as $module) {
|
|
if ($module->publicRoutes() !== null) {
|
|
$this->loadRoutesFrom($module->publicRoutes());
|
|
}
|
|
}
|
|
}
|
|
}
|