Skip to content

Guard is Role and Permission management system for Laravel

License

Notifications You must be signed in to change notification settings

amdad121/guard-laravel

Repository files navigation

Guard Role And Permission Package For Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Guard is Role and Permission management system for Laravel

Installation

You can install the package via composer:

composer require amdadulhaq/guard-laravel

You can publish and run the migrations with:

php artisan vendor:publish --tag="guard-laravel-migrations"
php artisan migrate

Add HasRoles Trait and UserContract Interface on User Model

namespace App\Models;

use AmdadulHaq\Guard\Contracts\User as UserContract;
use AmdadulHaq\Guard\HasRoles;
# ...

class User extends Authenticatable implements UserContract
{
    use HasRoles;
}

Usage

Role Create

use AmdadulHaq\Guard\Models\Role;

Role::create(['name' => 'administrator']);

Permission Create

use AmdadulHaq\Guard\Models\Permission;
use AmdadulHaq\Guard\Models\Role;

$items = [
    'role' => ['viewAny', 'view', 'create', 'update', 'delete', 'restore', 'forceDelete'],
    'permission' => ['viewAny', 'view', 'create', 'update', 'delete'],
];

$role = Role::first();

foreach ($items as $group => $names) {
    foreach ($names as $name) {
        $permission = Permission::create(['name' => $group.'.'.$name]);

        $role->givePermissionTo($permission);
    }
}

Assign Role and Permission

use AmdadulHaq\Guard\Models\Permission;
use AmdadulHaq\Guard\Models\Role;
use App\Models\User;

$user = User::first();

$role = Role::first();

// Assign role
$user->assignRole($role);

$permission = Permission::first();

// Assign permission
$role->givePermissionTo($permission);

Revoke Role and Permission

use AmdadulHaq\Guard\Models\Permission;
use AmdadulHaq\Guard\Models\Role;
use App\Models\User;

$user = User::first();

$role = Role::first();

// Revoke role
$user->revokeRole($role);

$permission = Permission::first();

// Revoke permission
$role->revokePermissionTo($permission);

Check Role and Permission

use AmdadulHaq\Guard\Models\Permission;
use AmdadulHaq\Guard\Models\Role;
use App\Models\User;

$user = User::first();

$role = Role::first();

// Role check
$user->hasRole($role->name) // true or false

$permission = Permission::first();

// Permission check
$user->hasPermission($permission); // true or false

You can use multiple ways, some of are given bellow:

use Illuminate\Support\Facades\Gate;

// for permission
Gate::authorize('role.view');

// for role
Gate::authorize('administrator');
// for permission
$this->authorize('role.view');

// for role
$this->authorize('administrator');
use Illuminate\Support\Facades\Route;

// for permission
Route::get('/', function () {
    // ...
})->middleware('can:role.view');

// for role
Route::get('/', function () {
    // ...
})->middleware('can:administrator');
// for permission
@can('role.view')
    It's works
@endcan

// for role
@can('administrator')
    It's works
@endcan

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Guard is Role and Permission management system for Laravel

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Languages