v0.9.2
This commit is contained in:
commit
8f8513f802
278 files changed
+18400
No files matched your search
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Console;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use IriksIT\Manager\Concerns\HasManagerAccess;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
use IriksIT\Manager\Support\Names;
|
||||
use IriksIT\Manager\Support\Tables;
|
||||
|
||||
/**
|
||||
* Checks that the manager is wired into the app, and says what to do when it is not. Meant for people and for
|
||||
* AI agents: `--json` prints one machine-readable report, and the exit code is non-zero when a check fails.
|
||||
*
|
||||
* php artisan manager:doctor [--json]
|
||||
*
|
||||
* A check is `ok`, `warn` (works, but look at it) or `fail` (the manager will not work).
|
||||
*/
|
||||
class DoctorCommand extends Command
|
||||
{
|
||||
protected $signature = 'manager:doctor {--json : Print the report as JSON}';
|
||||
|
||||
protected $description = 'Check that the manager is set up correctly in this app';
|
||||
|
||||
/** @var list<array{check: string, status: string, message: string, fix: string|null}> */
|
||||
private array $checks = [];
|
||||
|
||||
public function handle(ModuleRegistry $modules): int
|
||||
{
|
||||
$this->checkUserModel($modules);
|
||||
$this->checkTables($modules);
|
||||
$this->checkStoredPermissions($modules);
|
||||
$this->checkRoutes();
|
||||
$this->checkNames();
|
||||
$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');
|
||||
|
||||
// A table of the site that the package adopted (`manager.tables`) may lack columns the package writes.
|
||||
$lacking = [];
|
||||
|
||||
foreach ($modules->active() as $module) {
|
||||
foreach (method_exists($module, 'tables') ? $module->tables() : [] as $table) {
|
||||
if ($missingColumns = Tables::missingColumns($table)) {
|
||||
$lacking[] = Names::table($table).' ('.implode(', ', $missingColumns).')';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($lacking !== []) {
|
||||
$this->add('columns', 'fail', 'Columns the manager needs are missing: '.implode('; ', $lacking), 'Add them with a migration of the site, or run `php artisan migrate` when the manager migrations have not run yet.');
|
||||
}
|
||||
}
|
||||
|
||||
/** Rights stored in groups (by an earlier manager of the site, say) that no module of the package knows: they grant nothing. */
|
||||
private function checkStoredPermissions(ModuleRegistry $modules): void
|
||||
{
|
||||
$table = Names::table('groups');
|
||||
|
||||
if (! $modules->enabled('users') || ! Schema::hasTable($table) || ! Schema::hasColumn($table, 'permissions')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$known = $modules->permissionValues();
|
||||
$unknown = [];
|
||||
|
||||
foreach (DB::table($table)->pluck('permissions') as $stored) {
|
||||
foreach ((array) (is_string($stored) ? json_decode($stored, true) : $stored) as $permission) {
|
||||
if (is_string($permission) && ! in_array($permission, $known, true)) {
|
||||
$unknown[$permission] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$unknown === []
|
||||
? $this->add('stored_permissions', 'ok', 'Every right stored in a group is one the manager knows.')
|
||||
: $this->add('stored_permissions', 'warn', 'Groups hold rights that grant nothing: '.implode(', ', array_slice(array_keys($unknown), 0, 10)).'.', 'Name the list of the site after the right (a resource `navigation` gives `navigation.manage`), switch the module on, or remove the right from the group.');
|
||||
}
|
||||
|
||||
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.');
|
||||
}
|
||||
}
|
||||
|
||||
/** Route names or addresses of the site that the package would share with its own. */
|
||||
private function checkNames(): void
|
||||
{
|
||||
$prefix = Names::routePrefix();
|
||||
$path = trim((string) config('manager.path'), '/');
|
||||
$names = [];
|
||||
$foreign = [];
|
||||
|
||||
foreach (Route::getRoutes()->getRoutes() as $route) {
|
||||
$name = (string) $route->getName();
|
||||
$mine = $route->isFallback || ($route->getActionName() === 'Closure' && str_starts_with($name, $prefix)) || str_starts_with(ltrim($route->getActionName(), '\\'), 'IriksIT\\Manager\\');
|
||||
|
||||
if ($prefix !== '' && str_starts_with($name, $prefix) && $name !== $prefix) {
|
||||
$names[$name] = ($names[$name] ?? 0) + 1;
|
||||
}
|
||||
|
||||
if (! $mine && ($route->uri() === $path || str_starts_with($route->uri(), $path.'/'))) {
|
||||
$foreign[] = $route->uri();
|
||||
}
|
||||
}
|
||||
|
||||
$twice = array_keys(array_filter($names, fn (int $count) => $count > 1));
|
||||
|
||||
if ($twice !== []) {
|
||||
$this->add('names', 'warn', 'Route names used twice: '.implode(', ', array_slice($twice, 0, 5)).'. The site and the manager share them, and one of the two stops working.', "Set 'route_name' in config/manager.php to something the site does not use, for example 'cms'.");
|
||||
} elseif ($foreign !== []) {
|
||||
$this->add('names', 'warn', 'The site has routes under the address of the manager: '.implode(', ', array_slice($foreign, 0, 5)).'.', "Move them, or set 'path' in config/manager.php (MANAGER_PATH) to another address.");
|
||||
} else {
|
||||
$this->add('names', 'ok', 'No route name or address is shared with the site.');
|
||||
}
|
||||
}
|
||||
|
||||
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').'.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user