Skip to content

Commit

Permalink
fix access to visite editing #3100
Browse files Browse the repository at this point in the history
  • Loading branch information
emilschn committed Sep 27, 2024
1 parent 45e0b53 commit a94d907
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/Controller/Back/SignalementVisitesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function cancelVisiteFromSignalement(

return $this->redirectToRoute('back_index');
}
$this->denyAccessUnlessGranted('SIGN_ADD_VISITE', $signalement);
$this->denyAccessUnlessGranted('SIGN_EDIT_VISITE', $intervention);

if ($intervention->hasScheduledDatePassed()) {
$this->addFlash('error', 'Cette visite est déja passée et ne peut pas être annulée, merci de la noter comme non-effectuée.');
Expand Down Expand Up @@ -196,7 +196,7 @@ public function rescheduleVisiteFromSignalement(

return $this->redirectToRoute('back_index');
}
$this->denyAccessUnlessGranted('SIGN_ADD_VISITE', $signalement);
$this->denyAccessUnlessGranted('SIGN_EDIT_VISITE', $intervention);

$errorRedirect = $this->getSecurityRedirect(
$signalement,
Expand Down Expand Up @@ -264,7 +264,7 @@ public function confirmVisiteFromSignalement(

return $this->redirectToRoute('back_index');
}
$this->denyAccessUnlessGranted('SIGN_ADD_VISITE', $signalement);
$this->denyAccessUnlessGranted('SIGN_EDIT_VISITE', $intervention);

$errorRedirect = $this->getSecurityRedirect(
$signalement,
Expand Down Expand Up @@ -317,7 +317,7 @@ public function editVisiteFromSignalement(

return $this->redirectToRoute('back_index');
}
$this->denyAccessUnlessGranted('SIGN_ADD_VISITE', $signalement);
$this->denyAccessUnlessGranted('SIGN_EDIT_VISITE', $intervention);

$errorRedirect = $this->getSecurityRedirect(
$signalement,
Expand Down Expand Up @@ -358,7 +358,7 @@ public function deleteRapportVisiteFromSignalement(
EntityManagerInterface $entityManager,
UploadHandlerService $uploadHandlerService,
): Response {
$this->denyAccessUnlessGranted('SIGN_ADD_VISITE', $intervention->getSignalement());
$this->denyAccessUnlessGranted('SIGN_EDIT_VISITE', $intervention);
if (!$this->isCsrfTokenValid('delete_rapport', $request->get('_token')) || $intervention->getSignalement()->getId() !== $signalement->getId() || $intervention->getFiles()->isEmpty()) {
return $this->redirectToRoute('back_signalement_view', ['uuid' => $signalement->getUuid()]);
}
Expand Down
24 changes: 22 additions & 2 deletions src/Security/Voter/SignalementVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Entity\Affectation;
use App\Entity\Enum\Qualification;
use App\Entity\Enum\QualificationStatus;
use App\Entity\Intervention;
use App\Entity\Signalement;
use App\Entity\User;
use Symfony\Bundle\SecurityBundle\Security;
Expand All @@ -20,6 +21,7 @@ class SignalementVoter extends Voter
public const EDIT = 'SIGN_EDIT';
public const VIEW = 'SIGN_VIEW';
public const ADD_VISITE = 'SIGN_ADD_VISITE';
public const EDIT_VISITE = 'SIGN_EDIT_VISITE';
public const USAGER_EDIT = 'SIGN_USAGER_EDIT';
public const EDIT_NDE = 'SIGN_EDIT_NDE';

Expand All @@ -29,8 +31,9 @@ public function __construct(private Security $security)

protected function supports(string $attribute, $subject): bool
{
return \in_array($attribute, [self::EDIT, self::VIEW, self::DELETE, self::VALIDATE, self::CLOSE, self::ADD_VISITE, self::USAGER_EDIT, self::EDIT_NDE])
&& ($subject instanceof Signalement);
return (\in_array($attribute, [self::EDIT, self::VIEW, self::DELETE, self::VALIDATE, self::CLOSE, self::ADD_VISITE, self::USAGER_EDIT, self::EDIT_NDE])
&& ($subject instanceof Signalement))
|| (\in_array($attribute, [self::EDIT_VISITE]) && ($subject instanceof Intervention));
}

protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
Expand All @@ -49,6 +52,10 @@ protected function voteOnAttribute(string $attribute, $subject, TokenInterface $
return $this->canAddVisite($subject, $user);
}

if (self::EDIT_VISITE == $attribute) {
return $this->canEditVisite($subject, $user);
}

if (self::EDIT_NDE == $attribute) {
return $this->canEditNDE($subject, $user);
}
Expand Down Expand Up @@ -150,6 +157,19 @@ public function canAddVisite(Signalement $signalement, User $user): bool
return $user->isSuperAdmin() || $isUserInAffectedPartnerWithQualificationVisite || $isUserTerritoryAdminOfSignalementTerritory;
}

public function canEditVisite(Intervention $intervention, User $user): bool
{
$signalement = $intervention->getSignalement();
if (Signalement::STATUS_ACTIVE !== $signalement->getStatut()) {
return false;
}

$isUserInPartnerAffectedToVisite = $user->getPartner() === $intervention->getPartner();
$isUserTerritoryAdminOfSignalementTerritory = $user->isTerritoryAdmin() && $user->getTerritory() === $signalement->getTerritory();

return $user->isSuperAdmin() || $isUserInPartnerAffectedToVisite || $isUserTerritoryAdminOfSignalementTerritory;
}

private function canEditNDE(Signalement $signalement, User $user): bool
{
$signalementQualificationNDE = $signalement->getSignalementQualifications()->filter(function ($qualification) {
Expand Down
26 changes: 13 additions & 13 deletions templates/back/signalement/view/visites/visites-buttons.html.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

{% if signalement.interventions is not empty %}
<div class="signalement-visites-buttons fr-text--right fr-btns-group fr-btns-group--sm fr-btns-group--inline fr-btns-group--right fr-btns-group--icon-left">
{% if intervention.status is same as constant('App\\Entity\\Intervention::STATUS_PLANNED') %}
{% if is_granted('SIGN_ADD_VISITE', intervention.signalement) %}
{% if intervention.status is same as constant('App\\Entity\\Intervention::STATUS_PLANNED') %}
{% if is_granted('SIGN_EDIT_VISITE', intervention) %}
{% if workflow_can(intervention, 'cancel') %}
<button class="fr-btn fr-btn--danger fr-fi-close-line" aria-controls="cancel-visite-modal-{{intervention.id}}" data-fr-opened="false">
Annuler la visite
Expand All @@ -19,20 +19,20 @@
{% endif %}
{% elseif intervention.status is same as constant('App\\Entity\\Intervention::STATUS_DONE') %}
{% if signalement.interventions is empty or intervention.files is empty or intervention.getRapportDeVisite is empty %}
{% if is_granted('SIGN_ADD_VISITE', intervention.signalement) %}
<button
{% if is_granted('SIGN_EDIT_VISITE', intervention) %}
<button
class="fr-btn fr-btn--secondary fr-fi-file-fill"
aria-controls="edit-visite-modal-{{intervention.id}}"
aria-controls="edit-visite-modal-{{intervention.id}}"
data-fr-opened="false"
>
Ajouter un rapport de visite
</button>
{% endif %}
{% else %}
{% if is_granted('SIGN_ADD_VISITE', intervention.signalement) %}
<button
class="fr-btn fr-btn--secondary fr-fi-edit-line"
aria-controls="edit-visite-modal-{{intervention.id}}"
{% if is_granted('SIGN_EDIT_VISITE', intervention) %}
<button
class="fr-btn fr-btn--secondary fr-fi-edit-line"
aria-controls="edit-visite-modal-{{intervention.id}}"
data-fr-opened="false">
Editer le rapport
</button>
Expand All @@ -45,10 +45,10 @@
<span aria-hidden="true" class="fr-fi-file-line fr-icon--sm"></span> Voir le rapport de visite
</a>
{% endif %}
{% if is_granted('SIGN_ADD_VISITE', intervention.signalement)%}
<button
class="fr-btn fr-btn--secondary fr-icon-camera-fill open-modal-upload-files-btn"
data-fr-opened="false"
{% if is_granted('SIGN_EDIT_VISITE', intervention) %}
<button
class="fr-btn fr-btn--secondary fr-icon-camera-fill open-modal-upload-files-btn"
data-fr-opened="false"
aria-controls="visites-upload-files-{{intervention.id}}"
data-file-type="photo"
data-document-type="PHOTO_VISITE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
</div>

{% if signalement.interventions is empty %}
{% if is_granted('SIGN_ADD_VISITE', signalement) %}
{% include 'back/signalement/view/visites/visites-buttons.html.twig' %}
{% endif %}
{% include 'back/signalement/view/visites/visite-item.html.twig' %}
{% else %}
{% for intervention in signalement.interventions | filter(intervention => intervention.type != enum('App\\Entity\\Enum\\InterventionType').ARRETE_PREFECTORAL) %}
Expand Down

0 comments on commit a94d907

Please sign in to comment.