src/Security/Voter/Voter/ReportSpecVoter.php line 14

Open in your IDE?
  1. <?php
  2. /* @author <storageprocedure@gmail.com> */
  3. declare(strict_types=1);
  4. namespace App\Security\Voter\Voter;
  5. use App\Entity\ReportSpec;
  6. use App\Entity\User;
  7. use App\Security\Voter\AbstractVoter;
  8. use App\Service\InstituteService;
  9. use App\Service\Interfaces\ReportStatusInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. class ReportSpecVoter extends AbstractVoter
  12. {
  13.     private InstituteService $instituteService;
  14.     /**
  15.      * @param InstituteService $instituteService
  16.      */
  17.     public function __construct(InstituteService $instituteService)
  18.     {
  19.         $this->instituteService $instituteService;
  20.     }
  21.     /**
  22.      * @param string $attribute
  23.      * @param $subject
  24.      * @return bool
  25.      */
  26.     protected function supports(string $attribute$subject): bool
  27.     {
  28.         $cond1 $subject instanceof ReportSpec;
  29.         $cond2 in_array($attribute, [self::VIEWself::EDITself::DELETE]);
  30.         return $cond1 && $cond2;
  31.     }
  32.     /**
  33.      * @param string $attribute
  34.      * @param $subject
  35.      * @param TokenInterface $token
  36.      * @return bool
  37.      */
  38.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  39.     {
  40.         $user $token->getUser();
  41.         if (!$user instanceof User) {
  42.             return false;
  43.         }
  44.         if ($user->hasRole(User::ROLE_ADMIN)) {
  45.             return true;
  46.         }
  47.         switch ($attribute) {
  48.             case self::VIEW:
  49.                 return $this->canView($subject$user);
  50.             case self::EDIT:
  51.                 return $this->canEdit($subject$user);
  52.             case self::DELETE:
  53.                 return $this->canDelete($subject$user);
  54.         }
  55.         return false;
  56.     }
  57.     /**
  58.      * Просматривать отчет и его детализацию может тот, у кого есть права на доступ к данному учреджению.
  59.      * @param ReportSpec $reportSpec
  60.      * @param User $user
  61.      * @return bool
  62.      */
  63.     private function canView(ReportSpec $reportSpecUser $user): bool
  64.     {
  65.         $institute $reportSpec->getInstitute();
  66.         return $this->instituteService->isInstituteAllow($institute$user);
  67.     }
  68.     /**
  69.      * Редактировать отчет может только его создатель и только если отчет - черновик.
  70.      * @param ReportSpec $reportSpec
  71.      * @param User $user
  72.      * @return bool
  73.      */
  74.     private function canEdit(ReportSpec $reportSpecUser $user): bool
  75.     {
  76.         $cond1 $this->isOwner($reportSpec$user);
  77.         $cond2 $reportSpec->getStatus() === ReportStatusInterface::STATUS_DRAFT;
  78.         return $cond1 && $cond2;
  79.     }
  80.     /**
  81.      * Удалять отчет может только его создатель и только если отчет - черновик.
  82.      * @param ReportSpec $reportSpec
  83.      * @param User $user
  84.      * @return bool
  85.      */
  86.     private function canDelete(ReportSpec $reportSpecUser $user): bool
  87.     {
  88.         $cond1 $this->isOwner($reportSpec$user);
  89.         $cond2 $reportSpec->getStatus() === ReportStatusInterface::STATUS_DRAFT;
  90.         return $cond1 && $cond2;
  91.     }
  92. }