PHP code example of awesome166 / abac

1. Go to this page and download the library: Download awesome166/abac 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/ */

    

awesome166 / abac example snippets


return [
  'user_model' => App\Models\User::class, // Your user model
  'cache_ttl' => 86400, // Permission cache duration in seconds
  'account_status_check' => true, // Enable account active status check
];

// use App\Traits\HasPermissions;
use awesome166\abac\Traits\HasPermissions;

class User extends Authenticatable
{
  use HasPermissions;
  // ...
}

// Require 'read' access on 'reports' permission
Route::get('/reports', [ReportController::class, 'index'])
  ->middleware('abac:reports,read');

public function viewDashboard(User $user)
{
  return Gate::check('abac', ['dashboard', 'read']);
}

if (auth()->user()->can('abac', ['settings', 'write'])) {
  // Update settings
}

// Returns boolean
Gate::check('abac', ['notifications', 'on']);

// Must specify ac', ['documents', 'write']);

// Check specific CRUD operation
Gate::check('abac', ['posts', 'delete']);

$user->recachePermissions(); // Recache individual user

// Create permission
$permission = Permission::create([
  'slug' => 'manage-payments',
  'name' => 'Payment Management',
  'type' => 'read-write',
]);

// Assign to user
AssignedPermission::create([
  'permission_id' => $permission->id,
  'assignee_id' => $user->id,
  'assignee_type' => 'user',
  'access' => ['write']
]);

// Assign to role
AssignedPermission::create([
  'permission_id' => $permission->id,
  'assignee_id' => $role->id,
  'assignee_type' => 'role',
  'access' => ['read']
]);

// Fetch all permissions assigned to the user's roles
$user->roles()->with('permissions')->get();

// Fetch all permissions assigned to an account
$account->assignedPermissions()->with('permission')->get();

// Check if a specific permission is assigned to an account
$permission->assignedPermissions()->where('account_id', $accountId)->get();

// In User model
protected static function boot()
{
  parent::boot();

  static::created(function ($user) {
    // Assign to default user account
    $userAccount = Account::where('type', 'user')->first();
    $userRole = Role::where('name', 'Customer')->first();

    $user->update(['account_id' => $userAccount->id]);
    $user->roles()->attach($userRole);
  });
}

// Manually create admin user
$adminAccount = Account::where('type', 'admin')->first();
$adminRole = Role::where('name', 'Administrator')->first();

$user = User::create([...]);
$user->account()->associate($adminAccount);
$user->roles()->attach($adminRole);

// In controller
public function editProduct(Product $product)
{
  $this->authorize('abac', ['manage-products', 'update']);
  // ...
}
bash
php artisan abac:install
bash
php artisan vendor:publish --tag=abac-config