src/Security/Voter/SocialConversationVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\SocialConversation;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class SocialConversationVoter extends Voter
  8. {
  9.     public const VIEW 'SOCIAL_VIEW';
  10.     public const REPLY 'SOCIAL_REPLY';
  11.     public const MANAGE 'SOCIAL_MANAGE';
  12.     public const QUALIFY 'SOCIAL_QUALIFY';
  13.     public const FOLLOW 'SOCIAL_FOLLOW';
  14.     public const CONVERT_CLIENT 'SOCIAL_CONVERT_CLIENT';
  15.     public const CONVERT_ORDER 'SOCIAL_CONVERT_ORDER';
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         return in_array($attribute, [self::VIEWself::REPLYself::MANAGEself::QUALIFYself::FOLLOWself::CONVERT_CLIENTself::CONVERT_ORDER], true)
  19.             && $subject instanceof SocialConversation;
  20.     }
  21.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  22.     {
  23.         $user $token->getUser();
  24.         if (!$user instanceof User) {
  25.             return false;
  26.         }
  27.         if (in_array('ROLE_SUPER_ADMIN', (array) $user->getRoles(), true)) {
  28.             return true;
  29.         }
  30.         $rights = (array) $user->getArrayRight();
  31.         if (!in_array('SOCIAL_INBOX'$rightstrue)) {
  32.             return false;
  33.         }
  34.         /** @var SocialConversation $conversation */
  35.         $conversation $subject;
  36.         return match ($attribute) {
  37.             self::VIEW => true,
  38.             self::REPLY => in_array('SOCIAL_INBOX_REPLY'$rightstrue),
  39.             self::MANAGE => in_array('SOCIAL_INBOX_ASSIGN'$rightstrue)
  40.                 || $conversation->getAssignedTo()?->getId() === $user->getId(),
  41.             self::QUALIFY => in_array('SOCIAL_INBOX_QUALIFY'$rightstrue)
  42.                 || in_array('SOCIAL_INBOX_ASSIGN'$rightstrue)
  43.                 || $conversation->getAssignedTo()?->getId() === $user->getId(),
  44.             self::FOLLOW => in_array('SOCIAL_INBOX_QUALIFY'$rightstrue)
  45.                 || in_array('SOCIAL_INBOX_ASSIGN'$rightstrue)
  46.                 || $conversation->getAssignedTo()?->getId() === $user->getId(),
  47.             self::CONVERT_CLIENT => in_array('SOCIAL_INBOX_CONVERT_CLIENT'$rightstrue)
  48.                 || in_array('USERS_CREATE'$rightstrue),
  49.             self::CONVERT_ORDER => in_array('SOCIAL_INBOX_CONVERT_ORDER'$rightstrue)
  50.                 || in_array('DOCUMENT_CLIENT_CREATE'$rightstrue),
  51.             default => false,
  52.         };
  53.     }
  54. }