<?php
namespace App\Security\Voter;
use App\Entity\Company;
use App\Entity\CompanyDemand;
use App\Entity\Demand;
use App\Entity\User;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class DemandVoter extends Voter
{
protected function supports(string $attribute, $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, ['DEMAND_SHOW', 'DEMAND_UPLOAD', 'DEMAND_UPDATE', 'INTERVENTION_SHOW', 'ESTIMATE_SHOW'], true) && $subject instanceof Demand;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof User) {
return false;
}
/** @var Demand $demand */
$demand = $subject;
/** @var Collection<CompanyDemand> $companyDemandAssigned */
$companyDemandAssigned = $demand->getCompanyDemands()->filter(function (CompanyDemand $companyDemand) use ($user): bool {
return $companyDemand->getCompany() === $user->getCompanies()->first();
});
$companyDemandSelected = $demand->getCompanyDemands()->filter(function (CompanyDemand $companyDemand) use ($user): bool {
return $companyDemand->getCompany() === $user->getCompanies()->first() && $companyDemand->getSelected() === true;
})->first();
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case 'DEMAND_SHOW':
return $demand->getUser() === $user;
case 'DEMAND_UPLOAD':
// logic to determine if the user can EDIT
// return true or false
if ($demand->getUser() === $user) {
if (
$demand->getStatus() <= Demand::IN_PENDING_STATUS ||
$demand->getStatus() === Demand::ON_GOING_STATUS ||
$demand->getStatus() === Demand::COMPANY_POSTED_FEEDBACK_STATUS ||
$demand->getStatus() === Demand::COMPLETE_STATUS
) {
return true;
}
}
if (in_array('ROLE_ADMIN', $user->getRoles(), true)) {
return true;
}
if ($companyDemandAssigned->first()->getCompany()?->getUser() === $user) {
if (
$demand->getStatus() === Demand::ON_GOING_STATUS ||
$demand->getStatus() === Demand::INTERVENTION_CONFIRMATION_STATUS ||
$demand->getStatus() === Demand::SELECTED_COMPANY_STATUS ||
$demand->getStatus() === Demand::USER_POSTED_FEEDBACK_STATUS ||
$demand->getStatus() === Demand::COMPLETE_STATUS
) {
return true;
}
}
// no break
case 'DEMAND_UPDATE':
// logic to determine if the user can VIEW
// return true or false
return $demand->getUser() === $user && $demand->getStatus() <= Demand::IN_PENDING_STATUS;
case 'INTERVENTION_SHOW':
//Si la company est assigner a la demande
if (!$companyDemandAssigned->isEmpty()) {
//Si la company est selectionner
if (!empty($companyDemandSelected)) {
//Si le status de la demande est superieur à "En cours"
if (
$demand->getStatus() >= Demand::ON_GOING_STATUS
) {
return true;
}
} else {
//Si les status de la demande sont "Confirmation d'intervention" ou "Company sélectionner"
if (
$demand->getStatus() === Demand::INTERVENTION_CONFIRMATION_STATUS ||
$demand->getStatus() === Demand::SELECTED_COMPANY_STATUS
) {
return true;
}
}
}
}
return false;
}
}