Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
41.18% |
14 / 34 |
|
36.36% |
4 / 11 |
CRAP | |
0.00% |
0 / 1 |
| VotePolicy | |
41.18% |
14 / 34 |
|
36.36% |
4 / 11 |
110.76 | |
0.00% |
0 / 1 |
| viewAny | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| view | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| delete | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
5.26 | |||
| restore | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| forceDelete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| clearUserVotes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| clearAllVotes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| voteOnJoke | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
| removeVoteFromJoke | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Policies; |
| 4 | |
| 5 | use App\Models\Vote; |
| 6 | use App\Models\User; |
| 7 | |
| 8 | class VotePolicy |
| 9 | { |
| 10 | /** |
| 11 | * Determine whether the user can view any models. |
| 12 | * User level (100) and above can view votes |
| 13 | */ |
| 14 | public function viewAny(User $user): bool |
| 15 | { |
| 16 | return $user->hasRole(['client', 'staff', 'admin', 'superuser']); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Determine whether the user can view the model. |
| 21 | * User level (100) and above can view votes |
| 22 | */ |
| 23 | public function view(User $user, Vote $vote): bool |
| 24 | { |
| 25 | return $user->hasRole(['client', 'staff', 'admin', 'superuser']); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Determine whether the user can create models. |
| 30 | * User level (100) and above can create votes |
| 31 | */ |
| 32 | public function create(User $user): bool |
| 33 | { |
| 34 | return $user->hasRole(['client', 'staff', 'admin', 'superuser']); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Determine whether the user can update the model. |
| 39 | * Users can update their own votes, Staff+ can update any vote |
| 40 | */ |
| 41 | public function update(User $user, Vote $vote): bool |
| 42 | { |
| 43 | // Admin and Superuser can update any vote |
| 44 | if ($user->hasRole(['admin', 'superuser'])) { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | // Staff can update any vote |
| 49 | if ($user->hasRole('staff')) { |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | // Clients can only update their own votes |
| 54 | if ($user->hasRole('client')) { |
| 55 | return $vote->user_id === $user->id; |
| 56 | } |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Determine whether the user can delete the model. |
| 63 | * Users can delete their own votes, Staff+ can delete any vote |
| 64 | */ |
| 65 | public function delete(User $user, Vote $vote): bool |
| 66 | { |
| 67 | // Admin and Superuser can delete any vote |
| 68 | if ($user->hasRole(['admin', 'superuser'])) { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | // Staff can delete any vote |
| 73 | if ($user->hasRole('staff')) { |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | // Clients can only delete their own votes |
| 78 | if ($user->hasRole('client')) { |
| 79 | return $vote->user_id === $user->id; |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Determine whether the user can restore the model. |
| 87 | * Staff level (500) and above can restore votes |
| 88 | */ |
| 89 | public function restore(User $user, Vote $vote): bool |
| 90 | { |
| 91 | return $user->hasRole(['staff', 'admin', 'superuser']); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Determine whether the user can permanently delete the model. |
| 96 | * Admin level (750) and above can permanently delete votes |
| 97 | */ |
| 98 | public function forceDelete(User $user, Vote $vote): bool |
| 99 | { |
| 100 | return $user->hasRole(['admin', 'superuser']); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Determine whether the user can clear votes for a specific user. |
| 105 | * Staff level (500) and above can clear user votes |
| 106 | */ |
| 107 | public function clearUserVotes(User $user, $targetUser): bool |
| 108 | { |
| 109 | return $user->hasRole(['staff', 'admin', 'superuser']); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Determine whether the user can clear all votes in the system. |
| 114 | * Admin level (750) and above can clear all votes |
| 115 | */ |
| 116 | public function clearAllVotes(User $user): bool |
| 117 | { |
| 118 | return $user->hasRole(['admin', 'superuser']); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Determine whether the user can vote on a specific joke. |
| 123 | * User level (100) and above can vote on jokes with valid categories |
| 124 | */ |
| 125 | public function voteOnJoke(User $user, $joke): bool |
| 126 | { |
| 127 | // All authenticated users can vote |
| 128 | if (!$user->hasRole(['client', 'staff', 'admin', 'superuser'])) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // For regular clients, check if joke has valid categories |
| 133 | if ($user->hasRole('client')) { |
| 134 | // Load categories if not already loaded |
| 135 | if (!$joke->relationLoaded('categories')) { |
| 136 | $joke->load('categories'); |
| 137 | } |
| 138 | // Check if joke has at least one non-deleted category |
| 139 | return $joke->categories->where('deleted_at', null)->isNotEmpty(); |
| 140 | } |
| 141 | |
| 142 | // Staff+ can vote on any joke |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Determine whether the user can remove a vote from a specific joke. |
| 148 | * Users can remove their own votes, Staff+ can remove any vote |
| 149 | */ |
| 150 | public function removeVoteFromJoke(User $user, $joke): bool |
| 151 | { |
| 152 | // Check if user has a vote on this joke |
| 153 | $vote = Vote::where('user_id', $user->id) |
| 154 | ->where('joke_id', $joke->id) |
| 155 | ->first(); |
| 156 | |
| 157 | if (!$vote) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | // Use the delete permission logic |
| 162 | return $this->delete($user, $vote); |
| 163 | } |
| 164 | } |