Skip to content

Commit

Permalink
Create user account
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarchois committed Jan 21, 2025
1 parent 23cd3b8 commit cc7c9c7
Show file tree
Hide file tree
Showing 45 changed files with 601 additions and 1,332 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ APP_DIALOG_BASE_URL=http://nginx
REDIS_URL="redis://redis:6379"
API_ADRESSE_BASE_URL=https://api-adresse.data.gouv.fr
APP_IGN_GEOCODER_BASE_URL=https://data.geopf.fr
API_ORGANIZATION_FETCHER_URL=https://recherche-entreprises.api.gouv.fr/search
MATOMO_ENABLED=false
###> BD TOPO ###
BDTOPO_DATABASE_URL=
Expand Down
2 changes: 2 additions & 0 deletions config/packages/framework.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ framework:
base_uri: '%env(APP_IGN_GEOCODER_BASE_URL)%'
dialog.http.client:
base_uri: '%env(APP_DIALOG_BASE_URL)%'
organization_fetcher.client:
base_uri: '%env(API_ORGANIZATION_FETCHER_URL)%'

when@test:
framework:
Expand Down
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,9 @@ when@test:
arguments: ['@http_kernel']
App\Infrastructure\Adapter\DateUtils:
class: App\Tests\Mock\DateUtilsMock
App\Tests\Mock\ApiOrganizationFetcherMock:
decorates: 'organization_fetcher.client'
decoration_inner_name: 'App\Tests\Mock\EudonetParis\ApiOrganizationFetcherMock::organization_fetcher.client'

Psr\Log\NullLogger: ~
logger: '@Psr\Log\NullLogger'
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\SaveAccessRequestCommand">
<class name="App\Application\User\Command\RegisterCommand">
<property name="fullName">
<constraint name="NotBlank"/>
<constraint name="Length">
Expand All @@ -22,13 +22,8 @@
<option name="min">12</option>
</constraint>
</property>
<property name="organizationName">
<constraint name="NotBlank"/>
<constraint name="Length">
<option name="max">255</option>
</constraint>
</property>
<property name="organizationSiret">
<constraint name="NotBlank"/>
<constraint name="Type">
<option name="type">digit</option>
<option name="message">Les caractères doivent être des chiffres.</option>
Expand All @@ -38,10 +33,5 @@
<option name="min">14</option>
</constraint>
</property>
<property name="consentToBeContacted">
<constraint name="Type">
<option name="type">boolean</option>
</constraint>
</property>
</class>
</constraint-mapping>
33 changes: 0 additions & 33 deletions config/validator/Domain/User/AccessRequest.xml

This file was deleted.

10 changes: 10 additions & 0 deletions src/Application/ApiOrganizationFetcherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace App\Application;

interface ApiOrganizationFetcherInterface
{
public function findBySiret(string $siret): array;
}
14 changes: 0 additions & 14 deletions src/Application/User/Command/ConvertAccessRequestToUserCommand.php

This file was deleted.

15 changes: 15 additions & 0 deletions src/Application/User/Command/RegisterCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Application\User\Command;

use App\Application\CommandInterface;

