<?php
declare(strict_types=1);
/**
* ContractVoter.php File.
* This file is part of the Payment.net Project.
*
* PHP version 5
*
* @category Contract
* @package Bdm\BackofficeBundle\Security\Authorization\Voter
* @author Pavel Baraulya <pbaraulya@bdmultimedia.fr>
* @link http://www.payment.net/
*
* FEATURES :
* ==========
* Class specifies rules to access Contract entities
*
* TODO-LIST :
* ===========
* Is granted should be changed when multiple contracts will come foe merchant
*
* HISTORY :
* =========
* 20150821 - Helen Tochko: class creation
**/
namespace Bdm\BackofficeBundle\Security\Authorization\Voter;
use Bdm\AdminBundle\Entity\User;
use Bdm\BackofficeBundle\Entity\Contract;
use Bdm\BackofficeBundle\Entity\Merchant;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
/**
* Voter for contract entity
*/
class ContractVoter extends AbstractVoter
{
/**
* @param string $sAttribute attribute
* @param mixed $oSubject subject
* @return bool
*/
#[\Override]
public function supports($sAttribute, $oSubject)
{
return $oSubject instanceof Contract && parent::supports($sAttribute, $oSubject);
}
/**
* @param string $sAttr attr
* @param mixed $mEntity entity
* @param TokenInterface $oToken token
*
* @return bool
*/
protected function voteOnAttribute($sAttr, $mEntity, TokenInterface $oToken)
{
$oUser = $oToken->getUser();
if (true === $oUser instanceof User
&& true === $oUser->hasRole(User::ROLE_SUPER_ADMIN)
&& true === in_array($sAttr, ['VIEW', 'EDIT'])
) {
return true;
}
if (true === $oUser instanceof Merchant
&& $sAttr === 'VIEW'
&& $oUser->getContracts()->contains($mEntity)) {
return true;
}
return false;
}
}