Skip to content

Commit

Permalink
Retravaille et clarifie la distinction userOrganization / organizatio…
Browse files Browse the repository at this point in the history
…nUser dans le code (#929)

* Retravaille la distinction userOrganization/organizationUSer

* Fix typo

* Use views in repository
  • Loading branch information
florimondmanca authored Aug 29, 2024
1 parent 13fa51e commit e2274f3
Show file tree
Hide file tree
Showing 31 changed files with 135 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
final class DeleteRegulationCommand implements CommandInterface
{
public function __construct(
public readonly array $organizationUserUuids,
public readonly array $userOrganizationUuids,
public readonly RegulationOrderRecord $regulationOrderRecord,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __invoke(DeleteRegulationCommand $command): void
{
$regulationOrderRecord = $command->regulationOrderRecord;

if (false === $this->canOrganizationAccessToRegulation->isSatisfiedBy($regulationOrderRecord, $command->organizationUserUuids)) {
if (false === $this->canOrganizationAccessToRegulation->isSatisfiedBy($regulationOrderRecord, $command->userOrganizationUuids)) {
throw new RegulationOrderRecordCannotBeDeletedException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public function __construct(
public function __invoke(DeleteOrganizationUserCommand $command): void
{
$user = $command->organizationUser->getUser();
$organizationUsers = $this->organizationUserRepository->findOrganizationsByUser($user);
$userOrganizations = $this->organizationUserRepository->findByUserUuid($user->getUuid());

if (\count($organizationUsers) === 1) {
// User belongs to only one organization, he is totaly removed
if (\count($userOrganizations) === 1) {
// User belongs to only one organization, remove them and the organization user will be deleted too by CASCADE
$this->userRepository->remove($user);
} else {
// User belongs to several organizations, he is removed from the organization
// User belongs to several organizations, remove only their presence in this organization
$this->organizationUserRepository->remove($command->organizationUser);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function __construct(

public function __invoke(GetOrganizationUsersQuery $query): array
{
return $this->organizationUserRepository->findUsersByOrganizationUuid($query->organizationUuid);
return $this->organizationUserRepository->findByOrganizationUuid($query->organizationUuid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Application\User\View;

final readonly class UserView
final readonly class OrganizationUserView
{
public function __construct(
public string $uuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Application\User\View;

final readonly class OrganizationView
final readonly class UserOrganizationView
{
public function __construct(
public string $uuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class CanOrganizationAccessToRegulation
{
public function isSatisfiedBy(
OrganizationRegulationAccessInterface|string $organizationUuid,
array $organizationUserUuids,
array $organizationUuids,
): bool {
if (!\is_string($organizationUuid)) {
$organizationUuid = $organizationUuid->getOrganizationUuid();
}

return \in_array($organizationUuid, $organizationUserUuids);
return \in_array($organizationUuid, $organizationUuids);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@

namespace App\Domain\User\Repository;

use App\Application\User\View\OrganizationUserView;
use App\Application\User\View\UserOrganizationView;
use App\Domain\User\OrganizationUser;
use App\Domain\User\User;

interface OrganizationUserRepositoryInterface
{
public function add(OrganizationUser $organizationUser): void;

public function remove(OrganizationUser $organizationUser): void;

public function findOrganizationsByUser(User $user): array;
/** @return UserOrganizationView[] */
public function findByUserUuid(string $userUuid): array;

public function findUsersByOrganizationUuid(string $uuid): array;
/** @return OrganizationUserView[] */
public function findByOrganizationUuid(string $uuid): array;

public function findOrganizationUser(string $organizationUuid, string $userUuid): ?OrganizationUser;

Expand Down
6 changes: 3 additions & 3 deletions src/Domain/User/Specification/CanUserEditOrganization.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class CanUserEditOrganization
{
public function isSatisfiedBy(Organization $organization, SymfonyUser $user): bool
{
foreach ($user->getOrganizationUsers() as $organizationUser) {
if ($organizationUser->uuid !== $organization->getUuid()) {
foreach ($user->getUserOrganizations() as $userOrganization) {
if ($userOrganization->uuid !== $organization->getUuid()) {
continue;
}

return \in_array(OrganizationRolesEnum::ROLE_ORGA_ADMIN->value, $organizationUser->roles);
return \in_array(OrganizationRolesEnum::ROLE_ORGA_ADMIN->value, $userOrganization->roles);
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/User/Specification/CanUserPublishRegulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function isSatisfiedBy(RegulationOrderRecord $regulationOrderRecord, Symf
{
$organization = $regulationOrderRecord->getOrganization();

foreach ($user->getOrganizationUsers() as $userOrganization) {
foreach ($user->getUserOrganizations() as $userOrganization) {
if ($userOrganization->uuid !== $organization->getUuid()) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/User/Specification/CanUserViewOrganization.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class CanUserViewOrganization
{
public function isSatisfiedBy(Organization $organization, SymfonyUser $user): bool
{
return \in_array($organization->getUuid(), $user->getOrganizationUuids());
return \in_array($organization->getUuid(), $user->getUserOrganizationUuids());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __invoke(): Response
return new Response($this->twig->render(
name: 'organization/index.html.twig',
context: [
'organizations' => $user->getOrganizationUsers(),
'organizations' => $user->getUserOrganizations(),
],
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function getRegulationOrderRecordUsing(callable $func, bool $requireUs
/** @var SymfonyUser|null */
$user = $this->security->getUser();

if (!$user || !$this->canOrganizationAccessToRegulation->isSatisfiedBy($regulationOrderRecord, $user->getOrganizationUuids())) {
if (!$user || !$this->canOrganizationAccessToRegulation->isSatisfiedBy($regulationOrderRecord, $user->getUserOrganizationUuids())) {
throw new AccessDeniedHttpException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __invoke(Request $request): Response
type: GeneralInfoFormType::class,
data: $command,
options: [
'organizations' => $user->getOrganizationUsers(),
'organizations' => $user->getUserOrganizations(),
'action' => $this->router->generate('app_regulation_add'),
'save_options' => [
'label' => 'common.form.continue',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __invoke(Request $request, string $uuid): Response
}

try {
$this->commandBus->handle(new DeleteRegulationCommand($user->getOrganizationUuids(), $regulationOrderRecord));
$this->commandBus->handle(new DeleteRegulationCommand($user->getUserOrganizationUuids(), $regulationOrderRecord));
} catch (RegulationOrderRecordCannotBeDeletedException) {
throw new AccessDeniedHttpException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __invoke(string $regulationOrderRecordUuid, string $uuid): Respo
context: [
'measure' => MeasureView::fromEntity($measure),
'generalInfo' => $generalInfo,
'isReadOnly' => !$this->canOrganizationAccessToRegulation->isSatisfiedBy($regulationOrderRecord, $currentUser->getOrganizationUuids()),
'isReadOnly' => !$this->canOrganizationAccessToRegulation->isSatisfiedBy($regulationOrderRecord, $currentUser->getUserOrganizationUuids()),
'canDelete' => $this->canDeleteMeasures->isSatisfiedBy($regulationOrderRecord),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __invoke(Request $request, string $uuid): Response
type: GeneralInfoFormType::class,
data: $command,
options: [
'organizations' => $user->getOrganizationUsers(),
'organizations' => $user->getUserOrganizations(),
'action' => $this->router->generate('fragment_regulations_general_info_form', ['uuid' => $uuid]),
'save_options' => [
'label' => 'common.form.validate',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public function __invoke(Request $request): Response
);
}

$organizationUserUuids = $user?->getOrganizationUuids();
$userOrganizationUuids = $user?->getUserOrganizationUuids();

$regulations = $this->queryBus->handle(
new GetRegulationsQuery(
pageSize: $pageSize,
page: $page,
organizationUuids: $organizationUserUuids,
organizationUuids: $userOrganizationUuids,
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __invoke(string $uuid): Response
$regulationOrderRecord = $this->getRegulationOrderRecord($uuid, requireUserSameOrg: false);
$organizationUuid = $regulationOrderRecord->getOrganizationUuid();
$measures = $this->queryBus->handle(new GetMeasuresQuery($uuid));
$isReadOnly = !($currentUser && $this->canOrganizationAccessToRegulation->isSatisfiedBy($organizationUuid, $currentUser->getOrganizationUuids()));
$isReadOnly = !($currentUser && $this->canOrganizationAccessToRegulation->isSatisfiedBy($organizationUuid, $currentUser->getUserOrganizationUuids()));

$context = [
'uuid' => $uuid,
Expand Down
10 changes: 5 additions & 5 deletions src/Infrastructure/Form/Regulation/GeneralInfoFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Infrastructure\Form\Regulation;

use App\Application\User\View\OrganizationView;
use App\Application\User\View\UserOrganizationView;
use App\Domain\Regulation\Enum\RegulationOrderCategoryEnum;
use App\Domain\User\Organization;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -102,13 +102,13 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

$builder->get('organization')
->addModelTransformer(new CallbackTransformer(
function (?Organization $organization = null): ?OrganizationView {
function (?Organization $organization = null): ?UserOrganizationView {
return $organization
? new OrganizationView($organization->getUuid(), $organization->getName())
? new UserOrganizationView($organization->getUuid(), $organization->getName())
: null;
},
function (OrganizationView $organizationView): Organization {
return $this->entityManager->getReference(Organization::class, $organizationView->uuid);
function (UserOrganizationView $view): Organization {
return $this->entityManager->getReference(Organization::class, $view->uuid);
},
))
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

namespace App\Infrastructure\Persistence\Doctrine\Repository\User;

use App\Application\User\View\OrganizationView;
use App\Application\User\View\UserView;
use App\Application\User\View\OrganizationUserView;
use App\Application\User\View\UserOrganizationView;
use App\Domain\User\OrganizationUser;
use App\Domain\User\Repository\OrganizationUserRepositoryInterface;
use App\Domain\User\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

Expand All @@ -30,28 +29,39 @@ public function remove(OrganizationUser $organizationUser): void
$this->getEntityManager()->remove($organizationUser);
}

public function findOrganizationsByUser(User $user): array
public function findByUserUuid(string $uuid): array
{
return $this->createQueryBuilder('ou')
->select(
\sprintf('NEW %s(o.uuid, o.name, ou.roles)', OrganizationView::class),
)
->where('ou.user = :user')
->select(\sprintf(
'NEW %s(
o.uuid,
o.name,
ou.roles
)',
UserOrganizationView::class,
))
->where('ou.user = :userUuid')
->innerJoin('ou.organization', 'o')
->setParameter('user', $user)
->setParameter('userUuid', $uuid)
->getQuery()
->getResult();
}

public function findUsersByOrganizationUuid(string $uuid): array
public function findByOrganizationUuid(string $uuid): array
{
return $this->createQueryBuilder('ou')
->select(
\sprintf('NEW %s(u.uuid, u.fullName, u.email, ou.roles)', UserView::class),
)
->where('ou.organization = :organization')
->select(\sprintf(
'NEW %s(
u.uuid,
u.fullName,
u.email,
ou.roles
)',
OrganizationUserView::class,
))
->where('ou.organization = :organizationUuid')
->innerJoin('ou.user', 'u')
->setParameter('organization', $uuid)
->setParameter('organizationUuid', $uuid)
->orderBy('u.fullName', 'ASC')
->getQuery()
->getResult();
Expand Down
4 changes: 2 additions & 2 deletions src/Infrastructure/Security/Provider/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public function loadUserByIdentifier(string $identifier): UserInterface
throw new UserNotFoundException(\sprintf('Unable to find the user %s', $identifier));
}

$organizationUsers = $this->organizationUserRepositoryInterface->findOrganizationsByUser($user);
$userOrganizations = $this->organizationUserRepositoryInterface->findByUserUuid($user->getUuid());

return new SymfonyUser(
$user->getUuid(),
$user->getEmail(),
$user->getFullName(),
$user->getPassword(),
$organizationUsers,
$userOrganizations,
$user->getRoles(),
);
}
Expand Down
23 changes: 14 additions & 9 deletions src/Infrastructure/Security/SymfonyUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Infrastructure\Security;

use App\Application\User\View\OrganizationView;
use App\Application\User\View\UserOrganizationView;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

Expand All @@ -15,7 +15,8 @@ public function __construct(
private string $email,
private string $fullName,
private string $password,
private array $organizationUsers,
/** @var UserOrganizationView[] */
private array $userOrganizations,
private array $roles,
) {
}
Expand Down Expand Up @@ -55,17 +56,21 @@ public function getPassword(): string
return $this->password;
}

public function getOrganizationUsers(): array
/** @return UserOrganizationView[] */
public function getUserOrganizations(): array
{
return $this->organizationUsers;
return $this->userOrganizations;
}

public function getOrganizationUuids(): array
public function getUserOrganizationUuids(): array
{
return array_map(
function (OrganizationView $organizationUser) { return $organizationUser->uuid; },
$this->organizationUsers,
);
$uuids = [];

foreach ($this->userOrganizations as $org) {
$uuids[] = $org->uuid;
}

return $uuids;
}

public function eraseCredentials(): void
Expand Down
6 changes: 3 additions & 3 deletions templates/regulation/_items.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{% if isAuthenticated %}
{% set duplicateCsrfToken = csrf_token('duplicate-regulation') %}
{% set deleteCsrfToken = csrf_token('delete-regulation') %}
{% set organizationUsersUuids = app.user.getOrganizationUuids %}
{% set userOrganizationUuids = app.user.getUserOrganizationUuids %}
{% else %}
{% set organizationUsersUuids = [] %}
{% set userOrganizationUuids = [] %}
{% endif %}

<div class="fr-table fr-table--layout-fixed fr-table--no-caption">
Expand All @@ -25,7 +25,7 @@
{% set location = regulation.location %}
{% set isUpcoming = regulation.startDate ? app_is_client_future_day(regulation.startDate) : null %}
{% set hasPassed = regulation.endDate ? app_is_client_past_day(regulation.endDate) : null %}
{% set belongsToOrganizationUsers = isAuthenticated and (regulation.organizationUuid in organizationUsersUuids) %}
{% set belongsToOrganizationUsers = isAuthenticated and (regulation.organizationUuid in userOrganizationUuids) %}
<tr>
<td>
{{ regulation.identifier }}
Expand Down
Loading

0 comments on commit e2274f3

Please sign in to comment.