<?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 StudentVoter 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_STUDENT_ADD,
AppRoles::ROLE_STUDENT_EDIT,
AppRoles::ROLE_STUDENT_SHOW,
AppRoles::ROLE_STUDENT_MOVE_TO_TRASH,
AppRoles::ROLE_STUDENT_LIST,
AppRoles::ROLE_STUDENT_MAILING,
AppRoles::ROLE_STUDENT_PAYMENT,
AppRoles::ROLE_STUDENT_TASK_LIST,
AppRoles::ROLE_STUDENT_LIST_ANOTHER_STUDENTS,
AppRoles::ROLE_STUDENT_EDIT_ANOTHER_STUDENTS
])) {
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_STUDENT_ADD:
return $this->canCreate($user);
case AppRoles::ROLE_STUDENT_EDIT:
return $this->canEdit($user, $subject);
case AppRoles::ROLE_STUDENT_LIST:
return $this->canList($user);
case AppRoles::ROLE_STUDENT_MOVE_TO_TRASH:
return $this->canMoveToTrash($user);
case AppRoles::ROLE_STUDENT_MAILING:
return $this->canMail($user);
case AppRoles::ROLE_STUDENT_PAYMENT:
return $this->canAddPayment($user);
case AppRoles::ROLE_STUDENT_LIST_ANOTHER_STUDENTS:
return $this->canListAnotherStudents($user);
case AppRoles::ROLE_STUDENT_EDIT_ANOTHER_STUDENTS:
return $this->canEditAnotherStudents($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_STUDENT_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_STUDENT_EDIT);
}
return false;
}
/**
* Checks whether user is granted to review student's list
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canList(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_STUDENT_LIST);
}
return false;
}
/**
* Checks whether user is granted to delete student
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canMoveToTrash(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_STUDENT_MOVE_TO_TRASH);
}
return false;
}
/**
* Checks whether user has access mail to student
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canMail(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_STUDENT_MAILING);
}
return false;
}
/**
* Checks whether user has access add payment to student
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canAddPayment(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_STUDENT_PAYMENT);
}
return false;
}
/**
* Checks whether user has access see another students
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canListAnotherStudents(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_STUDENT_LIST_ANOTHER_STUDENTS);
}
return false;
}
/**
* Checks whether user has access edit another students
*
* @param User $loggedUser An User instance.
*
* @return boolean
*/
private function canEditAnotherStudents(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_STUDENT_EDIT_ANOTHER_STUDENTS);
}
return false;
}
}