src/FMT/Application/Voter/CampaignVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace FMT\Application\Voter;
  3. use FMT\Data\Entity\Campaign;
  4. use FMT\Data\Entity\User;
  5. use FMT\Data\Entity\UserProfile;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. /**
  9.  * Class CampaignVoter
  10.  * @package FMT\Application\Controller\Voter
  11.  */
  12. class CampaignVoter extends Voter
  13. {
  14.     const CAN_EDIT 'canEdit';
  15.     const CAN_VIEW 'canView';
  16.     const CAN_SEE_DAYS_LEFT 'canSeeDaysLeft';
  17.     const CAN_FUND 'canFund';
  18.     const CAN_SHARE_FB 'canShareFB';
  19.     const CAN_SHARE_TW 'canShareTW';
  20.     const CAN_SEE_SUMMARY 'canSeeSummary';
  21.     protected static $allPermissions = [
  22.         self::CAN_EDIT,
  23.         self::CAN_VIEW,
  24.         self::CAN_SEE_DAYS_LEFT,
  25.         self::CAN_FUND,
  26.         self::CAN_SHARE_FB,
  27.         self::CAN_SHARE_TW,
  28.         self::CAN_SEE_SUMMARY,
  29.     ];
  30.     /**
  31.      * @param string $attribute
  32.      * @param mixed $subject
  33.      * @return bool
  34.      */
  35.     protected function supports($attribute$subject)
  36.     {
  37.         if (!in_array($attributeself::$allPermissions)) {
  38.             return false;
  39.         }
  40.         if (!$subject instanceof Campaign) {
  41.             return false;
  42.         }
  43.         return true;
  44.     }
  45.     /**
  46.      * @param string $attribute
  47.      * @param mixed $subject
  48.      * @param TokenInterface $token
  49.      * @return bool
  50.      */
  51.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  52.     {
  53.         /** @var User $user */
  54.         $user $token->getUser();
  55.         if (!$user instanceof User && $user != 'anon.') {
  56.             return false;
  57.         }
  58.         if (method_exists($this$attribute)) {
  59.             return $this->$attribute($subject$user);
  60.         }
  61.         return false;
  62.     }
  63.     /**
  64.      * @param Campaign $campaign
  65.      * @param User|string $user
  66.      * @return bool
  67.      */
  68.     protected function canEdit(Campaign $campaign$user)
  69.     {
  70.         $isSchoolActive $user->isStudent() && $campaign->getSchool()->isActive();
  71.         return $campaign->getUser() === $user && !$campaign->isFinished() && $isSchoolActive;
  72.     }
  73.     /**
  74.      * @param Campaign $campaign
  75.      * @param User|string $user
  76.      * @return bool
  77.      */
  78.     protected function canView(Campaign $campaign$user)
  79.     {
  80.         $campaignUser $campaign->getUser();
  81.         $campaignUserVisible $campaignUser->getProfile()->getVisible();
  82.         $isVisibilityAll $campaignUserVisible === UserProfile::VISIBILITY_ALL;
  83.         $isAllowedAsContact $this->isAllowedAsContact($campaign$user);
  84.         $isAllowedAsRegistered $this->isAllowedAsRegistered($campaign$user);
  85.         return $campaignUser === $user ||
  86.             $isVisibilityAll ||
  87.             $isAllowedAsRegistered ||
  88.             $isAllowedAsContact;
  89.     }
  90.     /**
  91.      * @param Campaign $campaign
  92.      * @param User|string $user
  93.      * @return bool
  94.      */
  95.     protected function canSeeDaysLeft(Campaign $campaign$user)
  96.     {
  97.         return $campaign->getUser() === $user && $campaign->daysLeft();
  98.     }
  99.     /**
  100.      * @param Campaign $campaign
  101.      * @return bool
  102.      */
  103.     protected function canFund(Campaign $campaign)
  104.     {
  105.         return $campaign->getId() &&
  106.             !$campaign->isFinished() &&
  107.             !$campaign->isPaused() &&
  108.             $campaign->isStarted() &&
  109.             $campaign->getPercentOfFunded() < 1;
  110.     }
  111.     /**
  112.      * @param Campaign $campaign
  113.      * @return bool
  114.      */
  115.     protected function canShareFB(Campaign $campaign)
  116.     {
  117.         $profile $campaign->getUser()->getProfile();
  118.         return $profile->isVisibleForAll() && $profile->isFacebook() && $campaign->getId() && !$campaign->isFinished();
  119.     }
  120.     /**
  121.      * @param Campaign $campaign
  122.      * @return bool
  123.      */
  124.     protected function canShareTW(Campaign $campaign)
  125.     {
  126.         $profile $campaign->getUser()->getProfile();
  127.         return $profile->isVisibleForAll() && $profile->isTwitter() && $campaign->getId() && !$campaign->isFinished();
  128.     }
  129.     /**
  130.      * @param Campaign $campaign
  131.      * @param $user
  132.      * @return bool
  133.      */
  134.     protected function canSeeSummary(Campaign $campaign$user)
  135.     {
  136.         return $campaign->getUser() === $user;
  137.     }
  138.     /**
  139.      * @param Campaign $campaign
  140.      * @param $user
  141.      * @return bool
  142.      */
  143.     private function isAllowedAsRegistered(Campaign $campaign$user)
  144.     {
  145.         $campaignUser $campaign->getUser();
  146.         $campaignUserVisible $campaignUser->getProfile()->getVisible();
  147.         $isVisibilityRegistered $campaignUserVisible === UserProfile::VISIBILITY_REGISTRED;
  148.         $isRegisteredUser $user instanceof User && $user->isCompleted();
  149.         return $isVisibilityRegistered && $isRegisteredUser;
  150.     }
  151.     /**
  152.      * @param Campaign $campaign
  153.      * @param $user
  154.      * @return bool
  155.      */
  156.     private function isAllowedAsContact(Campaign $campaign$user)
  157.     {
  158.         $campaignUser $campaign->getUser();
  159.         $campaignUserVisible $campaignUser->getProfile()->getVisible();
  160.         $isVisibilityNon $campaignUserVisible === UserProfile::VISIBILITY_NON;
  161.         $isRegisteredUser $user instanceof User && $user->isCompleted();
  162.         $isContact false;
  163.         if ($isRegisteredUser && $campaignUser->hasContact($user)) {
  164.             $userContact $campaignUser->findContact($user);
  165.             if ($campaignContact $campaign->findContact($userContact)) {
  166.                 $isContact $campaignContact->isConfirmedContact();
  167.             }
  168.         }
  169.         return $isVisibilityNon && $isContact;
  170.     }
  171. }