<?php
/**
* Copyright (C) SUEZ Smart Solutions - All Rights Reserved
* On’Connect Gateway Management, 2018
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* For the full copyright and license information, please report to the LICENSE CONTRACT.
*/
namespace TSMS\TradeBundle\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use TSMS\CoreBundle\Entity\Eservice;
use TSMS\CoreBundle\Entity\User;
use TSMS\CoreBundle\Repository\UserContractRepository;
/**@todo check use / delete */
class CanAddFileVoter implements VoterInterface
{
const DOCUMENT_CAN_ADD_FILE = 'DOCUMENT_CAN_ADD_FILE';
/**
* @var UserContractRepository
*/
private $userContractRepository;
/**
* @param UserContractRepository $userContractRepo
*/
public function __construct(UserContractRepository $userContractRepo)
{
$this->userContractRepository = $userContractRepo;
}
/**
* {@inheritdoc}
*/
public function supportsAttribute($attribute)
{
return in_array($attribute, [self::DOCUMENT_CAN_ADD_FILE]);
}
/**
* {@inheritdoc}
*/
public function supportsClass($class)
{
$supportedClass = 'TSMS\CoreBundle\Entity\Contract';
return $supportedClass === $class || is_subclass_of($class, $supportedClass);
}
/**
* @param TokenInterface $token
* @param null|object $contract
* @param array $attributes
*
* @return int
*/
public function vote(TokenInterface $token, $contract, array $attributes)
{
// check if the class of this object is supported by this voter
if (
is_array($contract)
|| (null !== $contract && !$this->supportsClass(get_class($contract)))
) {
return VoterInterface::ACCESS_ABSTAIN;
}
// check if the voter is used correctly, only allow one attribute
// this isn't a requirement, it's just one easy way for you to
// design your voter
if (1 !== count($attributes)) {
throw new \InvalidArgumentException(
'Only one attribute is allowed for DOCUMENT_CAN_ADD_FILE'
);
}
// set the attribute to check against
$attribute = $attributes[0];
// check if the given attribute is covered by this voter
if (!$this->supportsAttribute($attribute)) {
return VoterInterface::ACCESS_ABSTAIN;
}
// get current logged in user
$user = $token->getUser();
// make sure there is a user object (i.e. that the user is logged in)
if (!$user instanceof User) {
return VoterInterface::ACCESS_DENIED;
}
switch ($attribute) {
case self::DOCUMENT_CAN_ADD_FILE:
// When there are multiple document spaces, this should be changed to test each corresponding eservice
$hasRoleDocumentWrite = $this->userContractRepository->hasEserviceInContractForUser(
Eservice::OUR_DOCUMENTS_WRITE_MNEMONIC,
$contract->getId(),
$user
);
if ($hasRoleDocumentWrite) {
return VoterInterface::ACCESS_GRANTED;
}
break;
}
return VoterInterface::ACCESS_DENIED;
}
}