src/System/Voter/User/UserRightVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\System\Voter\User;
  3. use App\Entities\Rights\Right;
  4. use App\Entities\Rights\Rights;
  5. use App\Entities\User\UserAccess;
  6. use App\Entities\User\UserInterface;
  7. use App\UseCase\User\Read\Access\Integration\IsMainSystemIntegrationHandler;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. class UserRightVoter extends Voter
  11. {
  12.     public function __construct(
  13.         private readonly IsMainSystemIntegrationHandler $isMainSystemIntegrationHandler,
  14.     ) {
  15.     }
  16.     protected function supports(string $attributemixed $subject): bool
  17.     {
  18.         return in_array(
  19.             $attribute,
  20.             [
  21.                 Rights::ServicesView->value,
  22.                 Rights::ServicesViewZpModel->value,
  23.                 Rights::ServicesEdit->value,
  24.                 Rights::FinAdminView->value,
  25.                 Rights::FinAdminViewZpModel->value,
  26.                 Rights::FinAdminEditPercent->value,
  27.                 Rights::FinAdminEditFine->value,
  28.                 Rights::FinAdminEditBonus->value,
  29.                 Rights::PayoutView->value,
  30.                 Rights::PayoutEdit->value,
  31.                 Rights::MopView->value,
  32.                 Rights::MopViewModelFio->value,
  33.                 Rights::MopViewOperatorFio->value,
  34.                 Rights::MopNotViewModelAndOperatorFio->value,
  35.                 Rights::OperatorView->value,
  36.                 Rights::OperatorViewZpOperator->value,
  37.                 Rights::OperatorEditBonus->value,
  38.                 Rights::OperatorEditFine->value,
  39.                 Rights::OperatorEditTotal->value,
  40.                 Rights::OperatorEditPercent->value,
  41.                 Rights::OperatorViewModelFio->value,
  42.                 Rights::MainSystemIntegration->value,
  43.             ],
  44.             true
  45.         );
  46.     }
  47.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  48.     {
  49.         $user $token->getUser();
  50.         if (!$user instanceof UserInterface) {
  51.             return false;
  52.         }
  53.         if ($user->isAdmin()) {
  54.             return true;
  55.         }
  56.         if ($this->isMainSystemIntegrationHandler->__invoke($user) && $attribute === Rights::MainSystemIntegration->value) {
  57.             return true;
  58.         }
  59.         /** @var UserAccess $user */
  60.         if (!$rights $user->getRights()) {
  61.             return false;
  62.         }
  63.         $rights $rights->toArray();
  64.         return match ($attribute) {
  65.             Rights::MopView->value =>
  66.                 $this->checkRight($rightsRights::MopViewModelFio->value)
  67.                 || $this->checkRight($rightsRights::MopViewOperatorFio->value)
  68.                 || $this->checkRight($rightsRights::MopNotViewModelAndOperatorFio->value),
  69.             Rights::MopViewModelFio->value =>
  70.                 $this->checkRight($rightsRights::MopViewModelFio->value)
  71.                 && !$this->checkRight($rightsRights::MopNotViewModelAndOperatorFio->value),
  72.             Rights::MopViewOperatorFio->value =>
  73.                 $this->checkRight($rightsRights::MopViewOperatorFio->value)
  74.                 && !$this->checkRight($rightsRights::MopNotViewModelAndOperatorFio->value),
  75.             default => $this->checkRight($rights$attribute),
  76.         };
  77.     }
  78.     /**
  79.      * @param array<Right> $rights
  80.      * @param string             $rightCode
  81.      *
  82.      * @return bool
  83.      */
  84.     private function checkRight(array $rightsstring $rightCode): bool
  85.     {
  86.         return !empty(array_filter($rights, fn(Right $right) => $right->getCode() === $rightCode));
  87.     }
  88. }