final class RegisterCommand implements CommandInterface
{
public ?string $fullName;
public ?string $email;
public ?string $password;
public ?string $organizationSiret;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,81 @@

namespace App\Application\User\Command;

use App\Application\ApiOrganizationFetcherInterface;
use App\Application\DateUtilsInterface;
use App\Application\IdFactoryInterface;
use App\Domain\User\AccessRequest;
use App\Application\PasswordHasherInterface;
use App\Application\StringUtilsInterface;
use App\Domain\User\Enum\OrganizationRolesEnum;
use App\Domain\User\Enum\UserRolesEnum;
use App\Domain\User\Exception\AccessRequestNotFoundException;
use App\Domain\User\Exception\SiretMissingException;
use App\Domain\User\Exception\OrganizationNotFoundException;
use App\Domain\User\Exception\UserAlreadyRegisteredException;
use App\Domain\User\Organization;
use App\Domain\User\OrganizationUser;
use App\Domain\User\PasswordUser;
use App\Domain\User\Repository\AccessRequestRepositoryInterface;
use App\Domain\User\Repository\OrganizationRepositoryInterface;
use App\Domain\User\Repository\OrganizationUserRepositoryInterface;
use App\Domain\User\Repository\PasswordUserRepositoryInterface;
use App\Domain\User\Repository\UserRepositoryInterface;
use App\Domain\User\User;

final class ConvertAccessRequestToUserCommandHandler
final class RegisterCommandHandler
{
public function __construct(
private IdFactoryInterface $idFactory,
private AccessRequestRepositoryInterface $accessRequestRepository,
private UserRepositoryInterface $userRepository,
private PasswordUserRepositoryInterface $passwordUserRepository,
private OrganizationUserRepositoryInterface $organizationUserRepository,
private OrganizationRepositoryInterface $organizationRepository,
private DateUtilsInterface $dateUtils,
private StringUtilsInterface $stringUtils,
private PasswordHasherInterface $passwordHasher,
private ApiOrganizationFetcherInterface $organizationFetcher,
) {
}

public function __invoke(ConvertAccessRequestToUserCommand $command): void
public function __invoke(RegisterCommand $command): User
{
$accessRequest = $this->accessRequestRepository->findOneByUuid($command->uuid);
if (!$accessRequest instanceof AccessRequest) {
throw new AccessRequestNotFoundException();
}

if (!$accessRequest->getSiret()) {
throw new SiretMissingException();
}
$email = $this->stringUtils->normalizeEmail($command->email);

$user = $this->userRepository->findOneByEmail($accessRequest->getEmail());
$user = $this->userRepository->findOneByEmail($email);
if ($user instanceof User) {
throw new UserAlreadyRegisteredException();
}

$organization = $this->organizationRepository->findOneBySiret($accessRequest->getSiret());
$organization = $this->organizationRepository->findOneBySiret($command->organizationSiret);
$organizationRole = OrganizationRolesEnum::ROLE_ORGA_CONTRIBUTOR->value; // Default organization role
$now = $this->dateUtils->getNow();

if (!$organization) {
try {
['name' => $name] = $this->organizationFetcher->findBySiret($command->organizationSiret);
} catch (OrganizationNotFoundException $e) {
throw $e;
}

$organizationRole = OrganizationRolesEnum::ROLE_ORGA_ADMIN->value; // The first user in an organization becomes an admin
$organization = (new Organization($this->idFactory->make()))
->setCreatedAt($now)
->setSiret($accessRequest->getSiret())
->setName($accessRequest->getOrganization());
->setSiret($command->organizationSiret)
->setName($name);
$this->organizationRepository->add($organization);
}

$user = (new User($this->idFactory->make()))
->setFullName($accessRequest->getFullName())
->setEmail($accessRequest->getEmail())
->setFullName($command->fullName)
->setEmail($email)
->setRoles([UserRolesEnum::ROLE_USER->value])
->setRegistrationDate($now);

$passwordUser = new PasswordUser(
uuid: $this->idFactory->make(),
password: $accessRequest->getPassword(),
password: $this->passwordHasher->hash($command->password),
user: $user,
);

$user->setPasswordUser($passwordUser);

$organizationUser = (new OrganizationUser($this->idFactory->make()))
->setUser($user)
->setOrganization($organization)
Expand All @@ -84,6 +87,7 @@ public function __invoke(ConvertAccessRequestToUserCommand $command): void
$this->userRepository->add($user);
$this->passwordUserRepository->add($passwordUser);
$this->organizationUserRepository->add($organizationUser);
$this->accessRequestRepository->remove($accessRequest);

return $user;
}
}
18 changes: 0 additions & 18 deletions src/Application/User/Command/SaveAccessRequestCommand.php

This file was deleted.

47 changes: 0 additions & 47 deletions src/Application/User/Command/SaveAccessRequestCommandHandler.php

This file was deleted.

80 changes: 0 additions & 80 deletions src/Domain/User/AccessRequest.php

This file was deleted.

Loading

0 comments on commit cc7c9c7

Please sign in to comment.