| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- declare(strict_types=1);
- namespace App\Security\Voter;
- use App\Entity\Access\Access;
- use App\Entity\Core\BankAccount;
- use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
- use Symfony\Component\Security\Core\Authorization\Voter\Voter;
- use Symfony\Component\Security\Core\Security;
- use Symfony\Component\Security\Core\User\UserInterface;
- class BankAccountVoter extends Voter
- {
- private Security $security;
- public function __construct(Security $security)
- {
- $this->security = $security;
- }
- protected function supports($attribute, $subject): bool
- {
- return in_array($attribute, ['BANK_ACCOUNT_READ', 'BANK_ACCOUNT_EDIT'])
- && $subject instanceof BankAccount;
- }
- /**
- * @param string $attribute
- * @param mixed $subject
- * @param TokenInterface $token
- * @return bool
- */
- protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
- {
- /** @var Access $user */
- $user = $token->getUser();
- // if the user is anonymous, do not grant access
- if (!$user instanceof UserInterface) {
- return false;
- }
- switch ($attribute) {
- case 'BANK_ACCOUNT_READ':
- if($subject->getOrganization()->count() === 1){
- return $this->security->isGranted('ROLE_ORGANIZATION_VIEW')
- && $subject->getOrganization()->current()->getId() === $user->getOrganization()->getId();
- }
- break;
- case 'BANK_ACCOUNT_EDIT':
- if($subject->getOrganization()->count() === 1){
- return $this->security->isGranted('ROLE_ORGANIZATION')
- && $subject->getOrganization()->current()->getId() === $user->getOrganization()->getId();
- }
- break;
- }
- return false;
- }
- }
|