Admin middleware

Role-based access to the admin panel through Spatie Permission, replacing the is_admin column fallback the core ships with.

What it adds

FilePurpose
app/Http/Middleware/EnsureUserIsAdmin.phpAllows the request when the user has the admin role
database/seeders/AdminRoleSeeder.phpCreates the admin and user roles
HasRoles in app/Models/Concerns/UiKitUser.phpAdded to the generated trait, so $user->hasRole('admin') works

Install

php artisan ui-kit:install-module admin-middleware

Composer: spatie/laravel-permission:^6.10, required by the installer before the stubs are copied.

What the installer changes

  • Publishes Spatie's config and migrations and runs migrate.
  • Runs AdminRoleSeeder.
  • Swaps EnsureIsAdminFallback::class for App\Http\Middleware\EnsureUserIsAdmin::class in the middleware list of config/admin.php.
  • Regenerates the UiKitUser trait with HasRoles and re-applies it to app/Models/User.php.

No routes, no navigation entries, no screens.

After install

Grant the role to your account:

php artisan ui-kit:make-admin [email protected]

make-admin assigns the role and also sets is_admin when that column exists, so an app that installs this module later keeps working for users who were admins before.

Using roles elsewhere

Everything Spatie offers is available: $user->assignRole('editor'), @role('admin') in Blade, role:admin middleware on your own routes. The admin routes keep using EnsureUserIsAdmin, which is a plain class in your app; edit it to check a permission instead of a role if that fits better.

Removing

Restore \Shipbytes\UiKit\Http\Middleware\EnsureIsAdminFallback::class in config/admin.php, delete the middleware and seeder, remove HasRoles from the trait, remove admin-middleware from installed_modules in config/ui-kit.php, and composer remove spatie/laravel-permission after rolling back its migration.

Was this page helpful?