<?php
// src/Security/Authorization/Voter/UserAclVoter.php
namespace MentalSchool\AppBundle\Security\Authorization\Voter;
use MentalSchool\AppBundle\Entity\User;
use MentalSchool\AppBundle\Security\AppRoles;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\DependencyInjection\{
ContainerInterface, ContainerAwareInterface
};
class GeneralVoter extends Voter implements ContainerAwareInterface
{
/**
* @var null|ContainerInterface A ContainerInterface instance.
*/
protected $container = null;
/**
* Set container.
*
* @param ContainerInterface $container A ContainerInterface instance.
*/
public function setContainer(ContainerInterface $container = null): void
{
if (null === $this->container) {
$this->container = $container;
}
}
protected function supports($attribute, $subject)
{
if(!defined('MentalSchool\AppBundle\Security\AppRoles::'. $attribute)){
return false;
}
$permission = constant('MentalSchool\AppBundle\Security\AppRoles::'. $attribute);
// if the attribute isn't one we support, return false
if (!in_array($permission, [
AppRoles::ROLE_GENERAL_SHOW_NEWS_LIST,
AppRoles::ROLE_GENERAL_ADD_NEW_PLACE,
AppRoles::ROLE_SHOW_INFORMATION_PAGE,
])) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
if (count(array_intersect(array('ROLE_SUPER_ADMIN'), $user->getRoles()))) {
return true;
}
$attribute = constant('MentalSchool\AppBundle\Security\AppRoles::'. $attribute);
switch ($attribute) {
case AppRoles::ROLE_GENERAL_SHOW_NEWS_LIST:
return $this->canSeeNewsList($user);
case AppRoles::ROLE_GENERAL_ADD_NEW_PLACE:
return $this->canAddNewPlace($user, $subject);
case AppRoles::ROLE_SHOW_INFORMATION_PAGE:
return $this->canShowInformationPage($user);
}
return false;
}
/**
* Checks whether user is granted to see news .
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canSeeNewsList(User $loggedUser): bool
{
if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
return true;
}
if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_GENERAL_SHOW_NEWS_LIST);
}
return false;
}
/**
* Checks whether user is granted to add palace
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canAddNewPlace(User $loggedUser): bool
{
if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
return true;
}
if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_GENERAL_ADD_NEW_PLACE);
}
return false;
}
/**
* Checks whether user is granted to show information page
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canShowInformationPage(User $loggedUser): bool
{
$em = $this->container->get('doctrine')->getManager();
if ($em->getRepository('AppBundle:InformationPage')->hasAccessToInformationPage($loggedUser->getSchool()->getId())){
if (count(array_intersect(array('ROLE_DIRECTOR'), $loggedUser->getRoles()))) {
return true;
}
if (count(array_intersect(array('ROLE_TEACHER'), $loggedUser->getRoles()))) {
return $loggedUser->hasPermissionOnAction(AppRoles::ROLE_SHOW_INFORMATION_PAGE);
}
}
return false;
}
}