Skip to content

Commit

Permalink
dev: Allow to filter organizations by granted permission in Organizat…
Browse files Browse the repository at this point in the history
…ionType
  • Loading branch information
marien-probesys committed Aug 29, 2024
1 parent 31cb55d commit 21c9f07
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/Form/Type/OrganizationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use App\Entity;
use App\Repository;
use App\Security;
use App\Service;
use Symfony\Bridge\Doctrine\Form\Type;
use Symfony\Component\Form\AbstractType;
Expand All @@ -20,6 +21,7 @@ class OrganizationType extends AbstractType
{
public function __construct(
private Repository\OrganizationRepository $organizationRepository,
private Security\Authorizer $authorizer,
private Service\Sorter\OrganizationSorter $organizationSorter,
) {
}
Expand All @@ -30,19 +32,39 @@ public function configureOptions(OptionsResolver $resolver): void
'class' => Entity\Organization::class,

'choice_loader' => function (Options $options): ChoiceLoaderInterface {
$permission = $options['permission'];

$vary = [$permission];

return ChoiceList::lazy(
$this,
function () {
function () use ($permission) {
$organizations = $this->organizationRepository->findAll();

if ($permission) {
$organizations = array_filter(
$organizations,
function ($organization) use ($permission): bool {
return $this->authorizer->isGranted($permission, $organization);
}
);
}

$this->organizationSorter->sort($organizations);

return $organizations;
},
$vary,
);
},

'choice_label' => 'name',
'choice_value' => 'id',

'permission' => '',
]);

$resolver->setAllowedTypes('permission', 'string');
}

public function getParent(): string
Expand Down

0 comments on commit 21c9f07

Please sign in to comment.