XetaIO /
Xetaravel
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Xetaravel\Policies; |
||
| 6 | |||
| 7 | use Xetaravel\Models\User; |
||
| 8 | use Illuminate\Auth\Access\HandlesAuthorization; |
||
| 9 | |||
| 10 | class UserPolicy |
||
| 11 | { |
||
| 12 | use HandlesAuthorization; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Determine whether the user can update a user. |
||
| 16 | * |
||
| 17 | * @param User $user |
||
| 18 | * @param User|null $model |
||
| 19 | * |
||
| 20 | * @return bool |
||
| 21 | */ |
||
| 22 | public function update(User $user, ?User $model = null): bool |
||
| 23 | { |
||
| 24 | // First check if user can update any user and a user has been provided |
||
| 25 | if ($user->hasPermissionTo('update user') && !is_null($model)) { |
||
| 26 | // Check if the user level is superior or equal to the other user level he wants to edit. |
||
| 27 | return $user->level >= $model->level; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 28 | } |
||
| 29 | |||
| 30 | return $user->hasPermissionTo('update user'); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Determine whether the user can delete a user. |
||
| 35 | * |
||
| 36 | * @param User $user |
||
| 37 | * @param User|null $model |
||
| 38 | * |
||
| 39 | * @return bool |
||
| 40 | */ |
||
| 41 | public function delete(User $user, ?User $model = null): bool |
||
| 42 | { |
||
| 43 | // First check if user can delete any user and a user has been provided |
||
| 44 | if ($user->hasPermissionTo('delete user') && !is_null($model)) { |
||
| 45 | // Check if the user level is superior or equal to the other user level he wants to edit. |
||
| 46 | return $user->level >= $model->level; |
||
|
0 ignored issues
–
show
|
|||
| 47 | } |
||
| 48 | return $user->hasPermissionTo('delete user'); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Determine whether the user can delete a user. |
||
| 53 | * |
||
| 54 | * @param User $user |
||
| 55 | * |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | public function restore(User $user): bool |
||
| 59 | { |
||
| 60 | return $user->hasPermissionTo('restore user'); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Determine whether the user can search in the model. |
||
| 65 | * |
||
| 66 | * @param User $user |
||
| 67 | * |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | public function search(User $user): bool |
||
| 71 | { |
||
| 72 | return $user->hasPermissionTo('search user'); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Determine whether the user can assign direct permission the model. |
||
| 77 | * |
||
| 78 | * @param User $user |
||
| 79 | * |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | public function assignDirectPermission(User $user): bool |
||
| 83 | { |
||
| 84 | return $user->hasPermissionTo('assign-direct-permission user'); |
||
| 85 | } |
||
| 86 | } |
||
| 87 |