This is my package lrp

0.0.3 2023-05-10 16:04 UTC

This package is auto-updated.

Last update: 2024-09-03 22:40:48 UTC


README

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

This package provide basic roles and permissions, this readme.md contains examples of usage.

Installation

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

Usage

Update user model relationship

app\Models\User.php

use NovatoPro\Lrp\Traits\UserLrp;

class User extends Authenticatable
{
    use UserLrp;
}

Update boot method in AuthServiceProvider

app\Providers\AuthServiceProvider.php

Gate::define('access', function (User $user, ...$permissions) {
    return $user->hasPermissions($permissions);
});

Examples

use NovatoPro\Lrp\Models\Role;
use NovatoPro\Lrp\Models\Permission;
// Example user credentials
$credentials = [
    'name'=>'Example User',
    'email'=>'developer.user@example.com',
    '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

License

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