src/AppBundle/Security/MatterVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Security;
  3. use AppBundle\Entity\Matter;
  4. use AppBundle\Entity\User;
  5. use SettingsBundle\Enum\ModuleEnum;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. /**
  9.  * Class MatterVoter
  10.  */
  11. class MatterVoter extends Voter
  12. {
  13.     const MATTER_VIEW_DRIVE_TAB 'matter.view_drive_tab';
  14.     /**
  15.      * {@inheritdoc}
  16.      */
  17.     protected function supports($attribute$subject)
  18.     {
  19.         if (!in_array($attribute, [self::MATTER_VIEW_DRIVE_TAB])) {
  20.             return false;
  21.         }
  22.         return $subject instanceof Matter;
  23.     }
  24.     /**
  25.      * {@inheritdoc}
  26.      */
  27.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  28.     {
  29.         $user $token->getUser();
  30.         if (!$user instanceof User) {
  31.             return false;
  32.         }
  33.         /** @var Matter $matter */
  34.         $matter $subject;
  35.         switch ($attribute) {
  36.             case self::MATTER_VIEW_DRIVE_TAB:
  37.                 return $this->canViewDriveTab($matter$user);
  38.             default:
  39.                 throw new \LogicException('This code should not be reached');
  40.         }
  41.     }
  42.     /**
  43.      * Determines whether user can view drive or not
  44.      *
  45.      * @param Matter $matter
  46.      * @param User $user
  47.      * @return boolean
  48.      */
  49.     private function canViewDriveTab(Matter $matterUser $user)
  50.     {
  51.         return $user->hasRole(ModuleEnum::MODULE_DRIVE)
  52.             && $user->getLawFirm()->haveDrive()
  53.             && ($matter->hasMatterUserDriveAccess($user) || $matter->isMatterFolderSharedToLawFirm());
  54.     }
  55. }