novatopro / lrp
This is my package lrp
0.0.3
2023-05-10 16:04 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- livewire/livewire: ^2.12
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.5
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
This package is auto-updated.
Last update: 2024-09-03 22:40:48 UTC
README
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.