src/Security/Voter/MainVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Campus;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. class MainVoter extends Voter
  11. {
  12.     private $session;
  13.     private $security;
  14.     public function __construct(SessionInterface $sessionInterfaceSecurity $security)
  15.     {
  16.         $this->security $security;
  17.         $this->session $sessionInterface;
  18.     }
  19.     protected function supports($attribute$subject)
  20.     {
  21.         if(!$this->security->getUser())
  22.             return false;
  23.         if(in_array("ROLE_STUDENT"$this->security->getUser()->getRoles()))
  24.         return false;
  25.        
  26.         $permission $this->session->get("PERMISSION");
  27.         if ($this->security->getUser() &&  in_array("SUPER_ADMIN"$this->security->getUser()->getRoles()))
  28.             return true;
  29.         if (!$permission)
  30.             $permission = array();
  31.         return in_array($attribute$permission);
  32.     }
  33.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  34.     {
  35.        
  36.       
  37.         $user $token->getUser();
  38.         // if the user is anonymous, do not grant access
  39.        
  40.         if (in_array("SUPER_ADMIN"$user->getRoles()))
  41.             return true;
  42.         switch ($attribute) {
  43.             case 'dorm_plc_own':
  44.             case 'drm_plc_own':
  45.             case 'drm_plc_sng_own':
  46.             case 'drm_edt_blck_own':
  47.             case 'drm_shw_blck_own':
  48.             case 'drm_stdt_lst_own':
  49.             case 'drm_stdt_vw_own':
  50.             
  51.                 if ($subject instanceof Campus) {
  52.                     foreach ($user->getDormUsers() as $key => $dormuser) {
  53.                         if ($dormuser->getCampus() == $subject)
  54.                             return true;
  55.                     }
  56.                     return false;
  57.                 }
  58.                 return true;
  59.             case 'drm_usr_own':
  60.                 if ($subject instanceof Campus) {
  61.                     return $user->getUserInfo()->getCampus() == $subject;
  62.                 }
  63.                 return true;
  64.             case 'dorm_sum_view_own':
  65.                 if ($subject instanceof Campus) {
  66.                     foreach ($user->getDormUsers() as $key => $dormuser) {
  67.                         if ($dormuser->getCampus() == $subject)
  68.                             return true;
  69.                     }
  70.                     return false;
  71.                 }
  72.                 return true;
  73.                 // logic to determine if the user can EDIT
  74.                 // return true or false
  75.                 break;
  76.             case 'POST_VIEW':
  77.                 // logic to determine if the user can VIEW
  78.                 // return true or false
  79.                 break;
  80.         }
  81.         return true;
  82.     }
  83. }