src/Knitink/ManagementBundle/Security/ClientVoter.php line 17

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the Knitink Project package.
  4.  *
  5.  * Created by Globunet Soluciones Tecnologicas, SL. on 13/01/17. <info@globunet.com>
  6.  * Copyright (c) 2016 Knitink Technologies, SL. All rights reserved.
  7.  */
  8. namespace Knitink\ManagementBundle\Security;
  9. use Knitink\ManagementBundle\Domain\ClientManager;
  10. use Knitink\ManagementBundle\Entity\Client;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. class ClientVoter extends Voter
  14. {
  15.     private $manager;
  16.     public function __construct(ClientManager $manager)
  17.     {
  18.         $this->manager $manager;
  19.     }
  20.     protected function supports($attribute$subject)
  21.     {
  22.         return $subject instanceof Client && $attribute === 'VOTER_KNITINK_MANAGEMENT_CLIENT';
  23.     }
  24.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  25.     {
  26.         $user $token->getUser();
  27.         if(($user instanceof \Symfony\Component\Security\Core\User\User || $user instanceof \Globunet\UserBundle\Model\UserInterface) && in_array("ROLE_SUPER_ADMIN"$user->getRoles())){
  28.             return true;
  29.         }
  30.         if (!$user instanceof \Knitink\CoreBundle\Entity\User) {
  31.             return false;
  32.         }
  33.         $ids $this->manager->searchIdsBy($user);
  34.         $result $this->checker($ids$attribute$subject);
  35.         return $result;
  36.     }
  37.     public function checker($ids$attribute$subject)
  38.     {
  39.         $result false;
  40.         if($subject != null && in_array($subject->getId(), $ids) ){
  41.             $result true;
  42.         }
  43.         return $result;
  44.     }
  45. }