diff --git a/AGENTS.md b/AGENTS.md index 6d909c0..7ac90cd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -19,6 +19,6 @@ between lil-koneko.com, CarsHub, iriks-it.nl and bouwbedrijf.iriks-it.nl, as one ## Open decisions -1. Frontend: ship the Inertia/Vue screens inside the package (built assets, published to the host) or make the package headless with a separate UI package. +1. Frontend: decided to ship the Inertia/Vue screens inside the package (prebuilt bundle, host needs no Node) with per-user and site themes; being built after v0.4.0's AI tooling slice. 2. Moving the rest from bouwbedrijf.iriks-it.nl, module by module: pages, reviews, then revisions, audit log, login and profile. Users, media and posts (with labels) are done. Leave out what is site-specific (the IriksIT API disk, the owner's simple screens, client names): this repo is public. 3. How the host's own `User` and a package `Group` relate when the host has its own roles. diff --git a/README.md b/README.md index 8a43692..45017c1 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,16 @@ php artisan migrate Put `IriksIT\Manager\Concerns\HasManagerAccess` on the `User` model. The manager lives at `/manager` (`MANAGER_PATH`) behind the `web` and `auth` middleware (`config/manager.php`). +## Set up with an AI agent + +```bash +php artisan manager:install --modules=users,media,posts --user-model="App\Models\User" +php artisan manager:doctor --json # non-zero exit when something is wrong; each problem has a `fix` +``` + +`resources/ai/INTEGRATION.md` is a step-by-step guide for an agent; `resources/boost/guidelines/core.blade.php` +is picked up by Laravel Boost. + ## Modules | Key | What | Default | diff --git a/TOKENS.md b/TOKENS.md index 0c1f232..fb86de7 100644 --- a/TOKENS.md +++ b/TOKENS.md @@ -22,3 +22,5 @@ Per stuk werk een rij; zie `dev/token-usage.py`. | 2026-12-04 | Wekelijkse update van de versiebereiken van afhankelijkheden, als pull request naar de open release | - | claude-sonnet-5-5 | 6 | 7.611 | 12 | 10.963 | 1.632.350 | 0,01 | | 2026-12-04 | De commit zonder AI-vermelding hersteld in de release, de uitleg over de nachtelijke scans gecorrigeerd en de notities in longterm-memory gemaakt | - | claude-sonnet-5-5 | 29 | 16.997 | 72 | 21.381 | 8.975.420 | 0,11 | | 2026-12-04 | De Packagist-stap meldt het pakket zelf aan als Packagist het nog niet kent | - | claude-sonnet-5-5 | 13 | 7.095 | 30 | 11.993 | 4.256.820 | 0,06 | +| 2026-12-04 | Installatie en gezondheidscheck voor AI (manager:install en manager:doctor) met een stappengids voor agents | - | claude-sonnet-5-5 | 12 | 12.112 | 28 | 64.789 | 2.344.181 | 0,04 | +| 2026-12-04 | Installatie en gezondheidscheck voor AI (manager:install en manager:doctor) met een stappengids voor agents | - | claude-fable-5-2 | 1 | 17.440 | 362.597 | 0 | 0 | 0,00 | diff --git a/resources/ai/INTEGRATION.md b/resources/ai/INTEGRATION.md new file mode 100644 index 0000000..485892d --- /dev/null +++ b/resources/ai/INTEGRATION.md @@ -0,0 +1,22 @@ +# Integrating iriks-it/laravel-manager (guide for AI agents) + +Goal: the manager runs in this Laravel app and `php artisan manager:doctor` is green. Do the steps in order and +never edit files inside `vendor/`. + +1. `composer require iriks-it/laravel-manager` +2. Pick the modules. `users` and `media` are on; `pages`, `posts` default on; `reviews`, `audit`, `statistics` default off + (audit and statistics store visitor data: only switch them on when the owner says so). +3. `php artisan manager:install --modules=users,media,posts --user-model="App\Models\User"` + (writes `MANAGER_*` to `.env`, publishes `config/manager.php`, runs the doctor). Add `--path=admin` to move the URL. +4. Add the trait to the user model: `use \IriksIT\Manager\Concerns\HasManagerAccess;`. Never put `is_super_admin` in `$fillable`. +5. `php artisan migrate` (a new process, so it sees the switches you wrote in step 3). +6. Create the first super admin: `php artisan manager:create-user --name="..." --email="..." --password="<12+ chars>"`. +7. Run `php artisan manager:doctor --json`. Every check must be `ok` or an understood `warn`; each `fail`/`warn` has a `fix`. + Repeat until `"ok": true`. + +Facts that matter: +- The manager lives at `/manager` (`MANAGER_PATH`) behind `web` + `auth` (`manager.middleware`); the app needs a route named `login`. +- Anything that is off has no routes, tables or permissions. A module that needs another one fails at boot when that one is off. +- Answers are JSON. Do not add site-specific code to the package: a site's own part is its own module class (`Contracts\Module`, + extend `Modules\BaseModule`, add it to `manager.available`). +- Behind a proxy set trusted proxies before switching `audit` on. diff --git a/resources/boost/guidelines/core.blade.php b/resources/boost/guidelines/core.blade.php new file mode 100644 index 0000000..1279a3c --- /dev/null +++ b/resources/boost/guidelines/core.blade.php @@ -0,0 +1,8 @@ +## iriks-it/laravel-manager + +A modular backend (users, media, pages, posts, reviews, audit, statistics) installed as a package. + +- To integrate it, follow `vendor/iriks-it/laravel-manager/resources/ai/INTEGRATION.md` step by step. +- Set it up with `php artisan manager:install --modules=... --user-model=...`; verify with `php artisan manager:doctor --json` (non-zero exit on failure, every problem has a `fix`). +- The user model needs the `HasManagerAccess` trait; `is_super_admin` is never mass assignable. The first super admin comes from `php artisan manager:create-user`. +- Switch parts with `MANAGER_=true|false`; never edit `vendor/`. Site-specific parts are your own `Module` class. diff --git a/src/Console/DoctorCommand.php b/src/Console/DoctorCommand.php new file mode 100644 index 0000000..2404fcc --- /dev/null +++ b/src/Console/DoctorCommand.php @@ -0,0 +1,146 @@ + */ + private array $checks = []; + + public function handle(ModuleRegistry $modules): int + { + $this->checkUserModel($modules); + $this->checkTables($modules); + $this->checkRoutes(); + $this->checkModules($modules); + + $failed = collect($this->checks)->contains('status', 'fail'); + + if ($this->option('json')) { + $this->line(json_encode(['ok' => ! $failed, 'checks' => $this->checks], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + } else { + foreach ($this->checks as $check) { + $line = sprintf('[%s] %s: %s', strtoupper($check['status']), $check['check'], $check['message']); + $check['status'] === 'fail' ? $this->error($line) : ($check['status'] === 'warn' ? $this->warn($line) : $this->info($line)); + + if ($check['fix'] !== null && $check['status'] !== 'ok') { + $this->line(' fix: '.$check['fix']); + } + } + } + + return $failed ? self::FAILURE : self::SUCCESS; + } + + private function add(string $check, string $status, string $message, ?string $fix = null): void + { + $this->checks[] = ['check' => $check, 'status' => $status, 'message' => $message, 'fix' => $fix]; + } + + private function checkUserModel(ModuleRegistry $modules): void + { + $model = config('manager.user_model'); + + if (! is_string($model) || ! class_exists($model)) { + $this->add('user_model', 'fail', 'manager.user_model is not a class: '.var_export($model, true), 'Set MANAGER_USER_MODEL (or manager.user_model) to your User model, for example App\\Models\\User.'); + + return; + } + + $this->add('user_model', 'ok', "The user model is {$model}."); + + if (! in_array(HasManagerAccess::class, class_uses_recursive($model), true)) { + $this->add('user_trait', 'fail', "{$model} does not use HasManagerAccess.", "Add `use \\IriksIT\\Manager\\Concerns\\HasManagerAccess;` inside the class {$model}."); + + return; + } + + $this->add('user_trait', 'ok', 'The user model uses HasManagerAccess.'); + + if (in_array('is_super_admin', (new $model)->getFillable(), true)) { + $this->add('super_admin_fillable', 'fail', 'is_super_admin is mass assignable: anyone who can fill the model could make themselves a super admin.', 'Remove is_super_admin from $fillable of the user model.'); + } + + if ($modules->enabled('users') && Schema::hasTable((new $model)->getTable()) && Schema::hasTable(config('manager.table_prefix').'groups')) { + $admins = $model::query()->where('is_super_admin', true)->count(); + $admins > 0 + ? $this->add('super_admin', 'ok', "{$admins} super admin(s) exist.") + : $this->add('super_admin', 'warn', 'There is no super admin yet: nobody can manage groups.', 'php artisan manager:create-user --name=... --email=... --password=...'); + } + } + + private function checkTables(ModuleRegistry $modules): void + { + $prefix = config('manager.table_prefix'); + $missing = []; + + foreach ($modules->active() as $module) { + foreach (method_exists($module, 'tables') ? $module->tables() : [] as $table) { + if (! Schema::hasTable($prefix.$table)) { + $missing[] = $prefix.$table; + } + } + } + + $missing === [] + ? $this->add('tables', 'ok', 'The tables of every active module exist.') + : $this->add('tables', 'fail', 'Missing tables: '.implode(', ', $missing), 'php artisan migrate'); + } + + private function checkRoutes(): void + { + Route::has('manager.dashboard') + ? $this->add('routes', 'ok', 'The manager routes are registered under /'.trim((string) config('manager.path'), '/').'.') + : $this->add('routes', 'fail', 'The manager routes are not registered.', 'Run `composer dump-autoload` and make sure the package service provider is discovered (not in dont-discover).'); + + Route::has('login') + ? $this->add('login', 'ok', 'The app has a login route.') + : $this->add('login', 'warn', 'The app has no route named `login`: the auth middleware has nowhere to send visitors.', 'Add a login to the app (for example a starter kit), or change manager.middleware.'); + } + + private function checkModules(ModuleRegistry $modules): void + { + $active = array_keys($modules->active()); + $this->add('modules', 'ok', 'Active modules: '.implode(', ', $active).'.'); + + if ($modules->enabled('media')) { + $disk = config('manager.media.disk'); + config("filesystems.disks.{$disk}") === null + ? $this->add('media_disk', 'fail', "The media disk \"{$disk}\" is not in config/filesystems.php.", 'Set MANAGER_MEDIA_DISK to a disk that exists.') + : $this->add('media_disk', 'ok', "The media disk is \"{$disk}\"."); + + if ($disk === 'public' && ! file_exists(public_path('storage'))) { + $this->add('storage_link', 'warn', 'public/storage does not exist: uploaded files cannot be served from the public disk.', 'php artisan storage:link'); + } + } + + if ($modules->enabled('audit')) { + $this->add('proxies', 'warn', 'The audit log stores the visitor address. Behind a proxy or load balancer, trusted proxies must be configured, or every visitor looks like one address and one bad actor can lock the manager out.', 'Configure trusted proxies (bootstrap/app.php: $middleware->trustProxies(...)) and set MANAGER_NEVER_BAN to your own address.'); + } + + if ($modules->enabled('statistics')) { + $pages = (array) config('manager.statistics.pages'); + $pages === ['/'] && ! config('manager.statistics.posts_path') + ? $this->add('statistics_pages', 'warn', 'Only the home page counts in the statistics.', 'Set manager.statistics.pages (and posts_path), or register a resolver with CountedPages::register().') + : $this->add('statistics_pages', 'ok', 'The statistics count '.count($pages).' page(s) and '.(config('manager.statistics.posts_path') ? 'the posts' : 'no posts').'.'); + } + } +} diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php new file mode 100644 index 0000000..15223fa --- /dev/null +++ b/src/Console/InstallCommand.php @@ -0,0 +1,89 @@ +callSilently('vendor:publish', ['--tag' => 'manager-config']); + + $env = []; + + if ($list = $this->option('modules')) { + $wanted = array_filter(array_map('trim', explode(',', $list))); + $unknown = array_diff($wanted, array_keys($modules->all())); + + if ($unknown !== []) { + $this->error('Unknown module(s): '.implode(', ', $unknown).'. Known: '.implode(', ', array_keys($modules->all())).'.'); + + return self::FAILURE; + } + + foreach (array_keys($modules->all()) as $key) { + if (in_array($key, ['users', 'media'], true)) { + continue; // switched in config, on by default + } + + $env['MANAGER_'.strtoupper($key)] = in_array($key, $wanted, true) ? 'true' : 'false'; + } + } + + if ($model = $this->option('user-model')) { + $env['MANAGER_USER_MODEL'] = $model; + } + + if ($path = $this->option('path')) { + $env['MANAGER_PATH'] = trim($path, '/'); + } + + foreach ($env as $key => $value) { + $this->writeEnv($key, $value); + } + + if ($env !== []) { + $this->line('Wrote to .env: '.implode(', ', array_map(fn ($k, $v) => "{$k}={$v}", array_keys($env), $env)).'. Run `php artisan config:clear` if the config is cached.'); + } + + if ($this->option('migrate')) { + // The switches were just written: a fresh process sees them, this one still has the old config. + $this->line('Run `php artisan migrate` (a new process) so the migrations of the modules you switched on are loaded.'); + } + + return Artisan::call('manager:doctor', $this->option('json') ? ['--json' => true] : [], $this->output) === 0 ? self::SUCCESS : self::FAILURE; + } + + private function writeEnv(string $key, string $value): void + { + $file = base_path('.env'); + $contents = file_exists($file) ? file_get_contents($file) : ''; + $line = "{$key}={$value}"; + + $contents = preg_match("/^{$key}=.*$/m", $contents) + ? preg_replace("/^{$key}=.*$/m", $line, $contents) + : rtrim($contents, "\n")."\n{$line}\n"; + + file_put_contents($file, $contents); + } +} diff --git a/src/ManagerServiceProvider.php b/src/ManagerServiceProvider.php index 21fe62b..56cfa93 100644 --- a/src/ManagerServiceProvider.php +++ b/src/ManagerServiceProvider.php @@ -45,6 +45,10 @@ class ManagerServiceProvider extends ServiceProvider $module->boot(); } + if ($this->app->runningInConsole()) { + $this->commands([Console\InstallCommand::class, Console\DoctorCommand::class]); + } + $this->registerGates($modules); $this->registerRoutes($modules); } diff --git a/src/Modules/Audit/AuditModule.php b/src/Modules/Audit/AuditModule.php index 3f0477d..b7ac0fa 100644 --- a/src/Modules/Audit/AuditModule.php +++ b/src/Modules/Audit/AuditModule.php @@ -36,6 +36,11 @@ class AuditModule extends BaseModule return false; } + public function tables(): array + { + return ['audit_logs', 'ip_bans', 'audit_settings', 'ip_notes']; + } + public function permissions(): array { return [ diff --git a/src/Modules/BaseModule.php b/src/Modules/BaseModule.php index 07119e6..30bb816 100644 --- a/src/Modules/BaseModule.php +++ b/src/Modules/BaseModule.php @@ -45,6 +45,16 @@ abstract class BaseModule implements Module return []; } + /** + * Tables (without the prefix) this module needs; `manager:doctor` checks that they exist. + * + * @return list + */ + public function tables(): array + { + return []; + } + public function actions(): array { return []; diff --git a/src/Modules/Media/MediaModule.php b/src/Modules/Media/MediaModule.php index 876ae1b..ba9e36c 100644 --- a/src/Modules/Media/MediaModule.php +++ b/src/Modules/Media/MediaModule.php @@ -25,6 +25,11 @@ class MediaModule extends BaseModule return true; } + public function tables(): array + { + return ['media']; + } + public function permissions(): array { return [ diff --git a/src/Modules/Pages/PagesModule.php b/src/Modules/Pages/PagesModule.php index 1fc0732..d6c3a33 100644 --- a/src/Modules/Pages/PagesModule.php +++ b/src/Modules/Pages/PagesModule.php @@ -24,6 +24,11 @@ class PagesModule extends BaseModule return false; } + public function tables(): array + { + return ['pages']; + } + public function permissions(): array { return [ diff --git a/src/Modules/Posts/PostsModule.php b/src/Modules/Posts/PostsModule.php index 38306d2..af32646 100644 --- a/src/Modules/Posts/PostsModule.php +++ b/src/Modules/Posts/PostsModule.php @@ -24,6 +24,11 @@ class PostsModule extends BaseModule return false; } + public function tables(): array + { + return ['posts', 'post_labels']; + } + public function permissions(): array { return [ diff --git a/src/Modules/Reviews/ReviewsModule.php b/src/Modules/Reviews/ReviewsModule.php index bf7b63c..e09ecc6 100644 --- a/src/Modules/Reviews/ReviewsModule.php +++ b/src/Modules/Reviews/ReviewsModule.php @@ -22,6 +22,11 @@ class ReviewsModule extends BaseModule return false; } + public function tables(): array + { + return ['reviews']; + } + public function permissions(): array { return [ diff --git a/src/Modules/Statistics/StatisticsModule.php b/src/Modules/Statistics/StatisticsModule.php index d8d357f..8f7c11b 100644 --- a/src/Modules/Statistics/StatisticsModule.php +++ b/src/Modules/Statistics/StatisticsModule.php @@ -33,6 +33,11 @@ class StatisticsModule extends BaseModule return false; } + public function tables(): array + { + return ['visitor_salts', 'visits', 'page_views', 'bot_hits']; + } + public function permissions(): array { return [new Permission('statistics.view', 'See the visitor statistics and download them', 'Statistics')]; diff --git a/src/Modules/Users/UsersModule.php b/src/Modules/Users/UsersModule.php index b7ef624..fcda472 100644 --- a/src/Modules/Users/UsersModule.php +++ b/src/Modules/Users/UsersModule.php @@ -22,6 +22,11 @@ class UsersModule extends BaseModule return true; } + public function tables(): array + { + return ['groups', 'group_user']; + } + public function permissions(): array { return [ diff --git a/tests/Feature/DoctorTest.php b/tests/Feature/DoctorTest.php new file mode 100644 index 0000000..234819d --- /dev/null +++ b/tests/Feature/DoctorTest.php @@ -0,0 +1,61 @@ + true]); + + return json_decode(Artisan::output(), true); +} + +it('reports a healthy setup as JSON with exit code 0', function () { + $report = doctorReport(); + $status = collect($report['checks'])->pluck('status', 'check'); + + expect($status['user_trait'])->toBe('ok') + ->and($status['tables'])->toBe('ok') + ->and($status['routes'])->toBe('ok') + ->and($report['ok'])->toBeTrue(); +}); + +it('fails with a fix when the user model is not a class', function () { + config(['manager.user_model' => 'Nope\\User']); + + $check = collect(doctorReport()['checks'])->firstWhere('check', 'user_model'); + + expect($check['status'])->toBe('fail')->and($check['fix'])->toContain('MANAGER_USER_MODEL'); + expect(Artisan::call('manager:doctor'))->toBe(1); +}); + +it('warns when there is no super admin yet', function () { + $check = collect(doctorReport()['checks'])->firstWhere('check', 'super_admin'); + + expect($check['status'])->toBe('warn')->and($check['fix'])->toContain('manager:create-user'); +}); + +it('lists the tables of the active modules', function () { + $tables = collect(app(ModuleRegistry::class)->active())->flatMap->tables(); + + expect($tables)->toContain('media', 'posts'); +}); + +it('refuses an unknown module in the installer', function () { + expect(Artisan::call('manager:install', ['--modules' => 'users,nonsense']))->toBe(1); +}); + +it('writes the switches to .env', function () { + $env = base_path('.env'); + $before = file_exists($env) ? file_get_contents($env) : null; + + Artisan::call('manager:install', ['--modules' => 'users,media,posts', '--path' => 'admin']); + + $written = file_get_contents($env); + $before === null ? unlink($env) : file_put_contents($env, $before); + + expect($written)->toContain('MANAGER_PATH=admin')->toContain('MANAGER_AUDIT=false')->toContain('MANAGER_POSTS=true'); +});