vendor/crea/parameter-bundle/src/Voter/ParameterVoter.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Crea\ParameterBundle\Voter;
  4. use Crea\ParameterBundle\Entity\Parameter;
  5. use Crea\ParameterBundle\Entity\ParameterType;
  6. use Crea\ParameterBundle\Provider\ParameterRightCodeProvider;
  7. use Exception;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. /**
  12. * @extends Voter<string, Parameter>
  13. */
  14. class ParameterVoter extends Voter
  15. {
  16. protected ParameterRightCodeProvider $parameterRightCodeProvider;
  17. public function __construct(ParameterRightCodeProvider $parameterRightCodeProvider)
  18. {
  19. $this->parameterRightCodeProvider = $parameterRightCodeProvider;
  20. }
  21. /**
  22. * @throws Exception
  23. */
  24. protected function supports($attribute, $subject): bool
  25. {
  26. $chapterAttributes = [];
  27. if ($subject instanceof ParameterType) {
  28. $chapterAttributes[] = $this->parameterRightCodeProvider->getParameterViewRightCode($subject);
  29. $chapterAttributes[] = $this->parameterRightCodeProvider->getParameterCreateRightCode($subject);
  30. }
  31. if ($subject instanceof Parameter) {
  32. $chapterAttributes[] = $this->parameterRightCodeProvider->getParameterUpdateRightCode($subject);
  33. $chapterAttributes[] = $this->parameterRightCodeProvider->getParameterRemoveRightCode($subject);
  34. }
  35. return in_array($attribute, $chapterAttributes);
  36. }
  37. /**
  38. * @throws Exception
  39. */
  40. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  41. {
  42. /** @var UserInterface $loggedUser */
  43. $loggedUser = $token->getUser();
  44. if (!$loggedUser instanceof UserInterface) {
  45. return false;
  46. }
  47. if ($subject instanceof ParameterType) {
  48. switch ($attribute) {
  49. case $this->parameterRightCodeProvider->getParameterViewRightCode($subject):
  50. return $this->voteOnParameterView($subject, $loggedUser);
  51. case $this->parameterRightCodeProvider->getParameterCreateRightCode($subject):
  52. return $this->voteOnParameterCreate($subject, $loggedUser);
  53. }
  54. }
  55. if ($subject instanceof Parameter) {
  56. switch ($attribute) {
  57. case $this->parameterRightCodeProvider->getParameterUpdateRightCode($subject):
  58. return $this->voteOnParameterUpdate($subject, $loggedUser);
  59. case $this->parameterRightCodeProvider->getParameterRemoveRightCode($subject):
  60. return $this->voteOnParameterRemove($subject, $loggedUser);
  61. }
  62. }
  63. return false;
  64. }
  65. private function voteOnParameterView(ParameterType $parameterType, UserInterface $loggedUser): bool
  66. {
  67. return in_array($this->parameterRightCodeProvider->getParameterViewRightCode($parameterType), $loggedUser->getRoles());
  68. }
  69. private function voteOnParameterCreate(ParameterType $parameterType, UserInterface $loggedUser): bool
  70. {
  71. return in_array($this->parameterRightCodeProvider->getParameterCreateRightCode($parameterType), $loggedUser->getRoles());
  72. }
  73. /**
  74. * @throws Exception
  75. */
  76. private function voteOnParameterUpdate(Parameter $parameter, UserInterface $loggedUser): bool
  77. {
  78. return in_array($this->parameterRightCodeProvider->getParameterUpdateRightCode($parameter), $loggedUser->getRoles());
  79. }
  80. /**
  81. * @throws Exception
  82. */
  83. private function voteOnParameterRemove(Parameter $parameter, UserInterface $loggedUser): bool
  84. {
  85. return in_array($this->parameterRightCodeProvider->getParameterRemoveRightCode($parameter), $loggedUser->getRoles());
  86. }
  87. }