-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbouncer_cheatsheet.txt
85 lines (65 loc) · 2.66 KB
/
bouncer_cheatsheet.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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)->to('view')->everything();
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');
// You can also forbid specific abilities with the same syntax...
Bouncer::forbid($user)->to('delete', $post);
// And also remove a forbidden ability with the same syntax...
Bouncer::unforbid($user)->to('delete', $post);
// Re-syncing a user's abilities
Bouncer::sync($user)->abilities($abilities);
// Assigning & retracting roles from users
Bouncer::assign('admin')->to($user);
Bouncer::retract('admin')->from($user);
// Re-syncing a user's roles
Bouncer::sync($user)->roles($roles);
// Checking the current user's abilities
$boolean = Bouncer::can('ban-users');
$boolean = Bouncer::can('edit', Post::class);
$boolean = Bouncer::can('delete', $post);
$boolean = Bouncer::cannot('ban-users');
$boolean = Bouncer::cannot('edit', Post::class);
$boolean = Bouncer::cannot('delete', $post);
// Checking a user's roles
$boolean = Bouncer::is($user)->a('subscriber');
$boolean = Bouncer::is($user)->an('admin');
$boolean = Bouncer::is($user)->notA('subscriber');
$boolean = Bouncer::is($user)->notAn('admin');
$boolean = Bouncer::is($user)->a('moderator', 'editor');
$boolean = Bouncer::is($user)->all('moderator', 'editor');
Bouncer::cache();
Bouncer::dontCache();
Bouncer::refresh();
Bouncer::refreshFor($user);
Some of this functionality is also available directly on the user model:
$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');
$boolean = $user->isAn('admin');
$boolean = $user->isAn('editor', 'moderator');
$boolean = $user->isAll('moderator', 'editor');
$boolean = $user->isNotAn('admin', 'moderator');
// Querying users by their roles
$users = User::whereIs('superadmin')->get();
$users = User::whereIs('superadmin', 'admin')->get();
$users = User::whereIsAll('sales', 'marketing')->get();
$abilities = $user->getAbilities();
$forbidden = $user->getForbiddenAbilities();