PHP code example of wnikk / laravel-access-rules

1. Go to this page and download the library: Download wnikk/laravel-access-rules 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/ */

    

wnikk / laravel-access-rules example snippets


$user->addPermission('orders.view');                                                      // RBAC: may or may not
$user->addPermission('orders.export', when: 'order.cost > 100 && order.items.count < 3'); // ABAC: depends on the record

$user->addPermission('products.view',
    when: "exists(product.tags, name == 'sale') && exists(product.comments, likes.count > 10)");

$user->can('products.view', $product);                      // one loaded product, checked in memory
# -- or --
Product::query()->allowedTo('products.view')->paginate();   // the list, filtered by the database:

// config/access.php: every model a condition may read
'resources' => ['product' => Product::class, 'comment' => Comment::class, 'like' => Like::class, 'tag' => Tag::class],

// Migration or seeder: create the permission with a resource
Access::newRule('products.view', 'View products', resource: 'product');

use Wnikk\LaravelAccessRules\Facades\Access;

// Add new rule permission
Access::newRule('articles.edit', 'Access to editing articles');

// Adding permissions to a user
$user->addPermission('articles.edit');

// According to the existing user from object
$user->inheritPermissionFrom(User::find(1));

// By identifier
$user->inheritPermissionFrom(User::class, 1);

// From the group
$user->inheritPermissionFrom('Group', 1);

$user->can('articles.edit'); 

$check = Access::for('AnotherAnySystemUser', 'UserID-From-Any-System-FF01')->can('articles.edit');
if (!$check) {abort(403);}