stanfortonski/laravel-roles

Users Roles for Laravel Applications. Laravel 7.x and 8.x.

v3.1.0 2021-05-13 07:48 UTC

This package is auto-updated.

Last update: 2024-04-18 15:31:46 UTC


README

Users roles for Laravel applications. Compatible with Laravel 7.x and 8.x.

Information

This package provides you to set of middleware, migrations, directives and methods to user model which can you use for build Laravel Application wht different types of users. You can block or allow to use part of your application to for example admin or guest.

Installation

  1. First download package composer require stanfortonski/laravel-roles.
  2. Setup provider. In config/app.php add following code to bottom of providers:
'providers' => [
    //...
    Stanfortonski\Laravelroles\ServiceProvider::class
],
  1. Setup middleware. In app/Http/Kernel.php add following code to bottom of middlewares:
protected $routeMiddleware = [
    //...
    'role' => \Stanfortonski\Laravelroles\Middleware\Role::class,
    'roles' => \Stanfortonski\Laravelroles\Middleware\OneOfRoles::class,
    'allofroles' => \Stanfortonski\Laravelroles\Middleware\AllOfRoles::class
];
  1. Use one of three traits HasRoles, HasRolesIds or HasRolesIdsAdapter in User class. Snippet: use HasRoles;, use HasRolesIds; or use HasRolesIdsAdapter;.
  2. Additonal you can publish config file roles.php. Run command: php artisan vendor:publish --provider="Stanfortonski\Laravelroles\ServiceProvider".

Usage

More about traits HasRoles, HasRolesIds or HasRolesIdsAdapter.

  • HasRoles is for roles based on names. Default way.

  • HasRolesIds is for roles based on ids (methods suffix is ById or ByIds). HasRolesIds is actually for combining with HasRoles.

  • HasRolesIdsAdapter is for roles based on ids but methods are exactly same samed as in HasRoles trait. It is for independent use without HasRolesIds or HasRoles. Purpose of that is middlewares and directives doesn't work with HasRoleIds methods.

  • For HasRolesIds you have to use suffix ByIds for multiple or ById for singular. For parameter pass integer or array of integers. Example: $user->hasRoleById(1) instead of $user->hasRole('nameofrole')

  • For HasRolesIdsAdapter you doesn't have to use suffix but for parameter you have to pass integer or array of integers. Example: $user->hasRole(1) instead of $user->hasRole('nameofrole')

Middleware

If you want to determine which user can use the link. You need to use one of three middleware: roles, roles, allofroles.

Examples: (Attention you have to define admin, moderator and writer roles before! Go To Seeding example.)

  1. Only admin can access to main page.
    Route::get('/', function () {
        return view('welcome');
    })->middleware('role:admin');
  1. Only Admin or moderator can access to main page.
    Route::get('/', function () {
        return view('welcome');
    })->middleware('roles:admin|moderator');
  1. Only User that have admin and writer roles (all of these) can access to main page.
    Route::get('/', function () {
        return view('welcome');
    })->middleware('allofroles:admin|writer');

Checking that user has roles

    $result = $user->hasRole('admin');
    $result = $user->hasOneOfRoles(['admin', 'mod']);
    $result = $user->hasAllOfRoles(['admin', 'mod']);

Adding roles

    $user->addRole('admin');
    $user->addRoles(['admin', 'writer']);

Removing roles

    $user->removeRole('admin');
    $user->removeRoles(['admin', 'mod']);

Seeding

If you want to define yours own roles run this command php artisan make:seeder RoleSeeder and next copy and paste below code to database/seeders/RoleSeeder.php.

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class RoleSeeder extends Seeder
{
    private $roles = [
        ['name' => 'admin', 'description' => 'User friendly text'], //role I
        //... next role
    ];

    public function run()
    {
        foreach ($this->roles as $role){
            DB::table('roles')->insert($role);
        }
    }
}

Then run composer dump-autoload and php artisan migrate --seed.