<?php
namespace App\Security\Voter\SatisfactionSurveys;
use App\Entity\SatisfactionSurveys\SatisfactionSurveyCustomerResponse;
use App\Entity\User;
use App\Enum\MenuRolesAssociatedEnum;
use App\Enum\MenuRolesManagerEnum;
use App\Enum\UserRolesEnum;
use App\Enum\VotersEnum;
use App\Repository\SatisfactionSurveys\SatisfactionSurveyCustomerResponseRepository;
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 SatisfactionSurveyCustomerResponseSecurityVoter extends Voter
{
private Security $security;
private array $voters;
private SatisfactionSurveyCustomerResponseRepository $sscrr;
public function __construct(Security $security, SatisfactionSurveyCustomerResponseRepository $sscrr)
{
$this->security = $security;
$this->voters = [
VotersEnum::LIST_SATISFACTION_SURVEY_CUSTOMER_RESPONSE,
VotersEnum::LIST_SATISFACTION_SURVEY_CUSTOMER_RESPONSE_ASSOCIATED,
VotersEnum::READ,
VotersEnum::OWN,
VotersEnum::EXPORT_SATISFACTION_SURVEY_CUSTOMER_RESPONSE,
VotersEnum::EXPORT_SATISFACTION_SURVEY_CUSTOMER_RESPONSE_ASSOCIATED,
];
$this->sscrr = $sscrr;
}
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 SatisfactionSurveyCustomerResponse) {
// 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_SATISFACTION_SURVEY_CUSTOMER_RESPONSE:
return $this->canList();
case VotersEnum::LIST_SATISFACTION_SURVEY_CUSTOMER_RESPONSE_ASSOCIATED:
return $this->canListAssociated();
case VotersEnum::READ:
return $this->canRead();
case VotersEnum::OWN:
return $this->isOwner($subject, $user);
case VotersEnum::EXPORT_SATISFACTION_SURVEY_CUSTOMER_RESPONSE:
return $this->canExport();
case VotersEnum::EXPORT_SATISFACTION_SURVEY_CUSTOMER_RESPONSE_ASSOCIATED:
return $this->canExportAssociated();
}
throw new LogicException('This code should not be reached!');
}
private function canList(): bool
{
return $this->isAdminUser();
}
private function canListAssociated(): bool
{
return $this->isAssociatedUser();
}
private function canRead(): bool
{
return $this->isAdminUser() || $this->isAssociatedUser();
}
private function isOwner(SatisfactionSurveyCustomerResponse $satisfactionSurveyCustomerResponse, User $user): bool
{
if ($this->security->isGranted(UserRolesEnum::ROLE_ADMIN_LONG) || $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_SATISFACTION_SURVEYS)) {
return true;
} elseif ($this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
$satisfactionSurveyCustomerResponses = $this->sscrr->getByProvincesSortedByBegin($user->getWorkProvinces());
/** @var SatisfactionSurveyCustomerResponse $item */
foreach ($satisfactionSurveyCustomerResponses as $item) {
if ($satisfactionSurveyCustomerResponse->getId() === $item->getId()) {
return true;
}
}
} elseif ($satisfactionSurveyCustomerResponse->getGarage() && $satisfactionSurveyCustomerResponse->getGarage()->getOwner() && $satisfactionSurveyCustomerResponse->getGarage()->getOwner()->getId() === $user->getId()) {
return true;
}
return false;
}
private function canExport(): bool
{
return $this->isAdminUser();
}
private function canExportAssociated(): bool
{
return $this->isAssociatedUser();
}
private function isAssociatedUser(): bool
{
return $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_SATISFACTION_SURVEYS_ASOCIATED);
}
private function isAdminUser(): bool
{
return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_SATISFACTION_SURVEYS);
}
}