<?php
namespace App\Security;
use App\Entity\BankUser;
use App\Entity\OA2User;
use App\Repository\BankPermissionRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class BankPermissionVoter extends Voter
{
public const MODE_READ = 'read';
public const MODE_CREATE = 'create';
public const MODE_UPDATE = 'update';
public const MODE_DELETE = 'delete';
public const PERMISSION_TRANSACTIONS = 'PERMISSION_TRANSACTIONS';
public const PERMISSION_CLIENTS = 'PERMISSION_CLIENTS';
public const PERMISSION_ACCOUNTS = 'PERMISSION_ACCOUNTS';
public const PERMISSION_CLIENT_DOCS = 'PERMISSION_CLIENT_DOCS';
public const PERMISSION_ORG_SETS = 'PERMISSION_ORG_SETS';
public const PERMISSION_PRODUCTS = 'PERMISSION_PRODUCTS';
public const PERMISSION_RULES = 'PERMISSION_RULES';
public const PERMISSION_LIQUIDITY = 'PERMISSION_LIQUIDITY';
public const PERMISSION_ANY = 'PERMISSION_ANY';
public const PERMISSION_AML = 'PERMISSION_AML';
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var BankPermissionRepository
*/
private $bankPermissionRepository;
public function __construct(EntityManagerInterface $entityManager, BankPermissionRepository $bankPermissionRepository)
{
$this->entityManager = $entityManager;
$this->bankPermissionRepository = $bankPermissionRepository;
}
protected function supports($attribute, $subject)
{
if (!in_array($attribute, [self::MODE_READ, self::MODE_CREATE, self::MODE_UPDATE, self::MODE_DELETE])) {
return false;
}
return !(gettype($subject) !== 'string')
;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var OA2User $user */
$user = $token->getUser();
if (!$user instanceof OA2User || !$user->isBankUser()) {
// the user must be logged in; if not, deny access
return false;
}
/** @var string $permissionName */
$permissionName = $subject;
/** @var BankUser $bankUser */
$bankUser = $user->getBankUser();
switch ($attribute) {
case self::MODE_READ:
return $this->canRead($permissionName, $bankUser);
break;
case self::MODE_CREATE:
return $this->canCreate($permissionName, $bankUser);
break;
case self::MODE_UPDATE:
return $this->canUpdate($permissionName, $bankUser);
break;
case self::MODE_DELETE:
return $this->canDelete($permissionName, $bankUser);
break;
}
// throw new \LogicException( 'This code should not be reached');
}
private function canRead(string $permissionName, BankUser $bankUser): bool
{
$permission = $permissionName === self::PERMISSION_ANY
? $this->bankPermissionRepository->findByReadAnyPermission($bankUser)
: $this->bankPermissionRepository->findByReadPermission($permissionName, $bankUser);
return (bool) $permission;
}
private function canCreate(string $permissionName, BankUser $bankUser): bool
{
return ($this->bankPermissionRepository->findByCreatePermission($permissionName, $bankUser))
? true
: false
;
}
private function canUpdate(string $permissionName, BankUser $bankUser): bool
{
return ($this->bankPermissionRepository->findByUpdatePermission($permissionName, $bankUser))
? true
: false
;
}
private function canDelete(string $permissionName, BankUser $bankUser): bool
{
return ($this->bankPermissionRepository->findByDeletePermission($permissionName, $bankUser))
? true
: false
;
}
}