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; } }