src/LiteDesk/UserBundle/Security/Authorization/UserVoter.php line 21

Open in your IDE?
  1. <?php
  2. namespace LiteDesk\UserBundle\Security\Authorization;
  3. use LiteDesk\OfficeBundle\Entity\Team;
  4. use LiteDesk\UserBundle\Entity\Contract;
  5. use LiteDesk\UserBundle\Entity\User;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  11. use JMS\DiExtraBundle\Annotation\Service;
  12. use JMS\DiExtraBundle\Annotation\Tag;
  13. use JMS\DiExtraBundle\Annotation\Inject;
  14. use JMS\DiExtraBundle\Annotation\InjectParams;
  15. /**
  16.  * @Service
  17.  * @Tag("security.voter")
  18.  */
  19. class UserVoter extends Voter
  20. {
  21.     const CREATE 'CREATE';
  22.     const VIEW 'VIEW';
  23.     const EDIT 'EDIT';
  24.     const PROMOTE 'PROMOTE';
  25.     const DEMOTE 'DEMOTE';
  26.     const DELETE 'DELETE';
  27. //    /**
  28. //     * @var AuthorizationCheckerInterface
  29. //     */
  30. //    protected $authorizationChecker;
  31. //
  32. //    /**
  33. //     * @InjectParams({
  34. //     *     "authorizationChecker" = @Inject("security.authorization_checker")
  35. //     * })
  36. //     */
  37. //    public function setContainer(AuthorizationCheckerInterface $authorizationChecker)
  38. //    {
  39. //        $this->authorizationChecker = $authorizationChecker;
  40. //    }
  41.     protected function supports($attribute$subject)
  42.     {
  43.         if(!($subject instanceof User))
  44.         {
  45.             return false;
  46.         }
  47.         if(!in_array($attribute$this->getSupportedAttributes()))
  48.         {
  49.             return false;
  50.         }
  51.         return true;
  52.     }
  53.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  54.     {
  55.         /** @var $user User */
  56.         $user $token->getUser();
  57.         if($user->hasRole('GLOBAL_EMPLOYEE_ADMINISTRATION'))
  58.         {
  59.             return true;
  60.         }
  61.         /** @var $subject User */
  62.         if($user->hasRole('LOCAL_EMPLOYEE_ADMINISTRATION') && $subject && method_exists($subject'getOffice'))
  63.         {
  64.             $visibleUntilDateLast = new \DateTime('-3 year');
  65.             $now = new \DateTime('now');
  66. //          Here below we get the lastContractStartDate to have an identifier to find out if the contract we have is really the last
  67. //          If it is the last contract which is ended, there is the need to see or edit it for three month after ending
  68. //          If it is an other contract than the last it should not be seen anymore after entering an enddate which is in the past
  69. //
  70.             $lastContractStartDate $subject->getLastContract()->getStartDate();
  71.             $contracts $subject->getContracts();
  72.             /** @var $contract Contract */
  73.             foreach ($contracts as $contract)
  74.             {
  75.                     if
  76.                     (   !empty($contract->getOffice()) &&
  77.                         !empty($user->getOffice()) &&
  78.                         $contract->getOffice()->getId() == $user->getOffice()->getId() &&
  79.                             (empty($contract->getExitDate()) ||
  80.                             ($contract->getExitDate() > $now) ||
  81.                             (($lastContractStartDate == $contract->getStartDate()) && $contract->getExitDate()>$visibleUntilDateLast))
  82.                     )
  83.                     {
  84.                         return true;
  85.                     }
  86.             }
  87.         }
  88.         if (!($user instanceof AdvancedUserInterface)) {
  89.             return false;
  90.         }
  91.         if($user->hasRole('LOCAL_EMPLOYEE_ADMINISTRATION') && !$subject)
  92.         {
  93.             return true;
  94.         }
  95.         return false;
  96.     }
  97.     protected function getSupportedAttributes()
  98.     {
  99.         return array(self::CREATEself::VIEWself::EDITself::PROMOTEself::DELETEself::DEMOTE);
  100.     }
  101. }