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

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