<?php
declare(strict_types=1);
namespace App\Security\Voter\Profile\Payments;
use App\Model\User\Entity\User\Role\Permission;
use App\Model\User\Entity\User\Role\RoleConstants;
use App\Security\UserIdentity;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
class PaymentsVoter extends Voter
{
public const PAYMENT_LIST = 'payment_list';
public const PAYMENT_SHOW = 'payment_show';
public const PAYMENT_WITHDRAW = 'payment_withdraw';
public const PAYMENT_DEPOSIT = 'payment_deposit';
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [
self::PAYMENT_LIST,
self::PAYMENT_SHOW,
self::PAYMENT_WITHDRAW,
self::PAYMENT_DEPOSIT
], true);
}
/**
* @param string $attribute
* @param \App\ReadModel\Profile\Payment\DetailView $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
// Moderators have full access
if ($this->security->isGranted('ROLE_MODERATOR')) {
return true;
}
$user = $token->getUser();
// Only authenticated UserIdentity can proceed
if (!$user instanceof UserIdentity) {
return false;
}
// Only profile owner can proceed
if ($subject->profile_id !== $user->getProfileId()) {
return false;
}
$role = (new RoleConstants($user->getRole()));
if ($role->isOrganizerUser()) {
if ($this->checkPermissionEmployee($user, $attribute) === false) {
throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
}
}
// Check specific permissions
// switch ($attribute) {
// case self::PAYMENT_SHOW:
// return $user->isPermission(Permission::PAYMENT_SHOW);
//
// default:
// return false;
// }
return true;
}
/**
* Проверка разрешений сотрудника
* @param UserIdentity $user
* @param string $attribute
* @return bool
*/
private function checkPermissionEmployee(UserIdentity $user, string $attribute): bool
{
// Check specific permissions
switch ($attribute) {
case self::PAYMENT_WITHDRAW:
return $user->isPermission(Permission::PAYMENT_WITHDRAW);
case self::PAYMENT_SHOW:
return $user->isPermission(Permission::PAYMENT_SHOW);
case self::PAYMENT_DEPOSIT:
return $user->isPermission(Permission::PAYMENT_DEPOSIT);
default:
return true;
}
}
}