src/Security/ProjectViewVoter.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * docustack
  5.  *
  6.  * @copyright  Copyright (c) 2014-2022, 47GradNord - Agentur für Internetlösungen
  7.  * @author     Holger Neuner <neuner@47gradnord.de>
  8.  */
  9. namespace App\Security;
  10. use App\Entity\DocumentationProject;
  11. use App\Entity\User;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. class ProjectViewVoter extends Voter
  15. {
  16.     const VIEW 'view';
  17.     protected function supports(string $attribute$subject)
  18.     {
  19.         // if the attribute isn't one we support, return false
  20.         if (!\in_array($attribute, [self::VIEW], true)) {
  21.             return false;
  22.         }
  23.         // only vote on `DocumentationProject` objects
  24.         if (!$subject instanceof DocumentationProject) {
  25.             return false;
  26.         }
  27.         return true;
  28.     }
  29.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  30.     {
  31.         $user $token->getUser();
  32.         if (!$user instanceof User) {
  33.             // the user must be logged in; if not, deny access
  34.             return false;
  35.         }
  36.         /** @var DocumentationProject $projectView */
  37.         $projectView $subject;
  38.         switch ($attribute) {
  39.             case self::VIEW:
  40.                 return $this->canView($projectView$user);
  41.         }
  42.         throw new \LogicException('This code should not be reached!');
  43.     }
  44.     protected function canView(DocumentationProject $projectUser $user)
  45.     {
  46.         // Lets look if User has same Organisation as the DocumentationProject
  47.         if ($user->getOrganisation() === $project->getOrganisation()) {
  48.             return true;
  49.         }
  50.         return false;
  51.     }
  52. }