src/MentalSchool/AppBundle/Security/Authorization/Voter/SchoolGroupVoter.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 SchoolGroupVoter 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_GROUP_ADD,
  37.             AppRoles::ROLE_GROUP_EDIT,
  38.             AppRoles::ROLE_GROUP_LIST_ANOTHER_TEACHERS_GROUPS,
  39.             AppRoles::ROLE_GROUP_DELETE,
  40.             AppRoles::ROLE_GROUP_TASK_ASSIGNED,
  41.             AppRoles::ROLE_GROUP_TASK_HISTORY
  42.         ])) {
  43.             return false;
  44.         }
  45.         return true;
  46.     }
  47.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  48.     {
  49.         $user $token->getUser();
  50.         if (!$user instanceof User) {
  51.             return false;
  52.         }
  53.         if (count(array_intersect(array('ROLE_SUPER_ADMIN'), $user->getRoles()))) {
  54.             return true;
  55.         }
  56.         $attribute constant('MentalSchool\AppBundle\Security\AppRoles::'$attribute);
  57.         switch ($attribute) {
  58.             case AppRoles::ROLE_GROUP_ADD:
  59.                 return $this->canCreate($user);
  60.             case AppRoles::ROLE_GROUP_EDIT:
  61.                 return $this->canEdit($user$subject);
  62.             case AppRoles::ROLE_GROUP_LIST_ANOTHER_TEACHERS_GROUPS:
  63.             return $this->canListAnotherGroups($user);
  64.             case AppRoles::ROLE_GROUP_DELETE:
  65.             return $this->canDelete($user);
  66.             case AppRoles::ROLE_GROUP_TASK_ASSIGNED:
  67.                 return $this->canAssignTask($user);
  68.             case AppRoles::ROLE_GROUP_TASK_HISTORY:
  69.                 return $this->canSeeTaskHistory($user);
  70.         }
  71.         return false;
  72.     }
  73.     /**
  74.      * Checks whether user is granted to create a new student.
  75.      *
  76.      * @param User $loggedUser An User instance.
  77.      *
  78.      * @return boolean
  79.      */
  80.     private function canCreate(User $loggedUser): bool
  81.     {
  82.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  83.             return true;
  84.         }
  85.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  86.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_GROUP_ADD);
  87.         }
  88.         return false;
  89.     }
  90.     /**
  91.      * Checks whether user is granted to edit student.
  92.      *
  93.      * @param User $loggedUser An User instance.
  94.      *
  95.      * @return boolean
  96.      */
  97.     private function canEdit(User $loggedUser$subject): bool
  98.     {
  99.         if($loggedUser->getSchool() !=  $subject->getSchool()){
  100.             return false;
  101.         }
  102.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  103.             return true;
  104.         }
  105.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  106.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_GROUP_EDIT);
  107.         }
  108.         return false;
  109.     }
  110.     /**
  111.      * Checks whether user is granted to review  student's list
  112.      *
  113.      * @param User $loggedUser An User instance.
  114.      *
  115.      * @return boolean
  116.      */
  117.     private function canListAnotherGroups(User $loggedUser): bool
  118.     {
  119.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  120.             return true;
  121.         }
  122.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  123.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_GROUP_LIST_ANOTHER_TEACHERS_GROUPS);
  124.         }
  125.         return false;
  126.     }
  127.     /**
  128.      * Checks whether user is granted to  delete group
  129.      *
  130.      * @param User $loggedUser An User instance.
  131.      *
  132.      * @return boolean
  133.      */
  134.     private function canDelete(User $loggedUser): bool
  135.     {
  136.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  137.             return true;
  138.         }
  139.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  140.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_GROUP_DELETE);
  141.         }
  142.         return false;
  143.     }
  144.     /**
  145.      * Checks whether user has access get task to student
  146.      *
  147.      * @param User $loggedUser An User instance.
  148.      *
  149.      * @return boolean
  150.      */
  151.     private function canAssignTask(User $loggedUser): bool
  152.     {
  153.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  154.             return true;
  155.         }
  156.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  157.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_GROUP_TASK_ASSIGNED);
  158.         }
  159.         return false;
  160.     }
  161.     /**
  162.      * Checks whether user has access see task history to student
  163.      *
  164.      * @param User $loggedUser An User instance.
  165.      *
  166.      * @return boolean
  167.      */
  168.     private function canSeeTaskHistory(User $loggedUser): bool
  169.     {
  170.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  171.             return true;
  172.         }
  173.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  174.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_GROUP_TASK_HISTORY);
  175.         }
  176.         return false;
  177.     }
  178. }