ecommerce121/util-bundle/Controller/ControllerBase.php line 463

  1. {
  2. return $this->controllerUtil->getValidator();
  3. }
  4. protected function getEventDispatcher(): EventDispatcherInterface
  5. {
  6. return $this->controllerUtil->getEventDispatcher();
  7. }
  8. protected function dispatchEvent(string $eventName, ?object $event = null): void
  9. {
  10. $this->controllerUtil->getEventDispatcher()->dispatch($event, $eventName);
  11. }
  12. protected function getControllerUtil(): ControllerUtil
  13. {
  14. return $this->controllerUtil;
  15. }
  16. protected function getTokenStorage(): TokenStorageInterface
  17. {
  18. return $this->controllerUtil->getTokenStorage();
  19. }
  20. protected function getKernelRootDir(): string
  21. {
  22. return $this->controllerUtil->getKernelRootDir();
  23. }
  24. protected function getUploadsPath(): string
  25. {
  26. return $this->getKernelRootDir().'/publc/uploads/';
  27. }
  28. protected function jsonResult(array $data = []): JsonResponse
  29. {
  30. return $this->json([
  31. 'hasError' => false,
  32. 'result' => $data
  33. ]);
  34. }
  35. protected function jsonError($message = '', $code = -1): JsonResponse
  36. {
  37. $data = [
  38. 'hasError' => true,
  39. 'error' => [
  40. 'message' => $message,
  41. 'code' => $code
  42. ]
  43. ];
  44. return $this->json($data);
  45. }
  46. public function denyAccessUnlessGranted(UserRoles|string|UserPermission $attribute, mixed $subject = null, string $message = 'Access Denied.'): void
  47. {
  48. $this->denyAccessUnlessGrantedCheck($attribute, $subject, $message);
  49. }
  50. protected function denyAccessUnlessGrantedCheck($attribute, $subject = null, string $message = 'Access Denied.'): void
  51. {
  52. if (!$this->isGranted($attribute, $subject)) {
  53. $exception = $this->createAccessDeniedException($message);
  54. $exception->setAttributes([$attribute]);
  55. $exception->setSubject($subject);
  56. throw $exception;
  57. }
  58. }
  59. public function getAdmin(): ?User
  60. {
  61. $user = $this->getUser();
  62. if (null === $user) {
  63. return null;
  64. }
  65. if (!($user instanceof User)) {
  66. throw new AuthenticationException('User must be of User type');
  67. }
  68. return $user;
  69. }
  70. protected function isGranted(string|UserRoles|UserPermission $attribute, mixed $subject = null): bool
  71. {
  72. return $this->getControllerUtil()->getAuthorizationChecker()->isGranted($attribute, $subject);
  73. }
  74. }