src/Voters/CompanySettingsVoter.php line 16

  1. <?php
  2. /*
  3.  *  @author Guerby Duval <info@tranzaksyon.com>
  4.  *  @link https://tranzaksyon.com
  5.  *  @copyright You are not allowed to remove this author "Guerby Duval <info@tranzaksyon.com>", the link "https://tranzaksyon.com" neither this copyright.
  6.  */
  7. namespace App\Voters;
  8. use App\Entity\User;
  9. use App\Enums\RolesEnum;
  10. use Symfony\Bundle\SecurityBundle\Security;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. class CompanySettingsVoter extends Voter
  14. {
  15.     const VIEW 'View_settings';
  16.     const EDIT 'Edit_settings';
  17.     const DELETE 'Delete_settings';
  18.     public function __construct(private Security $security)
  19.     {
  20.     }
  21.     protected function supports(string $attributemixed $subject): bool
  22.     {
  23.         if (in_array($attribute, [self::VIEWself::EDITself::DELETE]) && $subject instanceof User) {
  24.             return true;
  25.         }
  26.         return false;
  27.     }
  28.     /**
  29.      * @param string $attribute
  30.      * @param User $subject
  31.      * @param TokenInterface $token
  32.      * @return bool
  33.      */
  34.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  35.     {
  36.         /** @var User $user */
  37.         $user $token->getUser();
  38.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  39.             return true;
  40.         }
  41.         if ($this->security->isGranted('ROLE_COMPANY_ADMIN') || $this->security->isGranted('ROLE_ADMIN')) {
  42.             if ($token->getUser()->getId() === $subject->getId()) {
  43.                 return true;
  44.             }
  45.             if ($token->getUser()?->getEmployee()?->getSriInfo() !== null && $token->getUser()?->getEmployee()?->getSriInfo()?->getId() === $subject->getSriInfo()?->getId()) {
  46.                 return true;
  47.             }
  48.         }
  49.         // Si se yon partner
  50.         if ($this->security->isGranted(RolesEnum::partnerAdmin->value) && $user->getPartner()->getId() === $subject->getBelongToPartner()->getId()) {
  51.             return true;
  52.         }
  53.         return false;
  54.     }
  55. }