src/FMT/Application/Voter/UserVoter.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 FOS\UserBundle\Model\UserInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. /**
  9.  * Class UserVoter
  10.  * @package FMT\Application\Voter
  11.  */
  12. class UserVoter extends Voter
  13. {
  14.     const CAN_ADD_CAMPAIGN 'canAddCampaign';
  15.     const CAN_SHARE_FB 'canShareUserFB';
  16.     const CAN_SHARE_TW 'canShareUserTW';
  17.     const CAN_SEE_PRIVATE_ELEMENTS 'canSeePrivateElements';
  18.     const CAN_DELETE_ACCOUNT 'canDeleteAccount';
  19.     const CAN_EDIT_CAMPAIGN 'canEditCampaign';
  20.     const CAN_USE_SCHOOL 'canUseSchool';
  21.     const AVAILABLE_METHODS = [
  22.         self::CAN_ADD_CAMPAIGN,
  23.         self::CAN_SHARE_FB,
  24.         self::CAN_SHARE_TW,
  25.         self::CAN_SEE_PRIVATE_ELEMENTS,
  26.         self::CAN_DELETE_ACCOUNT,
  27.         self::CAN_EDIT_CAMPAIGN,
  28.         self::CAN_USE_SCHOOL,
  29.     ];
  30.     /**
  31.      * @param string $attribute
  32.      * @param mixed $subject
  33.      * @return bool
  34.      * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  35.      */
  36.     protected function supports($attribute$subject)
  37.     {
  38.         if (!in_array($attributeself::AVAILABLE_METHODS)) {
  39.             return false;
  40.         }
  41.         return true;
  42.     }
  43.     /**
  44.      * @param string $attribute
  45.      * @param mixed $subject
  46.      * @param TokenInterface $token
  47.      * @return bool
  48.      * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  49.      */
  50.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  51.     {
  52.         /** @var User $user */
  53.         $user $token->getUser();
  54.         if (!$subject instanceof User) {
  55.             return false;
  56.         }
  57.         if (method_exists($this$attribute)) {
  58.             return $this->$attribute($subject$user);
  59.         }
  60.         return false;
  61.     }
  62.     /**
  63.      * @param User $owner
  64.      * @param User|string $currentUser
  65.      * @return bool
  66.      */
  67.     protected function canAddCampaign(User $owner$currentUser)
  68.     {
  69.         $isSchoolActive $owner->isStudent() && $owner->getProfile()->getSchool()->isActive();
  70.         // to prevent more than one campaign add this to return:  && !$owner->hasFinishedCampaign()
  71.         return $owner === $currentUser && !$owner->hasUnfinishedCampaign() && $isSchoolActive;
  72.     }
  73.     /**
  74.      * @param User $owner
  75.      * @return bool
  76.      */
  77.     protected function canShareUserFB(User $owner)
  78.     {
  79.         $profile $owner->getProfile();
  80.         return $profile->isVisibleForAll() && $profile->isFacebook();
  81.     }
  82.     /**
  83.      * @param User $owner
  84.      * @return bool
  85.      */
  86.     protected function canShareUserTW(User $owner)
  87.     {
  88.         $profile $owner->getProfile();
  89.         return $profile->isVisibleForAll() && $profile->isTwitter();
  90.     }
  91.     /**
  92.      * @param User $owner
  93.      * @param User|string $currentUser
  94.      * @return bool
  95.      */
  96.     protected function canSeePrivateElements(User $owner$currentUser)
  97.     {
  98.         return $owner === $currentUser;
  99.     }
  100.     /**
  101.      * @param UserInterface $user
  102.      * @return bool
  103.      */
  104.     protected function canDeleteAccount(UserInterface $user)
  105.     {
  106.         $campaign $user->getUnfinishedCampaign();
  107.         if (null === $campaign) {
  108.             return true;
  109.         }
  110.         return !$campaign->isPositiveBalance();
  111.     }
  112.     /**
  113.      * @param Campaign $campaign
  114.      * @param User|string $user
  115.      * @return bool
  116.      */
  117.     protected function canEditCampaign(User $currentUser$user)
  118.     {
  119.         $isSchoolActive $user->isStudent() && $user->getProfile()->getSchool()->isActive();
  120.         return $user === $currentUser && $currentUser->getUnfinishedCampaign() && $isSchoolActive;
  121.     }
  122.     protected function canUseSchool(User $ownerCampaign $campaign)
  123.     {
  124.         return $campaign->getSchool()->isActive();
  125.     }
  126. }