<?php
namespace App\Security\Voter\Users;
use App\Entity\Garages\Garage;
use App\Entity\User;
use App\Enum\MenuRolesManagerEnum;
use App\Enum\UserRolesEnum;
use App\Enum\VotersEnum;
use App\Repository\Garages\GarageRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
final class UserVoter extends Voter
{
private Security $security;
private array $voters;
private GarageRepository $gr;
public function __construct(Security $security, GarageRepository $gr)
{
$this->security = $security;
$this->voters = [
VotersEnum::LIST_USER,
VotersEnum::CREATE_USER,
VotersEnum::READ,
VotersEnum::UPDATE,
VotersEnum::DELETE,
VotersEnum::STATISTICS_LIST,
VotersEnum::STATISTICS_VIEW,
VotersEnum::STATISTICS_ENABLE,
VotersEnum::STATISTICS_DISABLE,
VotersEnum::STATISTICS_DOWNLOAD,
];
$this->gr = $gr;
}
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 User) {
// 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;
}
switch ($attribute) {
case VotersEnum::LIST_USER:
return $this->canList();
case VotersEnum::CREATE_USER:
return $this->canCreate();
case VotersEnum::READ:
return $this->canRead();
case VotersEnum::UPDATE:
return $this->canUpdate($subject);
case VotersEnum::DELETE:
return $this->canDelete($subject);
case VotersEnum::STATISTICS_LIST:
return $this->canListStatistics($subject, $user);
case VotersEnum::STATISTICS_VIEW:
return $this->canViewStatistics($subject, $user);
case VotersEnum::STATISTICS_ENABLE:
return $this->canEnableStatistics($subject);
case VotersEnum::STATISTICS_DISABLE:
return $this->canDisableStatistics($subject);
case VotersEnum::STATISTICS_DOWNLOAD:
return $this->canDownloadStatistics($subject, $user);
}
throw new \LogicException('This code should not be reached!');
}
private function canList(): bool
{
return $this->isAdminUser();
}
private function canCreate(): bool
{
return $this->isAdminUser();
}
private function canRead(): bool
{
return $this->isAdminUser();
}
private function canUpdate(User $subject): bool
{
if ((UserRolesEnum::ROLE_ADMIN === $subject->getUserRole()) || (UserRolesEnum::ROLE_SUPER_ADMIN === $subject->getUserRole())) {
// only users with ROLE_ADMIN can update ADMINS users
return $this->security->isGranted(UserRolesEnum::ROLE_ADMIN_LONG);
} else {
return $this->isAdminUser();
}
}
private function canDelete(User $user): bool
{
return $this->canUpdate($user);
}
private function canListStatistics(User $user, User $loggedUser): bool
{
if ($this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
$found = false;
$garages = $this->gr->getEnabledGaragesByProvincesSortedByName($loggedUser->getWorkProvinces());
/** @var Garage $garage */
foreach ($garages as $garage) {
if ($garage->isOwner($user)) {
$found = true;
break;
}
}
return $found && $user->isEnableStatisticsManagement();
}
return $this->isStatisticsAdminUser() && $user->isEnableStatisticsManagement();
}
private function canViewStatistics(User $user, User $loggedUser): bool
{
return $this->canListStatistics($user, $loggedUser);
}
private function canEnableStatistics(User $user): bool
{
return $this->isStatisticsAdminUser() && !$user->isEnableStatisticsManagement();
}
private function canDisableStatistics(User $user): bool
{
return $this->isStatisticsAdminUser() && $user->isEnableStatisticsManagement();
}
private function canDownloadStatistics(User $user, User $loggedUser): bool
{
return $this->canListStatistics($user, $loggedUser);
}
private function isStatisticsAdminUser(): bool
{
return ($this->isAdminUser() || $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_BAROMETER_DATA)) && !$this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG);
}
private function isAdminUser(): bool
{
return $this->security->isGranted(UserRolesEnum::ROLE_ADMIN_LONG);
}
}