PHP code example of thepublicgood / deadbolt

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

    

thepublicgood / deadbolt example snippets


$permissions = [
	'Create Articles',
	'Edit Articles',
	'Delete Articles',
];

$permissions = [
	'articles.create' => 'Create Articles',
	'articles.edit' => 'Edit Articles',
	'articles.delete' => 'Delete Articles',
];

$permissions = Deadbolt::all();

/*
[
	'articles.create',
	'articles.edit',
	'articles.delete',
]
*/

$permissions = Deadbolt::describe();

/*
[
	'articles.create' => 'Create Articles',
	'articles.edit' => 'Edit Articles',
	'articles.delete' => 'Delete Articles',
]
*/

$permission = Deadbolt::describe('articles.create');
// $permission = 'Create Articles';

$permissions = Deadbolt::describe(['articles.create', 'articles.edit']);

/*
[
	'articles.create' => 'Create Articles',
	'articles.edit' => 'Edit Articles',
]
*/

$deadbolt = Deadbolt::user($request->user());

// Give a single permission
Deadbolt::user($user)->give('articles.create');

// Give muliple permissions
Deadbolt::user($user)->give('articles.create', 'articles.edit');

// Give an array of permissions
Deadbolt::user($user)->give($arrayOfPermissions);

Deadbolt::user($user)->super();

Deadbolt::user($user)->give('articles.publish');
// Throws a NoSuchPermissionException.

$permissions = Deadbolt::user($user)->all();

// Revoke a single permission
Deadbolt::user($user)->revoke('articles.edit');

// Revoke multiple permissions
Deadbolt::user($user)->revoke('articles.edit', 'articles.delete');

// Revoke an array of permissions
Deadbolt::user($user)->revoke($arrayOfArticles);

Deadbolt::user($user)->revokeAll();

Deadbolt::user($user)->sync($arrayOfPermissions);

// Check if a user has a permission
Deadbolt::user($user)->has('articles.create');

// Check that a user has ALL of the permissions
Deadbolt::user($user)->has('articles.create', 'articles.edit');

// Will be true even if only one of the permissions is assigned.
Deadbolt::user($user)->any('articles.edit', 'articles.delete');

// Will be false if the user has any of the specified permissions
Deadbolt::user($user)->none('articles.create', 'articles.delete');

// Give all the users a permission
Deadbolt::users($users)->give('articles.edit');

// Remove the specified permisssions from all users.
Deadbolt::users($users)->revoke('articles.delete');

// All the users MUST HAVE all of the permissions
Deadbolt::users($users)->have($arrayOfPermissions);

Deadbolt::users($users)->dontHave($arrayOfPermissions);

class User extends Authenticatable
{
    use HasPermissions;

    //...
}

$user = User::find(1);

// Give a permission
$user->permissions()->give('articles.edit');

// Or revoke a permission
$user->permissions()->revoke('articles.edit');

// Or test for a permission
$canEdit = $user->permissions()->has('articles.edit');



namespace App\\Policies;

use App\\User;
use Illuminate\\Auth\\Access\\HandlesAuthorization;

class ArticlePolicy
{
    use HandlesAuthorization;

    public function create(User $user)
    {
        return Deadbolt::user($user)->has('articles.create');

		// Or if you're using the HasPermissions trait:
		return $user->permissions()->has('articles.create');
    }
}

$user->can('create', Article::class);

public function update(User $user, Article $article)
{
    return Deadbolt::user($user)->hasAll('articles.create', 'articles.edit');

	// Or if you're using the HasPermissions trait:
	return $user->hasAll('articles.create', 'articles.edit');
}

$user->can('update', $article);

$driver = new DatabaseDriver($config);
Deadbolt::driver($driver))->user($user)->give('...');

return [

    'driver' => \App\Drivers\DatabaseDriver::class,

];



namespace App\\Drivers;

use Illuminate\\Support\\Arr;
use Illuminate\\Support\\Facades\\DB;
use TPG\\Deadbolt\\Drivers\\Contracts\\DriverInterface;

class DatabaseDriver implements DriverInterface
{
    protected $config;

    public function __construct(array $config)
    {
        $this->config = $config;
    }

    public function permissions(): array
    {
        // return an array of permission names filtered by `$groups`
        // Descriptions MUST be 
bash
php ./artisan deadbolt:install
bash
php ./artisan vendor:publish --provider=TPG\\Deadbolt\\DeadboltServiceProvider