1. Go to this page and download the library: Download delgont/armor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App;
use Delgont\Armor\PermissionRegistrar;
class ExpensePermissionRegistrar extends PermissionRegistrar
{
const CAN_MANAGE_EXPENSES = 'can_manage_expenses';
const CAN_ADD_EXPENSES = 'can_add_expenses';
/**
* Provide descriptions for each permission.
*
* @return array
*/
public function descriptions(): array
{
return [
self::CAN_MANAGE_EXPENSES => 'Allow user to manage expenses',
self::CAN_ADD_EXPENSES => 'Allow user to add expenses',
];
}
}
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Delgont\Armor\Armor;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application servipSycnces.
*
* @return void
*/
public function boot()
{
Armor::registerPermissionRegistrars([
\App\ExpensePermissionRegistrar::class,
]);
}
}
namespace App;
use Delgont\Armor\Concerns\ModelHasPermissions;
class User extends Authenticatable
{
use ModelHasPermissions;
}
# Geting permissions of specific group
$permissions = app(\App\ExpensePermissionRegistrar::class)->getPermissionsFromDb();
# Get model which uses trait Delgont\Armor\Concerns\ModelHasPermissions
$user = User::whereId(3)->first();
# Give permissions to user
$user->givePermissionTo($permissions);
# Withdrawal permissions from user
$user->withdrawPermissionsTo($permissions);
# Sync permissions from user
$user->syncPermissions($permissions);
# Check if model|user has permission
if($user->hasPermission('can_manage_expenses')) {
return 'User has the permission';
} else {
return 'User does not have the permission';
}
# Check if model has any of the permissions
if($user->hasAnyPermission('can_add_expenses', 'can_manage_expenses')) {
return 'User has the permission';
} else {
return 'User does not have the permission';
}
# Protect Routes - Works only for user model which is authenticatable
Route::post('/expenses/add', 'ExpenseController@store')->middleware(['permission:can_add_expenses']);
cmd
php artisan armor:sync-permissions
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.