src/Bidcoz/Bundle/CoreBundle/Security/Authorization/Voter/OrganizationVoter.php line 19

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\CoreBundle\Security\Authorization\Voter;
  3. use Bidcoz\Bundle\CoreBundle\Entity\Organization;
  4. use Bidcoz\Bundle\CoreBundle\Services\OrganizationManager;
  5. use Bidcoz\Bundle\CoreBundle\Services\PermissionManager;
  6. use RS\DiExtraBundle\Annotation as DI;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. /**
  12.  * @DI\Service
  13.  *
  14.  * @DI\Tag("security.voter")
  15.  */
  16. class OrganizationVoter extends Voter
  17. {
  18.     public const VIEW     'VIEW';
  19.     public const MANAGE   'MANAGE';
  20.     public const EMAIL    'EMAIL';
  21.     public const CONTACTS 'CONTACTS';
  22.     public const ADMIN    'ADMIN';
  23.     protected OrganizationManager $organizationManager;
  24.     protected PermissionManager $permissionManager;
  25.     private RequestStack $requestStack;
  26.     /**
  27.      * @DI\InjectParams({
  28.      *     "organizationManager" = @DI\Inject("organization_manager"),
  29.      *     "permissionManager" = @DI\Inject("permission_manager"),
  30.      *     "requestStack" = @DI\Inject("request_stack")
  31.      * })
  32.      */
  33.     public function __construct(
  34.         OrganizationManager $organizationManager,
  35.         PermissionManager $permissionManager,
  36.         RequestStack $requestStack
  37.     ) {
  38.         $this->organizationManager $organizationManager;
  39.         $this->permissionManager   $permissionManager;
  40.         $this->requestStack        $requestStack;
  41.     }
  42.     protected function supports($attribute$subject)
  43.     {
  44.         return $subject instanceof Organization;
  45.     }
  46.     protected function voteOnAttribute($attribute$organizationTokenInterface $token)
  47.     {
  48.         if (!in_array($attribute, [self::VIEWself::MANAGEself::EMAILself::CONTACTSself::ADMIN])) {
  49.             // For all of the "component" permissions, always grant true
  50.             // Note: This is a temporary solution to allow for the permissions to be checked without breaking the system
  51.             return true;
  52.         }
  53.         if (self::VIEW === $attribute && $organization->isActive() && $organization->isApproved()) {
  54.             return true;
  55.         }
  56.         $route $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->get('_route') : null;
  57.         if ('organization_register_user' === $route) {
  58.             return true;
  59.         }
  60.         // make sure there is a user object (i.e. that the user is logged in)
  61.         $user $token->getUser();
  62.         if (!$user instanceof UserInterface) {
  63.             return false;
  64.         }
  65.         if ($this->organizationManager->isOrganizationAdmin($organization$user)) {
  66.             return true;
  67.         }
  68.         // If not an org admin, no access to any manage resources
  69.         if (self::MANAGE === $attribute) {
  70.             return false;
  71.         }
  72.         // User is logged in, not an admin, see if they have the ability to view any of the orgs campaigns
  73.         if (count($this->permissionManager->getUserGroupMembershipForOrganization($organization$user))) {
  74.             return true;
  75.         }
  76.         return false;
  77.     }
  78. }