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

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\CoreBundle\Security\Authorization\Voter;
  3. use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
  4. use Bidcoz\Bundle\CoreBundle\Entity\GroupMember;
  5. use Bidcoz\Bundle\CoreBundle\Services\OrganizationManager;
  6. use Bidcoz\Bundle\CoreBundle\Services\PermissionManager;
  7. use RS\DiExtraBundle\Annotation as DI;
  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 CampaignVoter extends Voter
  16. {
  17.     const FULL         'FULL';
  18.     const VIEW         'VIEW';
  19.     const MANAGE       'MANAGE';
  20.     const AUCTION      'AUCTION';
  21.     const SHOP         'SHOP';
  22.     const FUND_A_NEED  'FUND_A_NEED';
  23.     const FINANCE      'FINANCE';
  24.     const DONATION     'DONATION';
  25.     const PROCUREMENT  'PROCUREMENT';
  26.     const FUND_DRIVE   'FUND_DRIVE';
  27.     const MEMBERSHIP   'MEMBERSHIP';
  28.     const PADDLE_RISE  'PADDLE_RISE';
  29.     const LEADERBOARD  'LEADERBOARD';
  30.     const SPONSORSHIP  'SPONSORSHIP';
  31.     const TICKET       'TICKET';
  32.     const CONTACTS     'CONTACTS';
  33.     const REGISTRATION 'REGISTRATION';
  34.     const RAFFLE       'RAFFLE';
  35.     const VOLUNTEER    'VOLUNTEER';
  36.     const EMAIL        'EMAIL';
  37.     const API          'API';
  38.     protected static $attributes = [
  39.         self::FULL,
  40.         self::VIEW,
  41.         self::MANAGE,
  42.         self::AUCTION,
  43.         self::SPONSORSHIP,
  44.         self::FINANCE,
  45.         self::DONATION,
  46.         self::PROCUREMENT,
  47.         self::FUND_DRIVE,
  48.         self::MEMBERSHIP,
  49.         self::PADDLE_RISE,
  50.         self::LEADERBOARD,
  51.         self::CONTACTS,
  52.         self::TICKET,
  53.         self::REGISTRATION,
  54.         self::RAFFLE,
  55.         self::VOLUNTEER,
  56.         self::EMAIL,
  57.         self::API,
  58.         self::SHOP,
  59.         self::FUND_A_NEED,
  60.     ];
  61.     protected $organizationManager;
  62.     protected $permissionManager;
  63.     /**
  64.      * @DI\InjectParams({
  65.      *      "organizationManager" = @DI\Inject("organization_manager"),
  66.      *      "permissionManager" = @DI\Inject("permission_manager"),
  67.      * })
  68.      */
  69.     public function __construct(OrganizationManager $organizationManagerPermissionManager $permissionManager)
  70.     {
  71.         $this->organizationManager $organizationManager;
  72.         $this->permissionManager   $permissionManager;
  73.     }
  74.     protected function supports($attribute$subject)
  75.     {
  76.         return $subject instanceof Campaign && in_array($attributeself::$attributes);
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     protected function voteOnAttribute($attribute$campaignTokenInterface $token): bool
  82.     {
  83.         $organization $campaign->getOrganization();
  84.         if (self::VIEW === $attribute && $organization->isActive()) {
  85.             if (!$campaign->isHidden()) {
  86.                 return true;
  87.             }
  88.         }
  89.         // make sure there is a user object (i.e. that the user is logged in)
  90.         $user $token->getUser();
  91.         if (!$user instanceof UserInterface) {
  92.             return false;
  93.         }
  94.         if ($user->hasRole('ROLE_SUPER_ADMIN')) {
  95.             return true;
  96.         }
  97.         if ($this->organizationManager->isOrganizationAdmin($organization$user)) {
  98.             return true;
  99.         }
  100.         if ($memberships $this->permissionManager->getUserGroupMembershipForCampaign($campaign$user)) {
  101.             if ($this->hasPermission($memberships'FULL')) {
  102.                 return true;
  103.             } elseif ($this->hasPermission($memberships$attribute)) {
  104.                 return true;
  105.             }
  106.         }
  107.         return false;
  108.     }
  109.     /**
  110.      * @param GroupMember[] $memberships
  111.      * @param $permission
  112.      *
  113.      * @return bool
  114.      */
  115.     protected function hasPermission(array $memberships$permission)
  116.     {
  117.         foreach ($memberships as $membership) {
  118.             $group $membership->getGroup();
  119.             if ($group->hasRole($permission)) {
  120.                 return true;
  121.             }
  122.         }
  123.         return false;
  124.     }
  125. }