Skip to content

Commit 51e3a75

Browse files
committed
[Docs] Update Gate examples for Laravel 11
1 parent da5c8bc commit 51e3a75

File tree

2 files changed

+73
-16
lines changed

2 files changed

+73
-16
lines changed

docs/basic-usage/super-admin.md

+12-15
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,18 @@ Then you can implement the best-practice of primarily using permission-based con
1111
## `Gate::before`
1212
If you want a "Super Admin" role to respond `true` to all permissions, without needing to assign all those permissions to a role, you can use [Laravel's `Gate::before()` method](https://laravel.com/docs/master/authorization#intercepting-gate-checks). For example:
1313

14+
In Laravel 11 this would go in the `boot()` method of `AppServiceProvider`:
15+
In Laravel 10 and below it would go in the `boot()` method of `AuthServiceProvider.php`:
1416
```php
1517
use Illuminate\Support\Facades\Gate;
16-
17-
class AuthServiceProvider extends ServiceProvider
18+
// ...
19+
public function boot()
1820
{
19-
public function boot()
20-
{
21-
//...
22-
23-
// Implicitly grant "Super Admin" role all permissions
24-
// This works in the app by using gate-related functions like auth()->user->can() and @can()
25-
Gate::before(function ($user, $ability) {
26-
return $user->hasRole('Super Admin') ? true : null;
27-
});
28-
}
21+
// Implicitly grant "Super Admin" role all permissions
22+
// This works in the app by using gate-related functions like auth()->user->can() and @can()
23+
Gate::before(function ($user, $ability) {
24+
return $user->hasRole('Super Admin') ? true : null;
25+
});
2926
}
3027
```
3128

@@ -37,11 +34,11 @@ Jeffrey Way explains the concept of a super-admin (and a model owner, and model
3734

3835
If you aren't using `Gate::before()` as described above, you could alternatively grant super-admin control by checking the role in individual Policy classes, using the `before()` method.
3936

40-
Here is an example from the [Laravel Documentation on Policy Filters](https://laravel.com/docs/master/authorization#policy-filters)
37+
Here is an example from the [Laravel Documentation on Policy Filters](https://laravel.com/docs/master/authorization#policy-filters), where you can define `before()` in your Policy where needed:
4138

4239
```php
43-
use App\Models\User; // could be any model
44-
40+
use App\Models\User; // could be any Authorizable model
41+
4542
/**
4643
* Perform pre-authorization checks on the model.
4744
*/

docs/best-practices/using-policies.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,64 @@ Using Policies allows you to simplify things by abstracting your "control" rules
99

1010
Jeffrey Way explains the concept simply in the [Laravel 6 Authorization Filters](https://laracasts.com/series/laravel-6-from-scratch/episodes/51) and [policies](https://laracasts.com/series/laravel-6-from-scratch/episodes/63) videos and in other related lessons in that chapter. He also mentions how to set up a super-admin, both in a model policy and globally in your application.
1111

12-
You can find an example of implementing a model policy with this Laravel Permissions package in this demo app: [https://github.com/drbyte/spatie-permissions-demo/blob/master/app/Policies/PostPolicy.php](https://github.com/drbyte/spatie-permissions-demo/blob/master/app/Policies/PostPolicy.php)
12+
Here's an example of a PostPolicy which could control access to Post model records:
13+
```php
14+
<?php
15+
namespace App\Policies;
16+
17+
use App\Models\Post;
18+
use App\Models\User;
19+
use Illuminate\Auth\Access\HandlesAuthorization;
20+
21+
class PostPolicy
22+
{
23+
use HandlesAuthorization;
24+
25+
public function view(?User $user, Post $post)
26+
{
27+
if ($post->published) {
28+
return true;
29+
}
30+
31+
// visitors cannot view unpublished items
32+
if ($user === null) {
33+
return false;
34+
}
35+
36+
// admin overrides published status
37+
if ($user->can('view unpublished posts')) {
38+
return true;
39+
}
40+
41+
// authors can view their own unpublished posts
42+
return $user->id == $post->user_id;
43+
}
44+
45+
public function create(User $user)
46+
{
47+
return ($user->can('create posts'));
48+
}
49+
50+
public function update(User $user, Post $post)
51+
{
52+
if ($user->can('edit own posts')) {
53+
return $user->id == $post->user_id;
54+
}
55+
56+
if ($user->can('edit all posts')) {
57+
return true;
58+
}
59+
}
60+
61+
public function delete(User $user, Post $post)
62+
{
63+
if ($user->can('delete own posts')) {
64+
return $user->id == $post->user_id;
65+
}
66+
67+
if ($user->can('delete any post')) {
68+
return true;
69+
}
70+
}
71+
}
72+
```

0 commit comments

Comments
 (0)