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

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.  * @DI\Tag("security.voter")
  14.  */
  15. class OrganizationVoter extends Voter
  16. {
  17.     const VIEW 'VIEW';
  18.     const MANAGE 'MANAGE';
  19.     const EMAIL 'EMAIL';
  20.     const CONTACTS 'CONTACTS';
  21.     const ADMIN 'ADMIN';
  22.     protected OrganizationManager $organizationManager;
  23.     protected PermissionManager $permissionManager;
  24.     private RequestStack $requestStack;
  25.     /**
  26.      * @DI\InjectParams({
  27.      *     "organizationManager" = @DI\Inject("organization_manager"),
  28.      *     "permissionManager" = @DI\Inject("permission_manager"),
  29.      *     "requestStack" = @DI\Inject("request_stack")
  30.      * })
  31.      */
  32.     public function __construct(
  33.         OrganizationManager $organizationManager,
  34.         PermissionManager $permissionManager,
  35.         RequestStack $requestStack
  36.     )
  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 && in_array($attribute, [self::VIEWself::MANAGEself::EMAILself::CONTACTSself::ADMIN]);
  45.     }
  46.     protected function voteOnAttribute($attribute$organizationTokenInterface $token)
  47.     {
  48.         if (self::VIEW === $attribute && $organization->isActive() && ($organization->isApproved() || $organization->isDirectoryOnly())) {
  49.             return true;
  50.         }
  51.         $route $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->get('_route') : null;
  52.         if ('organization_register_user' === $route) {
  53.             return true;
  54.         }
  55.         // make sure there is a user object (i.e. that the user is logged in)
  56.         $user $token->getUser();
  57.         if (!$user instanceof UserInterface) {
  58.             return false;
  59.         }
  60.         if ($this->organizationManager->isOrganizationAdmin($organization$user)) {
  61.             return true;
  62.         }
  63.         // If not an org admin, no access to any manage resources
  64.         if (self::MANAGE === $attribute) {
  65.             return false;
  66.         }
  67.         // User is logged in, not an admin, see if they have the ability to view any of the orgs campaigns
  68.         if (count($this->permissionManager->getUserGroupMembershipForOrganization($organization$user))) {
  69.             return true;
  70.         }
  71.         return false;
  72.     }
  73. }