This small package adds a configuration and user model trait to define administrators within your application.
Install the package using Composer:
composer require snoeren-development/laravel-admin-users
This package requires at least PHP 8.2 and Laravel 10.
Add the SnoerenDevelopment\AdminUsers\Adminable
trait to your user model and
define administrator users using the ADMINS=
environment variable. The variable
should contain a comma separated list of email addresses of your application
administrators.
This package comes with middleware. This middleware checks if the user is an administrator. If not, the middleware throws a 401 unauthorised exception.
Add the middleware to your $routeMiddleware
list in the HTTP kernel to use.
// Kernel.php
'admin' => \SnoerenDevelopment\AdminUsers\AdminMiddleware::class,
// AuthServiceProvider.php
use Illuminate\Support\Facades\Gate;
public function boot()
{
$this->registerPolicies();
Gate::before(function ($user, $ability) {
if ($user->isAdmin()) {
return true;
}
});
}
{{-- Using the gate via the can-statement. --}}
@can('admin')
The current user is an admin!
@else
The current user is not an admin!
@endcan
{{-- Using the custom if-statement --}}
@admin
The current user is an admin!
@else
The current user is not an admin!
@endadmin
You can publish the configuration file to your project using
php artisan vendor:publish
. Then select the admin package.
Publish and run the migration or manually add the is_admin
column to your users
table. Set the driver to mysql
in your .env
file using ADMINS_DRIVER=mysql
.
composer test
The MIT license. See LICENSE for more information.