novatopro/lrp is a Laravel package for this is my package lrp.
It currently has 0 GitHub stars and 11 downloads on Packagist (latest version 0.0.3).
Install it with composer require novatopro/lrp.
Discover more Laravel packages by novatopro
or browse all Laravel packages to compare alternatives.
Last updated
This package provide basic roles and permissions, this readme.md contains examples of usage.
You can install the package via composer:
composer require novatopro/lrp
You can publish and run the migrations with:
php artisan vendor:publish --tag="lrp-migrations"
php artisan migrate
app\Models\User.php
use NovatoPro\Lrp\Traits\UserLrp;
class User extends Authenticatable
{
use UserLrp;
}
app\Providers\AuthServiceProvider.php
Gate::define('access', function (User $user, ...$permissions) {
return $user->hasPermissions($permissions);
});
use NovatoPro\Lrp\Models\Role;
use NovatoPro\Lrp\Models\Permission;
// Example user credentials
$credentials = [
'name'=>'Example User',
'email'=>'[email protected]',
'password'=>'password'
];
// Create example user
$user = User::updateOrCreate(['email'=>$credentials['email']],['name'=>$credentials['name'],'password'=>Hash::make($credentials['password'])]);
// Create example role
$role = Role::updateOrCreate(['name'=>'Developer Features','slug'=>'developer-features']);
// Create example permission
$permission = Permission::updateOrCreate(['name'=>'Dev','slug'=>'dev','description'=>'Can see features in development']);
// Add permision to role without remove, without duplicate
$role->permissions()->syncWithoutDetaching($permission->id);
// Add role to user without remove, without duplicate
$user->roles()->syncWithoutDetaching($role->id);
// Check permissions in controllers
if($user->can('access', ['developer','dev','develop'])){
// Can see features in development
}else{
// Can't see features in development
}
// Authorize with permissions
use Illuminate\Support\Facades\Gate;
Gate::authorize('access','dev');
// Check permissions in blade
@can('access', ['developer','dev','develop'])
<h1>Can see features in development</h1>
@else
<h1>Can't see features in development</h1>
@endcan
The MIT License (MIT). Please see License File for more information.