<?php
namespace App\Security\Voter\LearningCourses;
use App\Entity\LearningCourses\LearningCourse;
use App\Entity\User;
use App\Enum\MenuRolesAssociatedEnum;
use App\Enum\MenuRolesManagerEnum;
use App\Enum\UserRolesEnum;
use App\Enum\VotersEnum;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
final class LearningCourseSecurityVoter extends Voter
{
private Security $security;
private array $voters;
public function __construct(Security $security)
{
$this->security = $security;
$this->voters = [
VotersEnum::LIST_LEARNING_COURSE,
VotersEnum::LIST_LEARNING_COURSE_ASSOCIATED,
VotersEnum::CREATE_LEARNING_COURSE,
VotersEnum::READ,
VotersEnum::UPDATE,
VotersEnum::DELETE,
VotersEnum::VIEW,
VotersEnum::EXPORT_LEARNING_COURSE_REGISTRATIONS,
VotersEnum::OWN,
VotersEnum::ACTIVATE,
VotersEnum::DEACTIVATE,
VotersEnum::REPORT_LEARNING_COURSE_REGISTRATIONS,
];
}
protected function supports(string $attribute, $subject): bool
{
// first check the $subject and last if the $attribute is supported,
// because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
if ($subject && !$subject instanceof LearningCourse) {
// only vote on these objects
return false;
}
if (in_array($attribute, $this->voters)) {
// if the attribute is one we support
return true;
}
return false;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
// you know $subject is a LearningCourse object, thanks to `supports()`
/** @var LearningCourse $learningCourse */
$learningCourse = $subject;
switch ($attribute) {
case VotersEnum::LIST_LEARNING_COURSE:
return $this->canList();
case VotersEnum::LIST_LEARNING_COURSE_ASSOCIATED:
return $this->canListAssociated();
case VotersEnum::CREATE_LEARNING_COURSE:
return $this->canCreate();
case VotersEnum::READ:
return $this->canRead();
case VotersEnum::UPDATE:
return $this->canUpdate();
case VotersEnum::DELETE:
return $this->canDelete();
case VotersEnum::VIEW:
return $this->canView($learningCourse);
case VotersEnum::EXPORT_LEARNING_COURSE_REGISTRATIONS:
return $this->canExport();
case VotersEnum::OWN:
return $this->isOwner($learningCourse, $user);
case VotersEnum::ACTIVATE:
return $this->canActivate($learningCourse);
case VotersEnum::DEACTIVATE:
return $this->canDeactivate($learningCourse);
case VotersEnum::REPORT_LEARNING_COURSE_REGISTRATIONS:
return $this->canReport();
}
throw new LogicException('This code should not be reached!');
}
private function canList(): bool
{
return $this->isAdminUser();
}
private function canListAssociated(): bool
{
return $this->isAdminUser()
|| $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_LEARNING_PLATFORM_ASOCIATED)
;
}
private function canCreate(): bool
{
return $this->isAdminUser();
}
private function canRead(): bool
{
return $this->isAdminUser();
}
private function canUpdate(): bool
{
return $this->isAdminUser();
}
private function canDelete(): bool
{
return $this->isAdminUser();
}
private function canView(LearningCourse $learningCourse): bool
{
return $this->isAdminUser() && $learningCourse->isActive();
}
private function canExport(): bool
{
return $this->isAdminUser();
}
private function isOwner(LearningCourse $learningCourse, User $user): bool
{
if ($this->isAdminUser() || $this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
return true;
} else {
foreach ($learningCourse->getAssociatedUsers() as $associatedUser) {
if ($associatedUser->getId() === $user->getId()) {
return true;
}
}
}
return false;
}
private function canActivate(LearningCourse $learningCourse): bool
{
return $this->canCreate() && !$learningCourse->isActive();
}
private function canDeactivate(LearningCourse $learningCourse): bool
{
return $this->canCreate() && $learningCourse->isActive();
}
private function canReport(): bool
{
return $this->isAdminUser();
}
private function isAdminUser(): bool
{
return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_LEARNING_PLATFORM);
}
}