First setup: module registry, switchable modules, permissions
This commit is contained in:
commit
936c794597
52 files changed
+1304
No files matched your search
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Concerns;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use IriksIT\Manager\Models\Group;
|
||||
|
||||
/**
|
||||
* Put this on the User model of the host app. Needs the Users module (its migration
|
||||
* adds `manager_super_admin` to the users table and creates the groups).
|
||||
*
|
||||
* A super admin may do everything. Everyone else may do what any of their groups may do.
|
||||
*/
|
||||
trait HasManagerAccess
|
||||
{
|
||||
public function managerGroups(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Group::class, config('manager.table_prefix').'group_user', 'user_id', 'group_id');
|
||||
}
|
||||
|
||||
public function isManagerSuperAdmin(): bool
|
||||
{
|
||||
return (bool) $this->manager_super_admin;
|
||||
}
|
||||
|
||||
public function hasManagerPermission(string $permission): bool
|
||||
{
|
||||
if ($this->isManagerSuperAdmin()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->managerGroups->contains(fn (Group $group) => in_array($permission, $group->permissions ?? [], true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Contracts;
|
||||
|
||||
/**
|
||||
* One switchable part of the manager. A module that is off contributes nothing:
|
||||
* no routes, no migrations, no menu items, no permissions.
|
||||
*/
|
||||
interface Module
|
||||
{
|
||||
/** Short unique key, also the key in config('manager.modules'). */
|
||||
public function key(): string;
|
||||
|
||||
/** Name in the menu. */
|
||||
public function label(): string;
|
||||
|
||||
/** Core modules (users, media) are on unless a site explicitly turns them off. */
|
||||
public function enabledByDefault(): bool;
|
||||
|
||||
/** @return list<string> keys of modules this one cannot work without */
|
||||
public function requires(): array;
|
||||
|
||||
/**
|
||||
* Permissions this module adds, as value => label. Values are prefixed by the module key.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function permissions(): array;
|
||||
|
||||
/** @return list<array{label: string, route: string, permission: string}> */
|
||||
public function navigation(): array;
|
||||
|
||||
/** Absolute path of the routes file, or null. */
|
||||
public function routes(): ?string;
|
||||
|
||||
/** Absolute path of the migrations folder, or null. */
|
||||
public function migrations(): ?string;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Exceptions;
|
||||
|
||||
class ModuleException extends \LogicException {}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
|
||||
/**
|
||||
* @method static bool enabled(string $key)
|
||||
* @method static array active()
|
||||
* @method static array permissions()
|
||||
* @method static array navigation()
|
||||
*
|
||||
* @see ModuleRegistry
|
||||
*/
|
||||
class Manager extends Facade
|
||||
{
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
return ModuleRegistry::class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
|
||||
class DashboardController
|
||||
{
|
||||
public function __invoke(Request $request, ModuleRegistry $modules)
|
||||
{
|
||||
// Only what this account may reach.
|
||||
$navigation = array_values(array_filter(
|
||||
$modules->navigation(),
|
||||
fn (array $item) => $request->user()?->can($item['permission']),
|
||||
));
|
||||
|
||||
return view('manager::dashboard', ['navigation' => $navigation]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager;
|
||||
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
|
||||
class ManagerServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->mergeConfigFrom(__DIR__.'/../config/manager.php', 'manager');
|
||||
|
||||
$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');
|
||||
|
||||
// A module that is off adds no migrations, so a site without reviews has no reviews table.
|
||||
foreach ($modules->active() as $module) {
|
||||
if ($module->migrations() !== null) {
|
||||
$this->loadMigrationsFrom($module->migrations());
|
||||
}
|
||||
}
|
||||
|
||||
$this->registerGates($modules);
|
||||
$this->registerRoutes($modules);
|
||||
}
|
||||
|
||||
private function registerGates(ModuleRegistry $modules): void
|
||||
{
|
||||
foreach (array_keys($modules->permissions()) as $permission) {
|
||||
Gate::define($permission, fn ($user) => method_exists($user, 'hasManagerPermission') && $user->hasManagerPermission($permission));
|
||||
}
|
||||
}
|
||||
|
||||
private function registerRoutes(ModuleRegistry $modules): void
|
||||
{
|
||||
Route::middleware(config('manager.middleware'))
|
||||
->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());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Models;
|
||||
|
||||
class Group extends Model
|
||||
{
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['permissions' => 'array'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Models;
|
||||
|
||||
class Media extends Model
|
||||
{
|
||||
public function getTable(): string
|
||||
{
|
||||
return config('manager.table_prefix').'media';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model as Eloquent;
|
||||
|
||||
/** Base of the package models: tables get the configured prefix, e.g. manager_pages. */
|
||||
abstract class Model extends Eloquent
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
public function getTable(): string
|
||||
{
|
||||
return $this->table ?? config('manager.table_prefix').parent::getTable();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Models;
|
||||
|
||||
class Page extends Model {}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Models;
|
||||
|
||||
class Post extends Model {}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Models;
|
||||
|
||||
class Review extends Model {}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules;
|
||||
|
||||
use IriksIT\Manager\Contracts\Module;
|
||||
|
||||
/**
|
||||
* Convention over configuration: a module `Foo` lives in src/Modules/Foo, with
|
||||
* routes.php and a migrations/ folder next to its class when it needs them.
|
||||
*/
|
||||
abstract class BaseModule implements Module
|
||||
{
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function requires(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function navigation(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function routes(): ?string
|
||||
{
|
||||
return $this->path('routes.php');
|
||||
}
|
||||
|
||||
public function migrations(): ?string
|
||||
{
|
||||
return $this->path('migrations');
|
||||
}
|
||||
|
||||
private function path(string $name): ?string
|
||||
{
|
||||
$path = dirname((new \ReflectionClass($this))->getFileName()).'/'.$name;
|
||||
|
||||
return file_exists($path) ? $path : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Media;
|
||||
|
||||
use IriksIT\Manager\Models\Media;
|
||||
|
||||
class MediaController
|
||||
{
|
||||
/** First opzet: lists the records as JSON. The screens come with the frontend. */
|
||||
public function index()
|
||||
{
|
||||
return response()->json(Media::query()->latest()->paginate(25));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Media;
|
||||
|
||||
use IriksIT\Manager\Modules\BaseModule;
|
||||
|
||||
class MediaModule extends BaseModule
|
||||
{
|
||||
public function key(): string
|
||||
{
|
||||
return 'media';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Media';
|
||||
}
|
||||
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [
|
||||
'media.view' => 'See the media library',
|
||||
'media.upload' => 'Upload files',
|
||||
'media.edit' => 'Change name and description of a file',
|
||||
'media.delete' => 'Delete files',
|
||||
];
|
||||
}
|
||||
|
||||
public function navigation(): array
|
||||
{
|
||||
return [
|
||||
['label' => 'Media', 'route' => 'manager.media.index', 'permission' => 'media.view'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create(config('manager.table_prefix').'media', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('disk');
|
||||
$table->string('path');
|
||||
$table->string('filename');
|
||||
$table->string('mime_type');
|
||||
$table->unsignedBigInteger('size');
|
||||
$table->unsignedInteger('width')->nullable();
|
||||
$table->unsignedInteger('height')->nullable();
|
||||
$table->string('title')->nullable();
|
||||
$table->text('description')->nullable();
|
||||
$table->unsignedBigInteger('uploaded_by')->nullable()->index();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists(config('manager.table_prefix').'media');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use IriksIT\Manager\Modules\Media\MediaController;
|
||||
|
||||
Route::get('/media', [MediaController::class, 'index'])->name('media.index')->middleware('can:media.view');
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Pages;
|
||||
|
||||
use IriksIT\Manager\Models\Page;
|
||||
|
||||
class PagesController
|
||||
{
|
||||
/** First opzet: lists the records as JSON. The screens come with the frontend. */
|
||||
public function index()
|
||||
{
|
||||
return response()->json(Page::query()->latest()->paginate(25));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Pages;
|
||||
|
||||
use IriksIT\Manager\Modules\BaseModule;
|
||||
|
||||
class PagesModule extends BaseModule
|
||||
{
|
||||
public function key(): string
|
||||
{
|
||||
return 'pages';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Pages';
|
||||
}
|
||||
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [
|
||||
'pages.edit' => 'Change the texts of the pages',
|
||||
];
|
||||
}
|
||||
|
||||
public function navigation(): array
|
||||
{
|
||||
return [
|
||||
['label' => 'Pages', 'route' => 'manager.pages.index', 'permission' => 'pages.edit'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create(config('manager.table_prefix').'pages', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('slug')->unique();
|
||||
$table->string('title');
|
||||
$table->longText('body')->nullable();
|
||||
$table->boolean('is_published')->default(false);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists(config('manager.table_prefix').'pages');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use IriksIT\Manager\Modules\Pages\PagesController;
|
||||
|
||||
Route::get('/pages', [PagesController::class, 'index'])->name('pages.index')->middleware('can:pages.edit');
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Posts;
|
||||
|
||||
use IriksIT\Manager\Models\Post;
|
||||
|
||||
class PostsController
|
||||
{
|
||||
/** First opzet: lists the records as JSON. The screens come with the frontend. */
|
||||
public function index()
|
||||
{
|
||||
return response()->json(Post::query()->latest()->paginate(25));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Posts;
|
||||
|
||||
use IriksIT\Manager\Modules\BaseModule;
|
||||
|
||||
class PostsModule extends BaseModule
|
||||
{
|
||||
public function key(): string
|
||||
{
|
||||
return 'posts';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Posts';
|
||||
}
|
||||
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [
|
||||
'posts.view' => 'See the posts, drafts included',
|
||||
'posts.write' => 'Write and change posts',
|
||||
'posts.publish' => 'Publish and unpublish posts',
|
||||
'posts.delete' => 'Delete posts',
|
||||
];
|
||||
}
|
||||
|
||||
public function navigation(): array
|
||||
{
|
||||
return [
|
||||
['label' => 'Posts', 'route' => 'manager.posts.index', 'permission' => 'posts.view'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create(config('manager.table_prefix').'posts', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('slug')->unique();
|
||||
$table->string('title');
|
||||
$table->text('excerpt')->nullable();
|
||||
$table->longText('body')->nullable();
|
||||
$table->timestamp('published_at')->nullable()->index();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists(config('manager.table_prefix').'posts');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use IriksIT\Manager\Modules\Posts\PostsController;
|
||||
|
||||
Route::get('/posts', [PostsController::class, 'index'])->name('posts.index')->middleware('can:posts.view');
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Reviews;
|
||||
|
||||
use IriksIT\Manager\Models\Review;
|
||||
|
||||
class ReviewsController
|
||||
{
|
||||
/** First opzet: lists the records as JSON. The screens come with the frontend. */
|
||||
public function index()
|
||||
{
|
||||
return response()->json(Review::query()->latest()->paginate(25));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Reviews;
|
||||
|
||||
use IriksIT\Manager\Modules\BaseModule;
|
||||
|
||||
class ReviewsModule extends BaseModule
|
||||
{
|
||||
public function key(): string
|
||||
{
|
||||
return 'reviews';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Reviews';
|
||||
}
|
||||
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [
|
||||
'reviews.manage' => 'Manage the reviews',
|
||||
];
|
||||
}
|
||||
|
||||
public function navigation(): array
|
||||
{
|
||||
return [
|
||||
['label' => 'Reviews', 'route' => 'manager.reviews.index', 'permission' => 'reviews.manage'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create(config('manager.table_prefix').'reviews', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('author');
|
||||
$table->string('place')->nullable();
|
||||
$table->unsignedTinyInteger('rating')->nullable();
|
||||
$table->text('body');
|
||||
$table->boolean('is_visible')->default(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists(config('manager.table_prefix').'reviews');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use IriksIT\Manager\Modules\Reviews\ReviewsController;
|
||||
|
||||
Route::get('/reviews', [ReviewsController::class, 'index'])->name('reviews.index')->middleware('can:reviews.manage');
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Users;
|
||||
|
||||
class UsersController
|
||||
{
|
||||
/** First opzet: lists the accounts as JSON. The screens come with the frontend. */
|
||||
public function index()
|
||||
{
|
||||
$user = config('manager.user_model');
|
||||
|
||||
return response()->json($user::query()->with('managerGroups')->latest()->paginate(25));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Users;
|
||||
|
||||
use IriksIT\Manager\Modules\BaseModule;
|
||||
|
||||
class UsersModule extends BaseModule
|
||||
{
|
||||
public function key(): string
|
||||
{
|
||||
return 'users';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Users';
|
||||
}
|
||||
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [
|
||||
'users.manage' => 'Create, change and delete accounts, and put them in groups',
|
||||
];
|
||||
}
|
||||
|
||||
public function navigation(): array
|
||||
{
|
||||
return [
|
||||
['label' => 'Users', 'route' => 'manager.users.index', 'permission' => 'users.manage'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$prefix = config('manager.table_prefix');
|
||||
|
||||
Schema::create($prefix.'groups', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name')->unique();
|
||||
$table->json('permissions')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
Schema::create($prefix.'group_user', function (Blueprint $table) use ($prefix) {
|
||||
$table->foreignId('group_id')->constrained($prefix.'groups')->cascadeOnDelete();
|
||||
$table->unsignedBigInteger('user_id')->index();
|
||||
$table->primary(['group_id', 'user_id']);
|
||||
});
|
||||
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->boolean('manager_super_admin')->default(false);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$prefix = config('manager.table_prefix');
|
||||
|
||||
Schema::table('users', fn (Blueprint $table) => $table->dropColumn('manager_super_admin'));
|
||||
Schema::dropIfExists($prefix.'group_user');
|
||||
Schema::dropIfExists($prefix.'groups');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use IriksIT\Manager\Modules\Users\UsersController;
|
||||
|
||||
Route::get('/users', [UsersController::class, 'index'])->name('users.index')->middleware('can:users.manage');
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support;
|
||||
|
||||
use IriksIT\Manager\Contracts\Module;
|
||||
use IriksIT\Manager\Exceptions\ModuleException;
|
||||
|
||||
/**
|
||||
* Knows which modules exist and which of them are on for this site.
|
||||
*/
|
||||
final class ModuleRegistry
|
||||
{
|
||||
/** @var array<string, Module> */
|
||||
private array $all = [];
|
||||
|
||||
/**
|
||||
* @param list<class-string<Module>> $available
|
||||
* @param array<string, bool> $switches
|
||||
*/
|
||||
public function __construct(array $available, private readonly array $switches)
|
||||
{
|
||||
foreach ($available as $class) {
|
||||
$module = new $class;
|
||||
$this->all[$module->key()] = $module;
|
||||
}
|
||||
}
|
||||
|
||||
/** @return array<string, Module> every known module */
|
||||
public function all(): array
|
||||
{
|
||||
return $this->all;
|
||||
}
|
||||
|
||||
public function has(string $key): bool
|
||||
{
|
||||
return isset($this->all[$key]);
|
||||
}
|
||||
|
||||
public function enabled(string $key): bool
|
||||
{
|
||||
if (! $this->has($key)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) ($this->switches[$key] ?? $this->all[$key]->enabledByDefault());
|
||||
}
|
||||
|
||||
/** @return array<string, Module> the modules that are on */
|
||||
public function active(): array
|
||||
{
|
||||
return array_filter($this->all, fn (Module $module) => $this->enabled($module->key()));
|
||||
}
|
||||
|
||||
/**
|
||||
* A module that is on while one it needs is off is a configuration mistake: fail loudly at boot.
|
||||
*/
|
||||
public function validate(): void
|
||||
{
|
||||
foreach ($this->active() as $module) {
|
||||
foreach ($module->requires() as $needed) {
|
||||
if (! $this->enabled($needed)) {
|
||||
throw new ModuleException("Manager module '{$module->key()}' needs module '{$needed}', which is off.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @return array<string, string> permission value => label, of the active modules only */
|
||||
public function permissions(): array
|
||||
{
|
||||
$permissions = [];
|
||||
|
||||
foreach ($this->active() as $module) {
|
||||
$permissions += $module->permissions();
|
||||
}
|
||||
|
||||
return $permissions;
|
||||
}
|
||||
|
||||
/** @return list<array{label: string, route: string, permission: string}> */
|
||||
public function navigation(): array
|
||||
{
|
||||
$items = [];
|
||||
|
||||
foreach ($this->active() as $module) {
|
||||
array_push($items, ...$module->navigation());
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user