<?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 SchoolGroupVoter 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_GROUP_ADD,
AppRoles::ROLE_GROUP_EDIT,
AppRoles::ROLE_GROUP_LIST_ANOTHER_TEACHERS_GROUPS,
AppRoles::ROLE_GROUP_DELETE,
AppRoles::ROLE_GROUP_TASK_ASSIGNED,
AppRoles::ROLE_GROUP_TASK_HISTORY
])) {
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_GROUP_ADD:
return $this->canCreate($user);
case AppRoles::ROLE_GROUP_EDIT:
return $this->canEdit($user, $subject);
case AppRoles::ROLE_GROUP_LIST_ANOTHER_TEACHERS_GROUPS:
return $this->canListAnotherGroups($user);
case AppRoles::ROLE_GROUP_DELETE:
return $this->canDelete($user);
case AppRoles::ROLE_GROUP_TASK_ASSIGNED:
return $this->canAssignTask($user);
case AppRoles::ROLE_GROUP_TASK_HISTORY:
return $this->canSeeTaskHistory($user);
}
return false;
}
/**
* Checks whether user is granted to create a new student.
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canCreate(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_GROUP_ADD);
}
return false;
}
/**
* Checks whether user is granted to edit student.
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canEdit(User $loggedUser, $subject): bool
{
if($loggedUser->getSchool() != $subject->getSchool()){
return false;
}
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_GROUP_EDIT);
}
return false;
}
/**
* Checks whether user is granted to review student's list
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canListAnotherGroups(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_GROUP_LIST_ANOTHER_TEACHERS_GROUPS);
}
return false;
}
/**
* Checks whether user is granted to delete group
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canDelete(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_GROUP_DELETE);
}
return false;
}
/**
* Checks whether user has access get task to student
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canAssignTask(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_GROUP_TASK_ASSIGNED);
}
return false;
}
/**
* Checks whether user has access see task history to student
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canSeeTaskHistory(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_GROUP_TASK_HISTORY);
}
return false;
}
}