PHP code example of techgonia / pbac

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

    

techgonia / pbac example snippets


use Pbac\Traits\HasPbacAccessControl;
use Pbac\Traits\HasPbacGroups;
use Pbac\Traits\HasPbacTeams;

class User extends Authenticatable
{
    use HasPbacAccessControl, HasPbacGroups, HasPbacTeams;
}

use Pbac\Models\PBACAccessControl;
use Pbac\Facades\Pbac;

// Grant permission
PBACAccessControl::factory()
    ->allow()
    ->forUser($user)
    ->forResource(Post::class, $post->id)
    ->withAction('edit')
    ->create();

// Or use the Facade
Pbac::allow()
    ->forUser($user)
    ->forResource(Post::class, $post->id)
    ->withAction('edit')
    ->create();

// Check permission
if ($user->can('edit', $post)) {
    // User can edit this post
}

// Or use the Facade
if (Pbac::can($user, 'edit', $post)) {
    // User can edit this post
}

// Even with high priority allow...
PBACAccessControl::factory()->allow()->withPriority(1000)->create();

// ...a low priority deny wins
PBACAccessControl::factory()->deny()->withPriority(1)->create();

// Result: Access DENIED (secure by default)

$editors = PBACAccessGroup::create(['name' => 'Editors']);
$user->groups()->attach($editors->id);

PBACAccessControl::factory()
    ->allow()
    ->forGroup($editors)
    ->forResource(Post::class, null) // All posts
    ->withAction(['view', 'edit', 'publish'])
    ->create();

PBACAccessControl::factory()
    ->allow()
    ->forUser($admin)
    ->forResource(AdminPanel::class, null)
    ->withAction('access')
    ->create([
        'extras' => ['allowed_ips' => ['192.168.1.0/24']]
    ]);

PBACAccessControl::factory()
    ->allow()
    ->forUser($user)
    ->forResource(Post::class, null)
    ->withAction('edit')
    ->create([
        'extras' => [
            '

$team = PBACAccessTeam::create(['name' => 'Team Alpha']);
$user->teams()->attach($team->id);

PBACAccessControl::factory()
    ->allow()
    ->forTeam($team)
    ->forResource(Document::class, null)
    ->withAction('*')
    ->create();

$user->is_super_admin = true;
$user->can('anything', $anything); // always true

Gate::allows('edit', $post);
Gate::authorize('publish', $post);

PBACAccessControl::factory()
    ->allow()                           // Set effect
    ->forUser($user)                   // Set target
    ->forResource(Post::class, $id)    // Set resource
    ->withAction(['view', 'edit'])      // Set actions
    ->withPriority(10)                  // Set priority
    ->create(['extras' => [...]]);      // Add conditions