PHP code example of dubroquin / bouncer
1. Go to this page and download the library: Download dubroquin/bouncer 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/ */
dubroquin / bouncer example snippets
// Give a user the ability to create posts
Bouncer::allow($user)->to('create', Post::class);
// Alternatively, do it through a role
Bouncer::allow('admin')->to('create', Post::class);
Bouncer::assign('admin')->to($user);
// You can also grant an ability only to a specific model
Bouncer::allow($user)->to('edit', $post);
Dubroquin\Bouncer\BouncerServiceProvider::class,
'Bouncer' => Dubroquin\Bouncer\BouncerFacade::class,
use Dubroquin\Bouncer\Database\HasRolesAndAbilities;
class User extends Model
{
use HasRolesAndAbilities;
}
use Bouncer;
Bouncer::cache();
Bouncer::allow('admin')->to('ban-users');
Bouncer::assign('admin')->to($user);
$user->assign('admin');
Bouncer::allow($user)->to('ban-users');
$user->allow('ban-users');
Bouncer::allow($user)->to('edit', Post::class);
Bouncer::allow($user)->to('edit', $post);
Bouncer::retract('admin')->from($user);
$user->retract('admin');
Bouncer::disallow($user)->to('ban-users');
$user->disallow('ban-users');
Bouncer::disallow('admin')->to('ban-users');
Bouncer::disallow($user)->to('delete', Post::class);
Bouncer::disallow($user)->to('delete', $post);
Bouncer::is($user)->a('moderator');
Bouncer::is($user)->an('admin');
Bouncer::is($user)->notA('moderator');
Bouncer::is($user)->notAn('admin');
Bouncer::is($user)->a('moderator', 'editor');
Bouncer::is($user)->all('editor', 'moderator');
Bouncer::is($user)->notAn('editor', 'moderator');
$user->isAn('admin');
$user->isA('subscriber');
$user->isNotAn('admin');
$user->isNotA('subscriber');
$user->isAll('editor', 'moderator');
$abilities = $user->getAbilities();
Bouncer::allows($ability);
Bouncer::denies($ability);
@if ($user->isAn('admin'))
//
@endif
Bouncer::refresh();
Bouncer::refreshFor($user);
// Adding abilities for users
Bouncer::allow($user)->to('ban-users');
Bouncer::allow($user)->to('edit', Post::class);
Bouncer::allow($user)->to('delete', $post);
Bouncer::allow($user)->everything();
Bouncer::allow($user)->toManage(Post::class);
Bouncer::allow($user)->toManage($post);
Bouncer::allow($user)->toAlways('view');
Bouncer::allow($user)->toOwn(Post::class);
Bouncer::allow($user)->toOwnEverything();
// Removing abilities uses the same syntax, e.g.
Bouncer::disallow($user)->to('delete', $post);
Bouncer::disallow($user)->toManage(Post::class);
Bouncer::disallow($user)->toOwn(Post::class);
// Adding & removing abilities for roles
Bouncer::allow('admin')->to('ban-users');
Bouncer::disallow('admin')->to('ban-users');
// Assigning & retracting roles from users
Bouncer::assign('admin')->to($user);
Bouncer::retract('admin')->from($user);
$check = Bouncer::allows('ban-users');
$check = Bouncer::allows('edit', Post::class);
$check = Bouncer::allows('delete', $post);
$check = Bouncer::denies('ban-users');
$check = Bouncer::denies('edit', Post::class);
$check = Bouncer::denies('delete', $post);
$check = Bouncer::is($user)->a('subscriber');
$check = Bouncer::is($user)->an('admin');
$check = Bouncer::is($user)->notA('subscriber');
$check = Bouncer::is($user)->notAn('admin');
$check = Bouncer::is($user)->a('moderator', 'editor');
$check = Bouncer::is($user)->all('moderator', 'editor');
Bouncer::cache();
Bouncer::dontCache();
Bouncer::refresh();
Bouncer::refreshFor($user);
$user->allow('ban-users');
$user->allow('edit', Post::class);
$user->allow('delete', $post);
$user->disallow('ban-users');
$user->disallow('edit', Post::class);
$user->disallow('delete', $post);
$user->assign('admin');
$user->retract('admin');
$check = $user->isAn('admin');
$check = $user->isAn('editor', 'moderator');
$check = $user->isAll('moderator', 'editor');
$check = $user->isNot('subscriber', 'moderator');
$abilities = $user->getAbilities();
php artisan vendor:publish --tag="bouncer.migrations"
php artisan migrate
php artisan vendor:publish --provider="Dubroquin\Bouncer\BouncerServiceProvider" --tag="migrations"
php artisan bouncer:upgrade
php artisan bouncer:upgrade --no-migrate