<?php
namespace App\Security\Voter\Permissions\Admin;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Security\Core\User\UserInterface;
class AdminOrganizationVoter extends AdminVoter
{
/** @todo: Documentation on relations on user profiles */
protected function getUserProfiles(UserInterface $user, string $organizationId): array
{
$heimdallUser = $this->userManager->findUser($user->getUsername());
$userProfiles = $this->userProfileManager->getUserProfiles($heimdallUser);
try {
$organization = $this->organizationManager->getOrganization($organizationId);
} catch (BadRequestHttpException $e) {
$organization = null;
}
$contractUserProfiles = [];
if ($organization) {
$contractId = $organization->getContract();
$contractUserProfiles = array_filter(
$userProfiles,
fn ($userProfile) => in_array($contractId, $userProfile->getContracts())
);
}
$organizationUserProfiles = array_filter(
$userProfiles,
fn ($userProfile) => in_array(
$organizationId,
$userProfile->getOrganizations()
) || !empty($userProfile->getPortals())
);
return array_merge($organizationUserProfiles, $contractUserProfiles);
}
}