|
3 | 3 | namespace App\Policies; |
4 | 4 |
|
5 | 5 | use App\Models\User; |
| 6 | +use Illuminate\Auth\Access\Response; |
6 | 7 |
|
7 | 8 | class UserPolicy |
8 | 9 | { |
9 | 10 | /** |
10 | 11 | * Determine whether the user can view any models. |
11 | 12 | */ |
12 | | - public function viewAny(User $user): bool |
| 13 | + public function viewAny(User $user): Response |
13 | 14 | { |
14 | | - return $user->is_admin; |
| 15 | + return $user->is_admin |
| 16 | + ? Response::allow() |
| 17 | + : Response::deny('You do not have permission to view users.'); |
15 | 18 | } |
16 | 19 |
|
17 | 20 | /** |
18 | 21 | * Determine whether the user can view the model. |
19 | 22 | */ |
20 | | - public function view(User $user, User $model): bool |
| 23 | + public function view(User $user, User $model): Response |
21 | 24 | { |
22 | | - return $user->is_admin; |
| 25 | + return $user->is_admin |
| 26 | + ? Response::allow() |
| 27 | + : Response::deny('You do not have permission to view this user.'); |
23 | 28 | } |
24 | 29 |
|
25 | 30 | /** |
26 | 31 | * Determine whether the user can create models. |
27 | 32 | */ |
28 | | - public function create(User $user): bool |
| 33 | + public function create(User $user): Response |
29 | 34 | { |
30 | | - return $user->is_admin; |
| 35 | + return $user->is_admin |
| 36 | + ? Response::allow() |
| 37 | + : Response::deny('You do not have permission to create a new user.'); |
31 | 38 | } |
32 | 39 |
|
33 | 40 | /** |
34 | 41 | * Determine whether the user can update the model. |
35 | 42 | */ |
36 | | - public function update(User $user, User $model): bool |
| 43 | + public function update(User $user, User $model): Response |
37 | 44 | { |
38 | | - if ($user->id == $model->id) { |
39 | | - return true; |
| 45 | + if ($model->is($user)) { |
| 46 | + return Response::deny('You cannot update your own account.'); |
40 | 47 | } |
41 | 48 |
|
42 | | - return $user->is_admin; |
| 49 | + return $user->is_admin |
| 50 | + ? Response::allow() |
| 51 | + : Response::deny('You do not have permission to update this user.'); |
43 | 52 | } |
44 | 53 |
|
45 | 54 | /** |
46 | 55 | * Determine whether the user can bulk delete any model. |
47 | 56 | */ |
48 | | - public function deleteAny(User $user): bool |
| 57 | + public function deleteAny(User $user): Response |
49 | 58 | { |
50 | | - return false; |
| 59 | + return Response::deny('You do not have permission to delete users.'); |
51 | 60 | } |
52 | 61 |
|
53 | 62 | /** |
54 | 63 | * Determine whether the user can delete the model. |
55 | 64 | */ |
56 | | - public function delete(User $user, User $model): bool |
| 65 | + public function delete(User $user, User $model): Response |
57 | 66 | { |
| 67 | + if ($model->is_admin) { |
| 68 | + return Response::deny('You cannot delete an admin user.'); |
| 69 | + } |
| 70 | + |
58 | 71 | return $user->is_admin |
59 | | - && ! $model->is_admin; |
| 72 | + ? Response::allow() |
| 73 | + : Response::deny('You do not have permission to delete this user.'); |
60 | 74 | } |
61 | 75 |
|
62 | 76 | /** |
63 | 77 | * Determine whether the user can restore the model. |
64 | 78 | */ |
65 | | - public function restore(User $user, User $model): bool |
| 79 | + public function restore(User $user, User $model): Response |
66 | 80 | { |
67 | | - return false; // soft deletes not used |
| 81 | + return Response::deny(); // soft deletes not used |
68 | 82 | } |
69 | 83 |
|
70 | 84 | /** |
71 | 85 | * Determine whether the user can permanently delete the model. |
72 | 86 | */ |
73 | | - public function forceDelete(User $user, User $model): bool |
| 87 | + public function forceDelete(User $user, User $model): Response |
74 | 88 | { |
75 | | - return false; // soft deletes not used |
| 89 | + return Response::deny(); // soft deletes not used |
76 | 90 | } |
77 | 91 | } |
0 commit comments