src/Security/Voters/AttachmentVoter.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\Allegato;
  4. use App\Entity\CPSUser;
  5. use App\Entity\OperatoreUser;
  6. use App\Entity\Pratica;
  7. use App\Entity\RichiestaIntegrazione;
  8. use App\Entity\RispostaOperatore;
  9. use App\Entity\Servizio;
  10. use App\Entity\User;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. use Symfony\Component\Security\Core\Security;
  15. class AttachmentVoter extends Voter
  16. {
  17.   const DOWNLOAD 'download';
  18.   const EDIT 'edit';
  19.   const UPLOAD 'upload';
  20.   const DELETE 'delete';
  21.   /** @var Security  */
  22.   private $security;
  23.   /** @var SessionInterface */
  24.   private $session;
  25.   private $hashValidity;
  26.   /**
  27.    * AttachmentVoter constructor.
  28.    * @param Security $security
  29.    * @param SessionInterface $session
  30.    * @param $hashValidity
  31.    */
  32.   public function __construct(Security $securitySessionInterface $session$hashValidity)
  33.   {
  34.     $this->security $security;
  35.     $this->session $session;
  36.     $this->hashValidity $hashValidity;
  37.   }
  38.   /**
  39.    * @param string $attribute
  40.    * @param mixed $subject
  41.    * @return bool
  42.    */
  43.   protected function supports($attribute$subject)
  44.   {
  45.     if (!in_array($attribute, [
  46.       self::DOWNLOAD,
  47.       self::EDIT,
  48.       self::UPLOAD,
  49.       self::DELETE
  50.     ])) {
  51.       return false;
  52.     }
  53.     if ($subject && !$subject instanceof Allegato) {
  54.       return false;
  55.     }
  56.     return true;
  57.   }
  58.   protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  59.   {
  60.     $user $token->getUser();
  61.     if (!$user instanceof User) {
  62.       // the user must be logged in; if not, deny access
  63.       return false;
  64.     }
  65.     // you know $subject is a Pratica object, thanks to `supports()`
  66.     /** @var Allegato $attachment */
  67.     $attachment $subject;
  68.     switch ($attribute) {
  69.       case self::UPLOAD:
  70.       case self::EDIT:
  71.         return $this->canEdit($attachment$user);
  72.       case self::DOWNLOAD:
  73.         return $this->canDownload($attachment$user);
  74.       case self::DELETE:
  75.         return $this->canDelete($attachment$user);
  76.     }
  77.     throw new \LogicException('This code should not be reached!');
  78.   }
  79.   /**
  80.    * @param Allegato $attachment
  81.    * @param User $user
  82.    * @return bool
  83.    */
  84.   private function canDownload(Allegato $attachmentUser $user)
  85.   {
  86.     if ($this->security->isGranted('ROLE_ADMIN')) {
  87.       return true;
  88.     }
  89.     if ($this->security->isGranted('ROLE_OPERATORE')) {
  90.       foreach ($attachment->getPratiche() as $pratica) {
  91.         $isOperatoreEnabled in_array($pratica->getServizio()->getId(), $user->getServiziAbilitati()->toArray());
  92.         if ($pratica->getOperatore() === $user || $isOperatoreEnabled) {
  93.           return true;
  94.         }
  95.       }
  96.       if ($attachment instanceof RichiestaIntegrazione) {
  97.         $pratica $attachment->getPratica();
  98.         $isOperatoreEnabled in_array($pratica->getServizio()->getId(), $user->getServiziAbilitati()->toArray());
  99.         if ($pratica->getOperatore() === $user || $isOperatoreEnabled) {
  100.           return true;
  101.         }
  102.       }
  103.       // Fixme: permetto sempre il download della risposta da parte degli operatori. Da sistemare una volta riorganizzati gli allegati
  104.       if ($attachment instanceof RispostaOperatore) {
  105.         return true;
  106.       }
  107.     }
  108.     if ($attachment->getOwner() === $user) {
  109.       return true;
  110.     }
  111.     $canDownload false;
  112.     $pratica $attachment->getPratiche()->first();
  113.     if ($pratica instanceof Pratica) {
  114.       if ($user instanceof CPSUser) {
  115.         $relatedCFs $pratica->getRelatedCFs();
  116.         $canDownload = (is_array($relatedCFs) && in_array($user->getCodiceFiscale(), $relatedCFs ) || $pratica->getUser() == $user);
  117.       } elseif ($this->session->isStarted() && $this->session->has(Pratica::HASH_SESSION_KEY)) {
  118.         $canDownload $pratica->isValidHash($this->session->get(Pratica::HASH_SESSION_KEY), $this->hashValidity);
  119.       }
  120.     }
  121.     return $canDownload;
  122.   }
  123.   /**
  124.    * @param Allegato $attachment
  125.    * @param User $user
  126.    * @return bool
  127.    */
  128.   private function canEdit(Allegato $attachmentUser $user)
  129.   {
  130.     if ($this->security->isGranted('ROLE_ADMIN')) {
  131.       return true;
  132.     }
  133.     if ($this->security->isGranted('ROLE_OPERATORE')) {
  134.       return true;
  135.     }
  136.     if ($attachment->getOwner() === $user) {
  137.       return true;
  138.     }
  139.     if ($this->session->isStarted() && $attachment->getHash() === hash('sha256'$this->session->getId())) {
  140.       return true;
  141.     }
  142.     return false;
  143.   }
  144.   /**
  145.    * @param Allegato $attachment
  146.    * @param User $user
  147.    * @return bool
  148.    */
  149.   private function canDelete(Allegato $attachmentUser $user)
  150.   {
  151.     if ($attachment->getOwner() === $user) {
  152.       return true;
  153.     }
  154.     if ($this->session->isStarted() && $attachment->getHash() === hash('sha256'$this->session->getId())) {
  155.       return true;
  156.     }
  157.     return false;
  158.   }
  159. }