onelearningcommunity/laravel-model-explorer
Model Explorer exposes internal application structure — model definitions, table schemas, column types, relationships, and eventually live data. Accidentally exposing this to unauthenticated users in a production environment would be a serious security incident.
Authorization must therefore be:
Several patterns were considered:
APP_ENV=local. Simple, but too rigid: staging environments and shared development servers legitimately need access.enabled in config/model-explorer.php. Provides an emergency kill switch but not fine-grained access control.viewModelExplorer) with a default implementation. Consumers override it in their AuthServiceProvider using Laravel's standard idiom.Authorization is implemented via a Laravel Gate named viewModelExplorer.
The package service provider registers a default gate in packageBooted():
Gate::define('viewModelExplorer', function ($user = null): bool {
return app()->environment('local');
});
This default:
APP_ENV=local.Because App\Providers\AuthServiceProvider::boot() runs after package service providers, any consumer-defined override of the gate will replace the default:
// In the host application's AuthServiceProvider
Gate::define('viewModelExplorer', function (User $user): bool {
return $user->hasRole('developer');
});
A separate enabled config key acts as a hard kill switch — returning 404 regardless of gate outcome. This allows teams to completely disable the tool via environment variable (MODEL_EXPLORER_ENABLED=false) without touching authorization logic.
Laravel's Gate short-circuits to false for unauthenticated requests when a gate callback's first parameter does not accept null. Any gate override for this package must declare $user = null (or ?User $user) to permit unauthenticated access:
// Correct — allows guests through to the gate logic
Gate::define('viewModelExplorer', fn (?User $user) => true);
// Incorrect — Laravel will deny unauthenticated requests before calling this
Gate::define('viewModelExplorer', fn (User $user) => true);
This behaviour is enforced by the package's test suite and documented here for consumers who override the gate.
Positive:
enabled = false) provides a fast, zero-logic escape hatch.Negative:
$user = null requirement for guest access is a non-obvious Laravel behaviour that will surprise consumers who write a gate without it. Mitigated by documentation and the test suite.How can I help you explore Laravel packages today?