<?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 TeacherVoter 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_TEACHER_ADD,
AppRoles::ROLE_TEACHER_EDIT,
AppRoles::ROLE_TEACHER_LIST_ANOTHER_TEACHERS,
AppRoles::ROLE_TEACHER_DELETE,
AppRoles::ROLE_TEACHER_STATISTICS,
AppRoles::ROLE_TEACHER_MAILING
])) {
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_TEACHER_ADD:
return $this->canCreate($user);
case AppRoles::ROLE_TEACHER_EDIT:
return $this->canEdit($user, $subject);
case AppRoles::ROLE_TEACHER_LIST_ANOTHER_TEACHERS:
return $this->canListAnotherTeachers($user);
case AppRoles::ROLE_TEACHER_DELETE:
return $this->canDelete($user);
case AppRoles::ROLE_TEACHER_STATISTICS:
return $this->canSeeTeacherStatistics($user);
case AppRoles::ROLE_TEACHER_MAILING:
return $this->canSendMailToTeacher($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_TEACHER_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_TEACHER_EDIT);
}
return false;
}
/**
* Checks whether user is granted to review student's list
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canListAnotherTeachers(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_TEACHER_LIST_ANOTHER_TEACHERS);
}
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_TEACHER_DELETE);
}
return false;
}
/**
* Checks whether user has access get task to student
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canSeeTeacherStatistics(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_TEACHER_STATISTICS);
}
return false;
}
/**
* Checks whether user has access see task history to student
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canSendMailToTeacher(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_TEACHER_MAILING);
}
return false;
}
}