<?php
namespace App\Security\Voter;
use App\Entity\Campus;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security;
class MainVoter extends Voter
{
private $session;
private $security;
public function __construct(SessionInterface $sessionInterface, Security $security)
{
$this->security = $security;
$this->session = $sessionInterface;
}
protected function supports($attribute, $subject)
{
if(!$this->security->getUser())
return false;
if(in_array("ROLE_STUDENT", $this->security->getUser()->getRoles()))
return false;
$permission = $this->session->get("PERMISSION");
if ($this->security->getUser() && in_array("SUPER_ADMIN", $this->security->getUser()->getRoles()))
return true;
if (!$permission)
$permission = array();
return in_array($attribute, $permission);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (in_array("SUPER_ADMIN", $user->getRoles()))
return true;
switch ($attribute) {
case 'dorm_plc_own':
case 'drm_plc_own':
case 'drm_plc_sng_own':
case 'drm_edt_blck_own':
case 'drm_shw_blck_own':
case 'drm_stdt_lst_own':
case 'drm_stdt_vw_own':
if ($subject instanceof Campus) {
foreach ($user->getDormUsers() as $key => $dormuser) {
if ($dormuser->getCampus() == $subject)
return true;
}
return false;
}
return true;
case 'drm_usr_own':
if ($subject instanceof Campus) {
return $user->getUserInfo()->getCampus() == $subject;
}
return true;
case 'dorm_sum_view_own':
if ($subject instanceof Campus) {
foreach ($user->getDormUsers() as $key => $dormuser) {
if ($dormuser->getCampus() == $subject)
return true;
}
return false;
}
return true;
// logic to determine if the user can EDIT
// return true or false
break;
case 'POST_VIEW':
// logic to determine if the user can VIEW
// return true or false
break;
}
return true;
}
}