<?php
/**
* This file is part of the Knitink Project package.
*
* Created by Globunet Soluciones Tecnologicas, SL. on 13/01/17. <info@globunet.com>
* Copyright (c) 2016 Knitink Technologies, SL. All rights reserved.
*/
namespace Knitink\ManagementBundle\Security;
use Knitink\ManagementBundle\Domain\TeamManager;
use Knitink\ManagementBundle\Entity\Team;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\HttpFoundation\Session\Session;
use Doctrine\ORM\EntityManager;
class TeamVoter extends Voter
{
private $manager;
public function __construct(TeamManager $manager)
{
$this->manager = $manager;
}
protected function supports($attribute, $subject)
{
return $subject instanceof Team && $attribute === 'VOTER_KNITINK_MANAGEMENT_TEAM';
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if(($user instanceof \Symfony\Component\Security\Core\User\User || $user instanceof \Globunet\UserBundle\Model\UserInterface) && in_array("ROLE_SUPER_ADMIN", $user->getRoles())){
return true;
}
if (!$user instanceof \Knitink\CoreBundle\Entity\User) {
return false;
}
$ids = $this->manager->searchIdsBy($user);
$result = $this->checker($ids, $attribute, $subject);
return $result;
}
public function checker($ids, $attribute, $subject)
{
$result = false;
if($subject != null && in_array($subject->getId(), $ids) ){
$result = true;
}
return $result;
}
}