src/Security/Voter/Procedure/ProcedureVoter.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter\Procedure;
  4. use App\Model\User\Entity\User\Role\Permission;
  5. use App\Model\User\Entity\User\Role\RoleConstants;
  6. use App\ReadModel\Procedure\InvitedMember\InvitedMemberFetcher;
  7. use App\ReadModel\Procedure\Lot\DetailView;
  8. use App\ReadModel\Profile\ProfileFetcher;
  9. use App\Security\UserIdentity;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  13. use Symfony\Component\Security\Core\Security;
  14. class ProcedureVoter extends Voter
  15. {
  16.     public const CONTRACTS_SHOW_TO_LOT 'contracts_show_to_lot';
  17.     public const PROCEDURE_SHOW 'procedure_show';
  18.     private Security $security;
  19.     private $user;
  20.     /** @var \App\ReadModel\Profile\DetailView */
  21.     private \App\ReadModel\Profile\DetailView $profile;
  22.     private InvitedMemberFetcher $invitedMemberFetcher;
  23.     private ProfileFetcher $profileFetcher;
  24.     public function __construct(Security $securityInvitedMemberFetcher $invitedMemberFetcherProfileFetcher $profileFetcher)
  25.     {
  26.         $this->security $security;
  27.         $this->invitedMemberFetcher $invitedMemberFetcher;
  28.         $this->profileFetcher $profileFetcher;
  29.     }
  30.     protected function supports(string $attribute$subject): bool
  31.     {
  32.         return in_array($attribute, [
  33.             self::CONTRACTS_SHOW_TO_LOT,
  34.             self::PROCEDURE_SHOW
  35.         ], true);
  36.     }
  37.     /**
  38.      * @param string $attribute
  39.      * @param DetailView $subject
  40.      * @param TokenInterface $token
  41.      * @return bool
  42.      */
  43.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  44.     {
  45.         // Moderators have full access
  46.         if ($this->security->isGranted('ROLE_MODERATOR')) {
  47.             return true;
  48.         }
  49.         if ($this->security->isGranted(RoleConstants::auditor()->getValue())) {
  50.             return true;
  51.         }
  52.         if ($this->security->isGranted(RoleConstants::manager()->getValue())) {
  53.             return true;
  54.         }
  55.         $user $token->getUser();
  56.         // Only authenticated UserIdentity can proceed
  57.         if (!$user instanceof UserIdentity) {
  58.             return false;
  59.         }
  60.         $this->profile $this->profileFetcher->find($user->getProfileId());
  61.         $role = (new RoleConstants($user->getRole()));
  62.         if ($role->isOrganizerUser()) {
  63.             if ($this->checkPermissionEmployee($user$attribute) === false) {
  64.                 return $this->handleException();
  65.             }
  66.         }
  67.         //is not owner object
  68.         if ($this->isOwnerObject($subject$user) === false) {
  69.             if ($subject->getStatus()->isNew() or $subject->getStatus()->isArchive()) {
  70.                 return $this->handleException();
  71.             }
  72.         }
  73.         if ($this->checkClosedPurchase($subject) === false) {
  74.             return $this->handleException();
  75.         }
  76.         return true;
  77.     }
  78.     private function checkClosedPurchase(DetailView $lot): bool
  79.     {
  80.         //закрытая закупка не виден не приглашенным
  81.         if ($lot->closed_purchase && $lot->is_hide_closed_purchase) {
  82.             $inn $this->profile->getInn();
  83.             //заказчик
  84.             if ($lot->organizer_profile_id === $this->profile->id) {
  85.                 return true;
  86.             }
  87.             if (!$this->invitedMemberFetcher->isInvitedByInnOrProfile($inn$lot->procedure_id$this->profile->id)) {
  88.                 return false;
  89.             }
  90.         }
  91.         return true;
  92.     }
  93.     /**
  94.      * Проверка разрешений сотрудника
  95.      * @param UserIdentity $user
  96.      * @param string $attribute
  97.      * @return bool
  98.      */
  99.     private function checkPermissionEmployee(UserIdentity $userstring $attribute): bool
  100.     {
  101.         switch ($attribute) {
  102.             case self::CONTRACTS_SHOW_TO_LOT:
  103.                 return $user->isPermission(Permission::CONTRACTS_SHOW_TO_LOT);
  104.                 break;
  105.             case self::PROCEDURE_SHOW:
  106.                 return true;
  107.                 break;
  108.         }
  109.         return false;
  110.     }
  111.     private function isOwnerObject(DetailView $subjectUserIdentity $user): bool
  112.     {
  113.         if ($subject->organizer_profile_id === $user->getProfileId()) {
  114.             return true;
  115.         }
  116.         return false;
  117.     }
  118.     private function handleException()
  119.     {
  120.         throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
  121.     }
  122. }