src/MentalSchool/AppBundle/Security/Authorization/Voter/StudentVoter.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 StudentVoter 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_STUDENT_ADD,
  37.             AppRoles::ROLE_STUDENT_EDIT,
  38.             AppRoles::ROLE_STUDENT_SHOW,
  39.             AppRoles::ROLE_STUDENT_MOVE_TO_TRASH,
  40.             AppRoles::ROLE_STUDENT_LIST,
  41.             AppRoles::ROLE_STUDENT_MAILING,
  42.             AppRoles::ROLE_STUDENT_PAYMENT,
  43.             AppRoles::ROLE_STUDENT_TASK_LIST,
  44.             AppRoles::ROLE_STUDENT_LIST_ANOTHER_STUDENTS,
  45.             AppRoles::ROLE_STUDENT_EDIT_ANOTHER_STUDENTS
  46.         ])) {
  47.             return false;
  48.         }
  49.         return true;
  50.     }
  51.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  52.     {
  53.         $user $token->getUser();
  54.         if (!$user instanceof User) {
  55.             return false;
  56.         }
  57.         if (count(array_intersect(array('ROLE_SUPER_ADMIN'), $user->getRoles()))) {
  58.             return true;
  59.         }
  60.         $attribute constant('MentalSchool\AppBundle\Security\AppRoles::'$attribute);
  61.         switch ($attribute) {
  62.             case AppRoles::ROLE_STUDENT_ADD:
  63.                 return $this->canCreate($user);
  64.             case AppRoles::ROLE_STUDENT_EDIT:
  65.                 return $this->canEdit($user$subject);
  66.             case AppRoles::ROLE_STUDENT_LIST:
  67.             return $this->canList($user);
  68.             case AppRoles::ROLE_STUDENT_MOVE_TO_TRASH:
  69.             return $this->canMoveToTrash($user);
  70.             case AppRoles::ROLE_STUDENT_MAILING:
  71.                 return $this->canMail($user);
  72.             case AppRoles::ROLE_STUDENT_PAYMENT:
  73.                 return $this->canAddPayment($user);
  74.             case AppRoles::ROLE_STUDENT_LIST_ANOTHER_STUDENTS:
  75.                 return $this->canListAnotherStudents($user);
  76.             case AppRoles::ROLE_STUDENT_EDIT_ANOTHER_STUDENTS:
  77.                 return $this->canEditAnotherStudents($user);
  78.         }
  79.         return false;
  80.     }
  81.     /**
  82.      * Checks whether user is granted to create a new student.
  83.      *
  84.      * @param User $loggedUser An User instance.
  85.      *
  86.      * @return boolean
  87.      */
  88.     private function canCreate(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_STUDENT_ADD);
  95.         }
  96.         return false;
  97.     }
  98.     /**
  99.      * Checks whether user is granted to edit student.
  100.      *
  101.      * @param User $loggedUser An User instance.
  102.      *
  103.      * @return boolean
  104.      */
  105.     private function canEdit(User $loggedUser$subject): bool
  106.     {
  107.         if($loggedUser->getSchool() !=  $subject->getSchool()){
  108.             return false;
  109.         }
  110.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  111.             return true;
  112.         }
  113.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  114.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_STUDENT_EDIT);
  115.         }
  116.         return false;
  117.     }
  118.     /**
  119.      * Checks whether user is granted to review  student's list
  120.      *
  121.      * @param User $loggedUser An User instance.
  122.      *
  123.      * @return boolean
  124.      */
  125.     private function canList(User $loggedUser): bool
  126.     {
  127.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  128.             return true;
  129.         }
  130.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  131.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_STUDENT_LIST);
  132.         }
  133.         return false;
  134.     }
  135.     /**
  136.      * Checks whether user is granted to  delete student
  137.      *
  138.      * @param User $loggedUser An User instance.
  139.      *
  140.      * @return boolean
  141.      */
  142.     private function canMoveToTrash(User $loggedUser): bool
  143.     {
  144.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  145.             return true;
  146.         }
  147.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  148.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_STUDENT_MOVE_TO_TRASH);
  149.         }
  150.         return false;
  151.     }
  152.     /**
  153.      * Checks whether user has access mail to student
  154.      *
  155.      * @param User $loggedUser An User instance.
  156.      *
  157.      * @return boolean
  158.      */
  159.     private function canMail(User $loggedUser): bool
  160.     {
  161.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  162.             return true;
  163.         }
  164.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  165.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_STUDENT_MAILING);
  166.         }
  167.         return false;
  168.     }
  169.     /**
  170.      * Checks whether user has access add payment to student
  171.      *
  172.      * @param User $loggedUser An User instance.
  173.      *
  174.      * @return boolean
  175.      */
  176.     private function canAddPayment(User $loggedUser): bool
  177.     {
  178.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  179.             return true;
  180.         }
  181.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  182.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_STUDENT_PAYMENT);
  183.         }
  184.         return false;
  185.     }
  186.     /**
  187.      * Checks whether user has access see another students
  188.      *
  189.      * @param User $loggedUser An User instance.
  190.      *
  191.      * @return boolean
  192.      */
  193.     private function canListAnotherStudents(User $loggedUser): bool
  194.     {
  195.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  196.             return true;
  197.         }
  198.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  199.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_STUDENT_LIST_ANOTHER_STUDENTS);
  200.         }
  201.         return false;
  202.     }
  203.     /**
  204.      * Checks whether user has access edit another students
  205.      *
  206.      * @param User $loggedUser An User instance.
  207.      *
  208.      * @return boolean
  209.      */
  210.     private function canEditAnotherStudents(User $loggedUser): bool
  211.     {
  212.         if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
  213.             return true;
  214.         }
  215.         if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
  216.             return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_STUDENT_EDIT_ANOTHER_STUDENTS);
  217.         }
  218.         return false;
  219.     }
  220. }