CategoryPolicy.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Policies;
  11. use App\Repositories\Enums\PermissionEnum;
  12. use App\Repositories\Models\Navigation\Category;
  13. use App\Repositories\Models\Base\User;
  14. use Illuminate\Auth\Access\HandlesAuthorization;
  15. class CategoryPolicy
  16. {
  17. use HandlesAuthorization;
  18. public function viewAny(User $user)
  19. {
  20. if ($this->all($user)) return true;
  21. if ($user->can(PermissionEnum::ROUTE_CATEGORY_VIEW_ANY()->name)) {
  22. return true;
  23. }
  24. return false;
  25. }
  26. public function view(User $user, Category $model)
  27. {
  28. if ($this->all($user)) return true;
  29. if ($user->isOwner($model) && $user->can(PermissionEnum::ROUTE_CATEGORY_VIEW()->name)) {
  30. return true;
  31. }
  32. return false;
  33. }
  34. public function create(User $user)
  35. {
  36. if ($this->all($user)) return true;
  37. if ($user->can(PermissionEnum::ROUTE_CATEGORY_CREATE()->name)) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. public function update(User $user, Category $model)
  43. {
  44. if ($this->all($user)) return true;
  45. if ($user->isOwnerOrganization($model) || $user->isOwner($model) || $user->can(PermissionEnum::ROUTE_CATEGORY_UPDATE()->name)) {
  46. return true;
  47. }
  48. return false;
  49. }
  50. public function delete(User $user, Category $model)
  51. {
  52. if ($this->all($user)) return true;
  53. if ($user->isOwnerOrganization($model) || $user->isOwner($model) || $user->can(PermissionEnum::ROUTE_CATEGORY_DELETE()->name)) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. private function all(User $user)
  59. {
  60. if ($user->can(PermissionEnum::ROUTE_CATEGORY()->name)) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. }