mashyindustries / laraccess by MrMashy

Roles, Inheritence & Permissions for Laravel 5.4
14
3
1
Package Data
Maintainer Username: MrMashy
Package Create Date: 2017-01-09
Package Last Update: 2017-03-31
Language: PHP
License: MIT
Last Refreshed: 2024-11-18 03:01:06
Package Statistics
Total Downloads: 14
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 3
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

Laraccess - User Roles & Role Inheritance

Works with Laravel 5.4

This package allows to save roles in a database. Roles can inherit other roles too.

It includes blade directives & middleware

Install

You can install the package via composer:

$ composer require mashyindustries/laraccess

This service provider must be installed.

// config/app.php
'providers' => [
    ...
    Mashy\Laraccess\LaraccessServiceProvider::class,
];

You can publish the migration with:

php artisan vendor:publish --provider="Mashy\Laraccess\LaraccessServiceProvider" --tag="migrations"

The package assumes that your users table name is called "users". If this is not the case you should manually edit the published migration to use your custom table name.

After the migration has been published you can create the role tables with:

php artisan migrate

You can publish the config-file with:

php artisan vendor:publish --provider="Mashy\Laraccess\LaraccessServiceProvider" --tag="config"

Usage

First add the Mashy\Laraccess\Traits\HasRoles trait to your User model.

use Mashy\Laraccess\Traits\HasRoles;

class User
{
    use HasRoles;
    
    // ...
}

This package allows for users to be associated with roles.

You can create roles with:

use Mashy\Laraccess\Models\Role;

$role = Role::create([
    'name' => 'Writer', //optional
    'slug' => 'writer', //required
    'description' => '' //optional
]);

The HasRoles adds collections to your models.

$roles = $user->roles(); // returns a collection

Using Roles

A role can be assigned to a user:

$user->assignRole('writer');

// you can also assign multiple roles at once
$user->assignRole('writer', 'admin');
$user->assignRole(['writer', 'admin']);

A role can be removed from a user:

$user->removeRole('writer');

Roles can also be synced :

//all current roles will be removed from the user and replace by the array given
$user->syncRoles(['writer', 'admin']);

You can determine if a user has a certain role:

$user->hasRole('writer');

You can also determine if a user has any of a given list of roles:

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

You can also determine if a user has all of a given list of roles:

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

Using blade directives

This package also adds Blade directives to verify whether the currently logged in user has all or any of a given list of roles.

@role('writer')
I'm a writer!
@else
I'm not a writer...
@endrole
@hasanyrole(['writer', 'admin'])
I have one or more of these roles!
@else
I have none of these roles...
@endrole
@hasallroles(['writer', 'admin'])
I have all of these roles!
@else
I don't have all of these roles...
@endrole

You can use Laravel's native @can directive to check if a user has a certain permission.

Using a middleware

Information coming soon...

Inheritance

Information coming soon...

Credits

This package was based on Spatie/Laravel-Permission

Alternatives