Skip to content

Commit c33e49c

Browse files
authored
[Chore] Update policies to use Illuminate\Auth\Access\Response (alexjustesen#1908)
1 parent d89f40d commit c33e49c

File tree

2 files changed

+53
-35
lines changed

2 files changed

+53
-35
lines changed

app/Policies/ResultPolicy.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,75 @@
44

55
use App\Models\Result;
66
use App\Models\User;
7+
use Illuminate\Auth\Access\Response;
78

89
class ResultPolicy
910
{
1011
/**
1112
* Determine whether the user can view any models.
1213
*/
13-
public function viewAny(User $user): bool
14+
public function viewAny(User $user): Response
1415
{
15-
return true;
16+
return Response::allow();
1617
}
1718

1819
/**
1920
* Determine whether the user can view the model.
2021
*/
21-
public function view(User $user, Result $result): bool
22+
public function view(User $user, Result $result): Response
2223
{
23-
return true;
24+
return Response::allow();
2425
}
2526

2627
/**
2728
* Determine whether the user can create models.
2829
*/
29-
public function create(User $user): bool
30+
public function create(User $user): Response
3031
{
31-
return false;
32+
return Response::deny();
3233
}
3334

3435
/**
3536
* Determine whether the user can update the model.
3637
*/
37-
public function update(User $user, Result $result): bool
38+
public function update(User $user, Result $result): Response
3839
{
39-
return $user->is_admin
40-
|| $user->is_user;
40+
return Response::allow();
4141
}
4242

4343
/**
4444
* Determine whether the user can bulk delete any model.
4545
*/
46-
public function deleteAny(User $user)
46+
public function deleteAny(User $user): Response
4747
{
48-
return $user->is_admin;
48+
return $user->is_admin
49+
? Response::allow()
50+
: Response::deny('You do not have permission to delete results.');
4951
}
5052

5153
/**
5254
* Determine whether the user can delete the model.
5355
*/
54-
public function delete(User $user, Result $result): bool
56+
public function delete(User $user, Result $result): Response
5557
{
56-
return $user->is_admin;
58+
return $user->is_admin
59+
? Response::allow()
60+
: Response::deny('You do not have permission to delete this result.');
5761
}
5862

5963
/**
6064
* Determine whether the user can restore the model.
6165
*/
62-
public function restore(User $user, Result $result): bool
66+
public function restore(User $user, Result $result): Response
6367
{
64-
return false; // soft deletes not used
68+
return Response::deny(); // soft deletes not used
6569
}
6670

6771
/**
6872
* Determine whether the user can permanently delete the model.
6973
*/
70-
public function forceDelete(User $user, Result $result): bool
74+
public function forceDelete(User $user, Result $result): Response
7175
{
72-
return false; // soft deletes not used
76+
return Response::deny(); // soft deletes not used
7377
}
7478
}

app/Policies/UserPolicy.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,89 @@
33
namespace App\Policies;
44

55
use App\Models\User;
6+
use Illuminate\Auth\Access\Response;
67

78
class UserPolicy
89
{
910
/**
1011
* Determine whether the user can view any models.
1112
*/
12-
public function viewAny(User $user): bool
13+
public function viewAny(User $user): Response
1314
{
14-
return $user->is_admin;
15+
return $user->is_admin
16+
? Response::allow()
17+
: Response::deny('You do not have permission to view users.');
1518
}
1619

1720
/**
1821
* Determine whether the user can view the model.
1922
*/
20-
public function view(User $user, User $model): bool
23+
public function view(User $user, User $model): Response
2124
{
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.');
2328
}
2429

2530
/**
2631
* Determine whether the user can create models.
2732
*/
28-
public function create(User $user): bool
33+
public function create(User $user): Response
2934
{
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.');
3138
}
3239

3340
/**
3441
* Determine whether the user can update the model.
3542
*/
36-
public function update(User $user, User $model): bool
43+
public function update(User $user, User $model): Response
3744
{
38-
if ($user->id == $model->id) {
39-
return true;
45+
if ($model->is($user)) {
46+
return Response::deny('You cannot update your own account.');
4047
}
4148

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.');
4352
}
4453

4554
/**
4655
* Determine whether the user can bulk delete any model.
4756
*/
48-
public function deleteAny(User $user): bool
57+
public function deleteAny(User $user): Response
4958
{
50-
return false;
59+
return Response::deny('You do not have permission to delete users.');
5160
}
5261

5362
/**
5463
* Determine whether the user can delete the model.
5564
*/
56-
public function delete(User $user, User $model): bool
65+
public function delete(User $user, User $model): Response
5766
{
67+
if ($model->is_admin) {
68+
return Response::deny('You cannot delete an admin user.');
69+
}
70+
5871
return $user->is_admin
59-
&& ! $model->is_admin;
72+
? Response::allow()
73+
: Response::deny('You do not have permission to delete this user.');
6074
}
6175

6276
/**
6377
* Determine whether the user can restore the model.
6478
*/
65-
public function restore(User $user, User $model): bool
79+
public function restore(User $user, User $model): Response
6680
{
67-
return false; // soft deletes not used
81+
return Response::deny(); // soft deletes not used
6882
}
6983

7084
/**
7185
* Determine whether the user can permanently delete the model.
7286
*/
73-
public function forceDelete(User $user, User $model): bool
87+
public function forceDelete(User $user, User $model): Response
7488
{
75-
return false; // soft deletes not used
89+
return Response::deny(); // soft deletes not used
7690
}
7791
}

0 commit comments

Comments
 (0)