PHP code example of silvanite / brandenburg

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

    

silvanite / brandenburg example snippets


// config/app.php
'providers' => [
    ...
    Silvanite\Brandenburg\Providers\BrandenburgServiceProvider::class,
];

'aliases' => [
    ...
    'BrandenburgPolicy' => Silvanite\Brandenburg\Facades\PolicyFacade::class,
],

use Silvanite\Brandenburg\Traits\HasRoles;

class User
{
    use HasRoles;
    ...
}

// AuthServiceProvider.php

if ($this->nobodyHasAccess('create-articles')) {
    return true;
};

// Check if the user has access
...

$editor = Silvanite\Brandenburg\Role::create([
    'name' => 'Editor',
    'slug' => 'editor',
]);

// Grant access
$editor->grant('create-articles');

// Revoke access
$editor->revoke('create-articles');

// Grant access to a set of permissions and remove all other permissions
$editor->setPermissions([
    'create-articles',
    'read-articles',
    'update-articles',
    'delete-articles',
]);

// Revoke all permissions
$editor->revokeAll();

$editorPermissions = $editor->permissions;

// returns ['create-articles', 'read-articles', 'update-articles', 'delete-articles']

// Using slug
$user->assignRole('editor');
$user->removeRole('editor');

// Using model
use Silvanite\Brandenburg\Role;

$user->assignRole(Role::first());
$user->removeRole(Role::first());

$user->setRolesById([1, 3, 4]);

// Same as
$user->roles()->sync([1, 3, 4]);

$canCreateArticle = $user->hasRoleWithPermission('create-articles');
sh
php artisan migrate
sh
php artisan vendor:publish --tag=brandenburg-config