src/MentalSchool/AppBundle/Security/Authorization/Voter/GeneralVoter.php line 14

Open in your IDE?
  1. <?php
  2. // src/Security/Authorization/Voter/UserAclVoter.php
  3. namespace MentalSchool\AppBundle\Security\Authorization\Voter;
  4. use MentalSchool\AppBundle\Entity\User;
  5. use MentalSchool\AppBundle\Security\AppRoles;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\DependencyInjection\{
  9.     ContainerInterfaceContainerAwareInterface
  10. };
  11. class GeneralVoter extends Voter implements ContainerAwareInterface
  12. {
  13.     /**
  14.      * @var null|ContainerInterface A ContainerInterface instance.
  15.      */
  16.     protected $container null;
  17.     /**
  18.      * Set container.
  19.      *
  20.      * @param ContainerInterface $container A ContainerInterface instance.
  21.      */
  22.     public function setContainer(ContainerInterface $container null): void
  23.     {
  24.         if (null === $this->container) {
  25.             $this->container $container;
  26.         }
  27.     }
  28.       protected function supports($attribute$subject)
  29.     {
  30.         if(!defined('MentalSchool\AppBundle\Security\AppRoles::'$attribute)){
  31.                 return false;
  32.         }
  33.         $permission constant('MentalSchool\AppBundle\Security\AppRoles::'$attribute);
  34.         // if the attribute isn't one we support, return false
  35.         if (!in_array($permission, [
  36.             AppRoles::ROLE_GENERAL_SHOW_NEWS_LIST,
  37.             AppRoles::ROLE_GENERAL_ADD_NEW_PLACE,
  38.             AppRoles::ROLE_SHOW_INFORMATION_PAGE,
  39.         ])) {
  40.             return false;
  41.         }
  42.         return true;
  43.     }
  44.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  45.     {
  46.         $user $token->getUser();
  47.         if (!$user instanceof User) {
  48.             return false;
  49.         }
  50.         if (count(array_intersect(array('ROLE_SUPER_ADMIN'), $user->getRoles()))) {
  51.             return true;
  52.         }
  53.         $attribute constant('MentalSchool\AppBundle\Security\AppRoles::'$attribute);
  54.         switch ($attribute) {
  55.             case AppRoles::ROLE_GENERAL_SHOW_NEWS_LIST:
  56.                 return $this->canSeeNewsList($user);
  57.             case AppRoles::ROLE_GENERAL_ADD_NEW_PLACE:
  58.                 return $this->canAddNewPlace($user$subject);
  59.             case AppRoles::ROLE_SHOW_INFORMATION_PAGE:
  60.                 return $this->canShowInformationPage($user);
  61.         }
  62.         return false;
  63.     }
  64.     /**
  65.      * Checks whether user is granted to see news .
  66.      *
  67.      * @param User $loggedUser An User instance.
  68.      *
  69.      * @return boolean
  70.      */
  71.     private function canSeeNewsList(User $loggedUser): bool
  72.     {
  73.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  74.             return true;
  75.         }
  76.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  77.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_GENERAL_SHOW_NEWS_LIST);
  78.         }
  79.         return false;
  80.     }
  81.     /**
  82.      * Checks whether user is granted to add palace
  83.      *
  84.      * @param User $loggedUser An User instance.
  85.      *
  86.      * @return boolean
  87.      */
  88.     private function canAddNewPlace(User $loggedUser): bool
  89.     {
  90.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  91.             return true;
  92.         }
  93.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  94.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_GENERAL_ADD_NEW_PLACE);
  95.         }
  96.         return false;
  97.     }
  98.     /**
  99.      * Checks whether user is granted to show information page
  100.      *
  101.      * @param User $loggedUser An User instance.
  102.      *
  103.      * @return boolean
  104.      */
  105.     private function canShowInformationPage(User $loggedUser): bool
  106.     {
  107.         $em $this->container->get('doctrine')->getManager();
  108.         if ($em->getRepository('AppBundle:InformationPage')->hasAccessToInformationPage($loggedUser->getSchool()->getId())){
  109.             if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  110.                 return true;
  111.             }
  112.             if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  113.                 return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_SHOW_INFORMATION_PAGE);
  114.             }
  115.         }
  116.         return false;
  117.     }
  118. }