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.         $user $token->getUser();
  53.         // Only authenticated UserIdentity can proceed
  54.         if (!$user instanceof UserIdentity) {
  55.             return false;
  56.         }
  57.         $this->profile $this->profileFetcher->find($user->getProfileId());
  58.         $role = (new RoleConstants($user->getRole()));
  59.         if ($role->isOrganizerUser()) {
  60.             if ($this->checkPermissionEmployee($user$attribute) === false) {
  61.                 return $this->handleException();
  62.             }
  63.         }
  64.         //is not owner object
  65.         if ($this->isOwnerObject($subject$user) === false) {
  66.             if ($subject->getStatus()->isNew() or $subject->getStatus()->isArchive()) {
  67.                 return $this->handleException();
  68.             }
  69.         }
  70.         if ($this->checkClosedPurchase($subject) === false) {
  71.             return $this->handleException();
  72.         }
  73.         return true;
  74.     }
  75.     private function checkClosedPurchase(DetailView $lot): bool
  76.     {
  77.         //закрытая закупка не виден не приглашенным
  78.         if ($lot->closed_purchase && $lot->is_hide_closed_purchase) {
  79.             $inn $this->profile->getInn();
  80.             //заказчик
  81.             if ($lot->organizer_profile_id === $this->profile->id) {
  82.                 return true;
  83.             }
  84.             if (!$this->invitedMemberFetcher->isInvitedByInnOrProfile($inn$lot->procedure_id$this->profile->id)) {
  85.                 return false;
  86.             }
  87.         }
  88.         return true;
  89.     }
  90.     /**
  91.      * Проверка разрешений сотрудника
  92.      * @param UserIdentity $user
  93.      * @param string $attribute
  94.      * @return bool
  95.      */
  96.     private function checkPermissionEmployee(UserIdentity $userstring $attribute): bool
  97.     {
  98.         switch ($attribute) {
  99.             case self::CONTRACTS_SHOW_TO_LOT:
  100.                 return $user->isPermission(Permission::CONTRACTS_SHOW_TO_LOT);
  101.                 break;
  102.             case self::PROCEDURE_SHOW:
  103.                 return true;
  104.                 break;
  105.         }
  106.         return false;
  107.     }
  108.     private function isOwnerObject(DetailView $subjectUserIdentity $user): bool
  109.     {
  110.         if ($subject->organizer_profile_id === $user->getProfileId()) {
  111.             return true;
  112.         }
  113.         return false;
  114.     }
  115.     private function handleException()
  116.     {
  117.         throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
  118.     }
  119. }