src/Security/CompanyReviewVoter.php line 23

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of Synapse.
  4.  *
  5.  * Copyright (C) 2020-2022 Daniel Ménard
  6.  *
  7.  * For copyright and license information, please view the
  8.  * LICENSE file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\Security;
  12. use App\Entity\Company;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  15. /**
  16.  * CompanyReviewVoter.
  17.  *
  18.  * @author Daniel Ménard <daniel.menard.35@gmail.com>
  19.  */
  20. final class CompanyReviewVoter extends Voter
  21. {
  22.     /**
  23.      * L'attribut à utiliser pour déterminer si l'utilisateur a accès à l'annuaire du DDO.
  24.      */
  25.     public const DDO_REVIEWS 'ddo_reviews';
  26.     /**
  27.      * L'attribut à utiliser pour déterminer si l'utilisateur a accès à l'annuaire du groupe.
  28.      */
  29.     public const GROUP_REVIEWS 'group_reviews';
  30.     /**
  31.      * L'attribut à utiliser pour déterminer si l'utilisateur a accès à l'annuaire global Synapse.
  32.      */
  33.     public const GLOBAL_REVIEWS 'global_reviews';
  34.     /**
  35.      * Le rôle à attribuer à un utilisateur pour lui donner le droit GLOBAL_REVIEWS.
  36.      */
  37.     public const ROLE_GLOBAL_REVIEWS 'ROLE_GLOBAL_REVIEWS';
  38.     /**
  39.      * Liste des attributs que l'on gère.
  40.      */
  41.     private const ATTRIBUTES = [
  42.         self::DDO_REVIEWS => 'canAccessDdoReviews',
  43.         self::GROUP_REVIEWS => 'canAccessGroupReviews',
  44.         self::GLOBAL_REVIEWS => 'canAccessGlobalReviews',
  45.     ];
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     final protected function supports(string $attribute$subject): bool
  50.     {
  51.         return isset(self::ATTRIBUTES[$attribute]);
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     final protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  57.     {
  58.         // L'utilisateur doit être connecté, seuls les user Company ont un annuaire (pas les accès AO)
  59.         $user $token->getUser();
  60.         if (!$user instanceof Company) {
  61.             return false;
  62.         }
  63.         // Teste la permission demandée
  64.         $method = [$thisself::ATTRIBUTES[$attribute]];
  65.         return $method($user);
  66.     }
  67.     /**
  68.      * Teste si un utilisateur a accès à l'annuaire du DDO.
  69.      */
  70.     final protected function canAccessDdoReviews(Company $user): bool
  71.     {
  72.         // Seulement si c'est un DDO et que ce n'est pas un groupe
  73.         return $user->getIsDDO() && ($user->getGroupId() !== $user->getId());
  74.     }
  75.     /**
  76.      * Teste si un utilisateur a accès à l'annuaire du groupe.
  77.      */
  78.     final protected function canAccessGroupReviews(Company $user): bool
  79.     {
  80.         // Seulement si c'est un un groupe ou un DDO rattaché à un groupe
  81.         return !== $user->getGroupId();
  82.     }
  83.     /**
  84.      * Teste si un utilisateur a accès à l'annuaire global Synapse.
  85.      */
  86.     final protected function canAccessGlobalReviews(Company $user): bool
  87.     {
  88.         // Si le rôle 'ROLE_GLOBAL_REVIEWS' a explicitement été attribué ou si c'est un admin
  89.         $roles = (array) $user->getRoles();
  90.         return in_array(self::ROLE_GLOBAL_REVIEWS$roles) || in_array('ROLE_ADMIN'$roles);
  91.     }
  92. }