172 lines
7.9 KiB
PHP
172 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager;
|
|
|
|
use Illuminate\Cache\RateLimiting\Limit;
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Blade;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use IriksIT\Manager\Support\IriksStorage;
|
|
use IriksIT\Manager\Support\ModuleRegistry;
|
|
use IriksIT\Manager\Support\Names;
|
|
use League\Flysystem\Filesystem;
|
|
|
|
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');
|
|
|
|
// What was set on the settings screen goes over the config before anything reads it.
|
|
Support\Settings\Store::overlay();
|
|
|
|
$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(
|
|
// A config that was published before resources existed does not list their module.
|
|
array_values(array_unique([...$app['config']->get('manager.available', []), Modules\Resources\ResourcesModule::class, Modules\Revisions\RevisionsModule::class, Modules\Settings\SettingsModule::class, Modules\Leads\LeadsModule::class])),
|
|
$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->loadJsonTranslationsFrom(__DIR__.'/../resources/lang');
|
|
$this->loadViewsFrom(__DIR__.'/../resources/views', 'manager');
|
|
// The blocks of a page: the views to change (they go where Laravel looks for a view of this package first) and the plain stylesheet.
|
|
$this->publishes([__DIR__.'/../resources/views/blocks' => resource_path('views/vendor/manager/blocks')], 'manager-blocks');
|
|
$this->publishes([__DIR__.'/../resources/blocks/blocks.css' => public_path('vendor/manager/blocks.css')], 'manager-blocks-assets');
|
|
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
|
|
|
|
foreach ([
|
|
'manager.auth' => Http\Middleware\Authenticate::class,
|
|
'manager.shell' => Http\Middleware\ServeShell::class,
|
|
'manager.locale' => Http\Middleware\SetLocale::class,
|
|
'manager.feature' => Http\Middleware\RequireFeature::class,
|
|
] as $alias => $class) {
|
|
$this->claimAlias($alias, $class);
|
|
}
|
|
// @feature('services') … @endfeature: what only shows when the feature is on.
|
|
Blade::if('feature', fn (string $key) => Support\Features::on($key));
|
|
RateLimiter::for('manager-login', fn (Request $request) => Limit::perMinute(5)->by(strtolower((string) $request->input('email')).'|'.$request->ip()));
|
|
|
|
$this->registerIriksStorage();
|
|
|
|
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, Console\ImportCommand::class, Console\StorageCheckCommand::class, Console\MediaMoveCommand::class, Console\SettingsCommand::class, Console\UserListCommand::class, Console\UserPasswordCommand::class, Console\MediaPruneCommand::class]);
|
|
}
|
|
|
|
$this->registerGates($modules);
|
|
$this->registerRoutes($modules);
|
|
}
|
|
|
|
/**
|
|
* The disk `iriksit`: files at the IriksIT API. A site that wants it sets `manager.media.disk` to `iriksit` (or any disk
|
|
* of its own with `driver` => `iriksit` in config/filesystems.php) and gives the address and the account
|
|
* in `manager.media.iriksit`. The disk is added here when the site has not made one itself.
|
|
*/
|
|
private function registerIriksStorage(): void
|
|
{
|
|
Storage::extend('iriksit', function ($app, array $config) {
|
|
$adapter = new IriksStorage\Adapter(new IriksStorage\Client($config));
|
|
|
|
return new FilesystemAdapter(new Filesystem($adapter, $config), $adapter, $config);
|
|
});
|
|
|
|
if (config('filesystems.disks.iriksit') === null) {
|
|
config(['filesystems.disks.iriksit' => ['driver' => 'iriksit', ...(array) config('manager.media.iriksit', [])]]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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());
|
|
}
|
|
|
|
/** What the routes of the package say for each of its middleware aliases: always the class, never the alias. */
|
|
private array $aliases = [];
|
|
|
|
/**
|
|
* Offer an alias for a middleware of the package, unless the app already has one of that name for something else
|
|
* (a site that had its own manager before the package, whose alias may even be set after this provider has booted).
|
|
* The routes of the package never go through the alias, only through the class, so the site's alias keeps meaning
|
|
* the site's middleware.
|
|
*/
|
|
private function claimAlias(string $alias, string $class): void
|
|
{
|
|
$this->aliases[$alias] = $class;
|
|
|
|
$taken = $this->app['router']->getMiddleware()[$alias] ?? null;
|
|
|
|
if ($taken === null || $taken === $class) {
|
|
$this->app['router']->aliasMiddleware($alias, $class);
|
|
}
|
|
}
|
|
|
|
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'), $this->aliases['manager.shell']])
|
|
->prefix(config('manager.path'))
|
|
->name(Names::routePrefix())
|
|
->group(fn () => $this->loadRoutesFrom(__DIR__.'/../routes/guest.php'));
|
|
}
|
|
|
|
Route::middleware([...$modules->middleware('before'), ...array_map(fn ($name) => $this->aliases[$name] ?? $name, config('manager.middleware')), $this->aliases['manager.locale'], ...($screens ? [$this->aliases['manager.shell']] : []), ...$modules->middleware('after')])
|
|
->prefix(config('manager.path'))
|
|
->name(Names::routePrefix())
|
|
->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());
|
|
}
|
|
}
|
|
}
|
|
}
|