src/Ox/HoardBundle/Security/Authorization/Voter/HoardVoter.php line 93

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: ouit0097
  5.  * Date: 24/06/15
  6.  * Time: 14:21
  7.  */
  8. namespace App\Ox\HoardBundle\Security\Authorization\Voter;
  9. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use NUCLEOS\UserBundle\Model\UserInterface;
  12. class HoardVoter implements VoterInterface
  13. {
  14.     const VIEW 'view';
  15.     const VIEW_COINS 'view_coins';
  16.     const VIEW_COINS_SUMMARY 'view_coins_summary';
  17.     const EDIT 'edit';
  18.     const DELETE 'delete';
  19.     private $supportedClass 'App\Ox\HoardBundle\Entity\Hoard';
  20.     public function supportsAttribute($attribute)
  21.     {
  22.         return in_array($attribute, array(
  23.             self::VIEW,
  24.             self::VIEW_COINS,
  25.             self::VIEW_COINS_SUMMARY,
  26.             self::EDIT,
  27.             self::DELETE,
  28.         ));
  29.     }
  30.     public function supportsClass($class)
  31.     {
  32.         return $this->supportedClass === $class || $this->isChildofSupportedClass($class);
  33.     }
  34.     public function isChildofSupportedClass($class)
  35.     {
  36.         return is_subclass_of($class$this->supportedClass);
  37.     }
  38.     // validate whether a particular user can access a particular hoard based on country and access grants
  39.     private function checkUserCanAccessHoardByCountry($user$hoard) {
  40.         // check for user exception access
  41.         foreach($user->getAccessibleHoards() as $userHoard) {
  42.             if($userHoard->getHoard() == $hoard)
  43.                 return true;
  44.         }
  45.         
  46.         $forbiddenCountryFlag false;
  47.         // check for country access
  48.         foreach($hoard->getCountries() as $country) {
  49.             if (!in_array($country$user->getAccessibleCountries()->toArray())){
  50.                 $forbiddenCountryFlag true;
  51.             }
  52.         }
  53.         if (!$forbiddenCountryFlag){
  54.             return true;
  55.         }
  56.         
  57.         return false;
  58.     }
  59.     
  60.     // the haord (or some part of it) is hidden from the user
  61.     private function getHoardIsHiddenFromUser($user$hoard) {
  62.         //public
  63.         if(!$user instanceof UserInterface || !($user->getId() > 0) ) {
  64.             return $hoard->getHideFrom()->getId() != 3// != 'none'
  65.         }
  66.         //admin
  67.         elseif($user && ( $user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_SUPER_ADMIN'))) {
  68.             return false;
  69.         }
  70.         //user
  71.         elseif($this->checkUserCanAccessHoardByCountry($user$hoard)) {
  72.             return false;
  73.         }
  74.         else {
  75.             return $hoard->getHideFrom()->getHideFrom() == 2//"public + collaborator";
  76.         }
  77.     }
  78.     /**
  79.      * @param TokenInterface $token      A TokenInterface instance
  80.      * @param object|null    $object     The hoard to secure
  81.      * @param array          $attributes An array of attributes associated with the method being invoked
  82.      *
  83.      * @return int either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED
  84.      */
  85.     public function vote(TokenInterface $token$object, array $attributes)
  86.     {       
  87.         // check if class of this object is supported by this voter
  88.         if (!is_object($object) || !$this->supportsClass(get_class($object))) {
  89.             return VoterInterface::ACCESS_ABSTAIN;
  90.         }
  91.         // check if the voter is used correct, only allow one attribute
  92.         // this isn't a requirement, it's just one easy way for you to
  93.         // design your voter
  94.         if (!== count($attributes)) {
  95.             throw new \InvalidArgumentException(
  96.                 'Only one attribute is allowed for VIEW or EDIT'
  97.             );
  98.         }
  99.         // set the attribute to check against
  100.         $attribute $attributes[0];
  101.         // check if the given attribute is covered by this voter
  102.         if (!$this->supportsAttribute($attribute)) {
  103.             return VoterInterface::ACCESS_ABSTAIN;
  104.         }
  105.         // get current logged in user
  106.         $user $token->getUser();
  107.         $hoard $object;
  108.         //not doing what we expect. Returns true when we are querying about a hoard.
  109.         // if($this->isChildofSupportedClass(get_class($object)))
  110.         //     $hoard = $object->getHoard();
  111.         switch($attribute) {
  112.             case self::VIEW:
  113.                 // check if hidden value is set
  114.                 $hideFromNone $hoard->getHideFrom()->getId() == 3;// || $hoard->getHideFrom()->getHideFrom()=='none';
  115.                 $hideAll $hoard->getHideWhat() && $hoard->getHideWhat()->getId() == 3;    //'all'
  116.                 if($hideFromNone || !$hideAll) {
  117.                     // check if marked as hidden
  118.                     if(!$hoard->getValidatedByUser())
  119.                     {
  120.                         if(!$user instanceof UserInterface || !($user->getId() > 0) ) 
  121.                         {
  122.                             return VoterInterface::ACCESS_DENIED;
  123.                         }
  124.                     }
  125.                     return VoterInterface::ACCESS_GRANTED;
  126.                 }
  127.                 // check if user is authenticated
  128.                 elseif(!$user instanceof UserInterface || !($user->getId() > 0) ) {
  129.                     return VoterInterface::ACCESS_DENIED;
  130.                 }
  131.                 elseif($user && ( $user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_SUPER_ADMIN'))) {
  132.                     return VoterInterface::ACCESS_GRANTED;
  133.                 }
  134.                 // check if user is the creator of the hoard
  135.                 elseif( $hoard->getCreated() && $user->getId() == $hoard->getCreated()->getId()) {
  136.                     return VoterInterface::ACCESS_GRANTED;
  137.                 }
  138.                 // check if user has access by country or specific access grant
  139.                 elseif($this->checkUserCanAccessHoardByCountry($user$hoard)) {
  140.                     return VoterInterface::ACCESS_GRANTED;
  141.                 }
  142.                 // check if hoard is hidden from users from other countries
  143.                 elseif($hoard->getHideFrom()->getId() == 2) { //"public + collaborator with view rights") {
  144.                     return VoterInterface::ACCESS_DENIED;
  145.                 }
  146.                 return VoterInterface::ACCESS_GRANTED;
  147.             case self::VIEW_COINS:
  148.                 if($this->getHoardIsHiddenFromUser($user$hoard))
  149.                 {
  150.                     $hideCoins $hoard->getHideWhat() && $hoard->getHideWhat()->getId() == 1//'the coins'
  151.                     $hideCoinsAndSummary $hoard->getHideWhat() && $hoard->getHideWhat()->getId() == 2//'the coins + the coin summary';
  152.                     if(!$hideCoins && !$hideCoinsAndSummary)
  153.                     {
  154.                         return VoterInterface::ACCESS_GRANTED;
  155.                     }
  156.                     return VoterInterface::ACCESS_DENIED;
  157.                 }
  158.                 return VoterInterface::ACCESS_GRANTED;
  159.                 
  160.             case self::VIEW_COINS_SUMMARY:
  161.                 if($this->getHoardIsHiddenFromUser($user$hoard))
  162.                 {
  163.                     $hideCoinsAndSummary $hoard->getHideWhat() && $hoard->getHideWhat()->getId() == 2;//'the coins + the coin summary';
  164.                     if(!$hideCoinsAndSummary)
  165.                     {
  166.                         return VoterInterface::ACCESS_GRANTED;
  167.                     }
  168.                     return VoterInterface::ACCESS_DENIED;
  169.                 }
  170.                 return VoterInterface::ACCESS_GRANTED;
  171.             case self::EDIT: case self::DELETE:
  172.                 // check if user is authenticated
  173.                 if(!$user instanceof UserInterface || !($user->getId() > 0) ) {
  174.                     return VoterInterface::ACCESS_DENIED;
  175.                 }
  176.                 // check if user is the creator of the hoard
  177.                 elseif( $hoard->getCreated() && $user->getId() == $hoard->getCreated()->getId()) {
  178.                     return VoterInterface::ACCESS_GRANTED;
  179.                 }
  180.                 // check if user has access by country or specific access grant
  181.                 elseif($this->checkUserCanAccessHoardByCountry($user$hoard)) {
  182.                     return VoterInterface::ACCESS_GRANTED;
  183.                 }
  184.                 elseif($user && ( $user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_SUPER_ADMIN'))) {
  185.                     return VoterInterface::ACCESS_GRANTED;
  186.                 }
  187.                 
  188.                 return VoterInterface::ACCESS_DENIED;
  189.         }
  190.         return VoterInterface::ACCESS_DENIED;
  191.     }
  192. }