src/AppBundle/Security/UsersConfinedVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Security;
  3. use AppBundle\Entity\Matter;
  4. use AppBundle\Entity\User;
  5. use AppBundle\Enum\FeatureAccessEnum;
  6. use CommonBundle\Model\UsersConfinedInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. /**
  10.  * Class UsersConfinedVoter
  11.  */
  12. class UsersConfinedVoter extends Voter
  13. {
  14.     /**
  15.      * {@inheritdoc}
  16.      */
  17.     protected function supports($attribute$subject)
  18.     {
  19.         return $subject instanceof UsersConfinedInterface;
  20.     }
  21.     /**
  22.      * {@inheritdoc}
  23.      */
  24.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  25.     {
  26.         $user $token->getUser();
  27.         if (!$user instanceof User) {
  28.             return false;
  29.         }
  30.         // todo: find better way to handle rights with this voter
  31.         if ($subject instanceof Matter && $user->hasRole(FeatureAccessEnum::ALL_MATTER)) {
  32.             return true;
  33.         }
  34.         /** @var UsersConfinedInterface $subject */
  35.         foreach ($subject->getUsersWithRights() as $usersWithRight) {
  36.             if ($user->getId() === $usersWithRight->getId()) {
  37.                 return true;
  38.             }
  39.         }
  40.         return false;
  41.     }
  42. }