Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete invitation #1203

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="App\Application\User\Command\CreateInvitationCommand">
<class name="App\Application\User\Command\Invitation\CreateInvitationCommand">
<property name="fullName">
<constraint name="NotBlank"/>
<constraint name="Length">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Application\User\Command;
namespace App\Application\User\Command\Invitation;

use App\Application\CommandInterface;
use App\Domain\User\Organization;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Application\User\Command;
namespace App\Application\User\Command\Invitation;

use App\Application\DateUtilsInterface;
use App\Application\IdFactoryInterface;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Application\User\Command\Invitation;

use App\Application\CommandInterface;
use App\Infrastructure\Security\SymfonyUser;

final readonly class DeleteInvitationCommand implements CommandInterface
{
public function __construct(
public string $invitationUuid,
public SymfonyUser $user,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Application\User\Command\Invitation;

use App\Domain\User\Exception\InvitationNotFoundException;
use App\Domain\User\Exception\InvitationNotOwnedException;
use App\Domain\User\Invitation;
use App\Domain\User\Repository\InvitationRepositoryInterface;
use App\Domain\User\Specification\CanUserEditOrganization;

final readonly class DeleteInvitationCommandHandler
{
public function __construct(
private InvitationRepositoryInterface $invitationRepository,
private CanUserEditOrganization $canUserEditOrganization,
) {
}

public function __invoke(DeleteInvitationCommand $command): string
{
$invitation = $this->invitationRepository->findOneByUuid($command->invitationUuid);
if (!$invitation instanceof Invitation) {
throw new InvitationNotFoundException();
}

$organizationUuid = $invitation->getOrganization()->getUuid();

if (!$this->canUserEditOrganization->isSatisfiedBy($invitation->getOrganization(), $command->user)) {
throw new InvitationNotOwnedException();
}

$this->invitationRepository->delete($invitation);

return $organizationUuid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Application\User\Command;
namespace App\Application\User\Command\Invitation;

use App\Application\CommandInterface;
use App\Domain\User\User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Application\User\Command;
namespace App\Application\User\Command\Invitation;

use App\Application\IdFactoryInterface;
use App\Domain\User\Exception\InvitationNotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace App\Infrastructure\Controller\MyArea\Organization\User;

use App\Application\CommandBusInterface;
use App\Application\User\Command\JoinOrganizationCommand;
use App\Application\User\Command\Invitation\JoinOrganizationCommand;
use App\Domain\User\Exception\InvitationNotFoundException;
use App\Domain\User\Exception\InvitationNotOwnedException;
use App\Domain\User\Exception\OrganizationUserAlreadyExistException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use App\Application\CommandBusInterface;
use App\Application\QueryBusInterface;
use App\Application\User\Command\CreateInvitationCommand;
use App\Application\User\Command\Invitation\CreateInvitationCommand;
use App\Application\User\Command\Mail\SendInvitationMailCommand;
use App\Domain\User\Exception\InvitationAlreadyExistsException;
use App\Domain\User\Exception\OrganizationUserAlreadyExistException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace App\Infrastructure\Controller\MyArea\Organization\User;

use App\Application\CommandBusInterface;
use App\Application\User\Command\Invitation\DeleteInvitationCommand;
use App\Domain\User\Exception\InvitationNotFoundException;
use App\Domain\User\Exception\InvitationNotOwnedException;
use App\Infrastructure\Security\AuthenticatedUser;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Requirement\Requirement;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Http\Attribute\IsCsrfTokenValid;
use Symfony\Contracts\Translation\TranslatorInterface;

final class DeleteInvitationController
{
public function __construct(
private CommandBusInterface $commandBus,
private RouterInterface $router,
private AuthenticatedUser $authenticatedUser,
private TranslatorInterface $translator,
) {
}

#[Route(
'/invitations/{uuid}/delete',
name: 'app_invitation_delete',
requirements: ['uuid' => Requirement::UUID],
methods: ['DELETE'],
)]
#[IsCsrfTokenValid('delete-invitation')]
public function __invoke(Request $request, string $uuid): Response
{
/** @var FlashBagAwareSessionInterface */
$session = $request->getSession();
$user = $this->authenticatedUser->getSymfonyUser();

try {
$organizationUuid = $this->commandBus->handle(new DeleteInvitationCommand($uuid, $user));
$session->getFlashBag()->add('success', $this->translator->trans('invitation.delete.success'));

return new RedirectResponse(
url: $this->router->generate('app_users_list', ['uuid' => $organizationUuid]),
status: Response::HTTP_SEE_OTHER,
);
} catch (InvitationNotFoundException) {
throw new NotFoundHttpException();
} catch (InvitationNotOwnedException) {
$session->getFlashBag()->add('error', $this->translator->trans('invitation.delete.error'));

return new RedirectResponse(
url: $this->router->generate('app_my_area'),
status: Response::HTTP_SEE_OTHER,
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function load(ObjectManager $manager): void
email: '[email protected]',
role: OrganizationRolesEnum::ROLE_ORGA_CONTRIBUTOR->value,
createdAt: new \DateTimeImmutable('2025-02-12'),
owner: $this->getReference('mainOrgUser', User::class),
owner: $this->getReference('mainOrgAdmin', User::class),
organization: $this->getReference('mainOrg', Organization::class),
);

Expand Down
28 changes: 28 additions & 0 deletions templates/my_area/organization/user/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{% endblock %}

{% set deleteCsrfToken = csrf_token('delete-user') %}
{% set deleteInvitationCsrfToken = csrf_token('delete-invitation') %}

{% block body %}
<section class="fr-container fr-py-5w" aria-labelledby="user-list">
Expand Down Expand Up @@ -54,7 +55,26 @@
<td>
{% if is_granted(constant('App\\Infrastructure\\Security\\Voter\\OrganizationVoter::EDIT'), organization) %}
<div class="fr-btns-group fr-btns-group--inline-sm">
<form
method="POST"
action="{{ path('app_invitation_delete', { uuid: invitation.uuid }) }}"
data-controller="form-submit"
data-action="modal-trigger:submit->form-submit#submit"
class="fr-hidden fr-unhidden-md"
>
<input type="hidden" name="_method" value="DELETE">

<d-modal-trigger modal="invitation-delete-modal" submitValue="invitation-delete-{{ invitation.uuid }}">
<button
class="fr-btn fr-btn--tertiary-no-outline fr-icon-delete-bin-line"
aria-controls="invitation-delete-modal"
aria-label="{{ 'invitation.list.delete'|trans }}"
title="{{ 'invitation.list.delete'|trans }}"
></button>
</d-modal-trigger>

<input type="hidden" name="_token" value="{{ deleteInvitationCsrfToken }}" />
</form>
</div>
{% endif %}
</td>
Expand Down Expand Up @@ -120,4 +140,12 @@
{ label: 'common.do_not_delete'|trans, attr: {value: 'close', class: 'fr-btn fr-btn--secondary'} },
]
} only %}
{% include 'common/confirmation_modal.html.twig' with {
id: 'invitation-delete-modal',
title: 'invitation.list.delete'|trans,
buttons: [
{ label: 'common.delete'|trans, attr: {type: 'submit', class: 'fr-btn'} },
{ label: 'common.do_not_delete'|trans, attr: {value: 'close', class: 'fr-btn fr-btn--secondary'} },
]
} only %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace App\Tests\Integration\Infrastructure\Controller\MyArea\Organization\User;

use App\Infrastructure\Persistence\Doctrine\Fixtures\InvitationFixture;
use App\Infrastructure\Persistence\Doctrine\Fixtures\UserFixture;
use App\Tests\Integration\Infrastructure\Controller\AbstractWebTestCase;
use App\Tests\SessionHelper;

final class DeleteInvitationControllerTest extends AbstractWebTestCase
{
use SessionHelper;

public function testDelete(): void
{
$client = $this->login(UserFixture::MAIN_ORG_ADMIN_EMAIL);
$client->request('DELETE', '/mon-espace/invitations/' . InvitationFixture::INVITATION_ALREADY_JOINED_UUID . '/delete', [
'_token' => $this->generateCsrfToken($client, 'delete-invitation'),
]);

$this->assertResponseStatusCodeSame(303);
$crawler = $client->followRedirect();

$this->assertResponseStatusCodeSame(200);
$this->assertEquals(['success' => ['Invitation supprimée.']], $this->getFlashes($crawler));
$this->assertRouteSame('app_users_list');
}

public function testInvitationNotOwned(): void
{
$client = $this->login('[email protected]');
$client->request('DELETE', '/mon-espace/invitations/' . InvitationFixture::UUID . '/delete', [
'_token' => $this->generateCsrfToken($client, 'delete-invitation'),
]);
$crawler = $client->followRedirect();

$this->assertEquals(['error' => ['Vous n\'avez pas les droits pour supprimer cette invitation.']], $this->getFlashes($crawler));
$this->assertResponseStatusCodeSame(200);
$this->assertRouteSame('app_my_area');
}

public function testOrganizationOrUserNotFound(): void
{
$client = $this->login();
$client->request('DELETE', '/mon-espace/invitations/d68fba17-fb22-490d-ae52-2a371f14ceb1/delete', [
'_token' => $this->generateCsrfToken($client, 'delete-invitation'),
]);
$this->assertResponseStatusCodeSame(404);
}

public function testWithoutAuthenticatedUser(): void
{
$client = static::createClient();
$client->request('DELETE', '/mon-espace/invitations/' . InvitationFixture::UUID . '/delete');
$this->assertResponseRedirects('http://localhost/login', 302);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace App\Tests\Unit\Application\User\Command;
namespace App\Tests\Unit\Application\User\Command\Invitation;

use App\Application\DateUtilsInterface;
use App\Application\IdFactoryInterface;
use App\Application\StringUtilsInterface;
use App\Application\User\Command\CreateInvitationCommand;
use App\Application\User\Command\CreateInvitationCommandHandler;
use App\Application\User\Command\Invitation\CreateInvitationCommand;
use App\Application\User\Command\Invitation\CreateInvitationCommandHandler;
use App\Domain\User\Enum\OrganizationRolesEnum;
use App\Domain\User\Exception\InvitationAlreadyExistsException;
use App\Domain\User\Exception\OrganizationUserAlreadyExistException;
Expand Down
Loading