You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1016 B

2 years ago
  1. <?php
  2. namespace App\Utilities\Polices;
  3. use App\Documents\PolicyDocument;
  4. use App\Models\ILaravel;
  5. use Illuminate\Database\Eloquent\Model;
  6. trait PolicyConditions
  7. {
  8. private PolicyDocument|null $policy;
  9. private Model|null $model;
  10. protected function auth(): bool
  11. {
  12. return auth()->check();
  13. }
  14. protected function permission($permission): bool
  15. {
  16. return !$this->auth() ? false : auth()->user()->can(['*', $permission]);
  17. }
  18. protected function model($functionName): bool
  19. {
  20. return empty($this->model) ? false : $this->model->{$functionName}();
  21. }
  22. protected function owner(): bool
  23. {
  24. return !$this->auth() || empty($this->model) ? false : $this->model->user_id == auth()->user()->id;
  25. }
  26. protected function group(): bool
  27. {
  28. return !$this->auth() || empty($this->model) ? false : $this->model->group_id == auth()->user()->group_id;
  29. }
  30. protected function guest(): bool
  31. {
  32. return !$this->auth();
  33. }
  34. }