*/ 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}."); $guard = config('auth.defaults.guard'); $loggedIn = config('auth.providers.'.config("auth.guards.{$guard}.provider").'.model'); if (is_string($loggedIn) && $loggedIn !== $model && ! is_subclass_of($loggedIn, $model) && ! is_subclass_of($model, $loggedIn)) { $this->add('auth_model', 'fail', "People log in as {$loggedIn} (config/auth.php), but manager.user_model is {$model}: the manager would not know their rights.", 'Point manager.user_model (MANAGER_USER_MODEL) at the same model as the auth provider.'); } 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(Names::table('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 { $missing = []; foreach ($modules->active() as $module) { foreach (method_exists($module, 'tables') ? $module->tables() : [] as $table) { if (! Schema::hasTable(Names::table($table))) { $missing[] = Names::table($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(Names::route('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).'); if (Route::has('login')) { $this->add('login', 'ok', 'The app has a login route.'); } elseif (Route::has(Names::route('login'))) { $this->add('login', 'ok', 'The app has no login of its own: visitors use the login of the manager.'); } else { $this->add('login', 'warn', 'There is no login: the auth middleware has nowhere to send visitors.', 'Set MANAGER_UI=package (the manager brings a login), or add a login to the app.'); } } 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').'.'); } } }