snoeren-development/laravel-admin-users

Define and check for admin users in your application!

Fund package maintenance!
bunq.me/SnoerenDevelopment

Installs: 9 979

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 0

Type:laravel-package

v1.9.0 2024-11-07 08:27 UTC

This package is auto-updated.

Last update: 2024-11-07 08:28:27 UTC


README

Latest version on Packagist Software License Build status Downloads

This small package adds a configuration and user model trait to define administrators within your application.

Installation

Install the package using Composer:

composer require snoeren-development/laravel-admin-users

Requirements

This package requires at least PHP 8.2 and Laravel 10.

Usage

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.

Using the middleware

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,

Grant administrators all permissions

// 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 Blade

{{-- 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

Publishing the configuration file

You can publish the configuration file to your project using php artisan vendor:publish. Then select the admin package.

Using the MySQL driver

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.

Testing

composer test

Credits

License

The MIT license. See LICENSE for more information.