1. Go to this page and download the library: Download silber/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/ */
silber / 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);
use Silber\Bouncer\Database\HasRolesAndAbilities;
class User extends Model
{
use HasRolesAndAbilities;
}
use Bouncer;
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([/* connection config */]);
$capsule->setAsGlobal();
use Illuminate\Database\Eloquent\Model;
use Silber\Bouncer\Database\HasRolesAndAbilities;
class User extends Model
{
use HasRolesAndAbilities;
}
use Silber\Bouncer\Bouncer;
$bouncer = Bouncer::create();
// If you are in a request with a current user
// that you'd wish to check permissions for,
// pass that user to the "create" method:
$bouncer = Bouncer::create($user);
use Silber\Bouncer\Bouncer;
use Illuminate\Container\Container;
Container::getInstance()->singleton(Bouncer::class, function () {
return Bouncer::create();
});
use Silber\Bouncer\Bouncer;
$bouncer = Bouncer::make()
->withCache($customCacheInstance)
->create();
Bouncer::allow($user)->toOwn(Post::class)->to('view');
// Or pass it an array of abilities:
Bouncer::allow($user)->toOwn(Post::class)->to(['view', 'update']);
Bouncer::allow($user)->toOwnEverything();
// And to restrict ownership to a given ability
Bouncer::allow($user)->toOwnEverything()->to('view');
namespace App\Models;
use Silber\Bouncer\Database\Ability as BouncerAbility;
class Ability extends BouncerAbility
{
// custom code
}
namespace App\Models;
use Silber\Bouncer\Database\Role as BouncerRole;
class Role extends BouncerRole
{
// custom code
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Silber\Bouncer\Database\Concerns\IsAbility;
class Ability extends Model
{
use IsAbility;
// custom code
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Silber\Bouncer\Database\Concerns\IsRole;
class Role extends Model
{
use IsRole;
// custom code
}
Bouncer::ownedVia(Game::class, function ($game, $user) {
return $game->team_id == $user->team_id;
});
use Bouncer;
use Illuminate\Database\Seeder;
class BouncerSeeder extends Seeder
{
public function run()
{
Bouncer::allow('superadmin')->everything();
Bouncer::allow('admin')->everything();
Bouncer::forbid('admin')->toManage(User::class);
Bouncer::allow('editor')->to('create', Post::class);
Bouncer::allow('editor')->toOwn(Post::class);
// etc.
}
}
use Bouncer, Closure;
class ScopeBouncer
{
public function handle($request, Closure $next, $identifier)
{
Bouncer::scope()->to($identifier);
return $next($request);
}
}
protected $routeMiddleware = [
// Keep the other route middleware, and add this:
'scope-bouncer' => \App\Http\Middleware\ScopeBouncer::class,
];