HasAuthorization.php 773 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Dcat\Admin\Traits;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Models\HasPermissions;
  5. use Illuminate\Contracts\Auth\Authenticatable;
  6. use Illuminate\Database\Eloquent\Model;
  7. trait HasAuthorization
  8. {
  9. /**
  10. * 验证权限.
  11. *
  12. * @return bool
  13. */
  14. public function passesAuthorization(): bool
  15. {
  16. return $this->authorize(Admin::user());
  17. }
  18. /**
  19. * 是否有权限判断.
  20. *
  21. * @param Model|Authenticatable|HasPermissions|null $user
  22. *
  23. * @return bool
  24. */
  25. protected function authorize($user): bool
  26. {
  27. return true;
  28. }
  29. /**
  30. * 返回无权限信息.
  31. *
  32. * @return mixed
  33. */
  34. public function failedAuthorization()
  35. {
  36. abort(403, __('admin.deny'));
  37. }
  38. }