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

[Adherent] Generate unique public ids #11295

Merged
merged 2 commits into from
Jan 10, 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
4 changes: 2 additions & 2 deletions features/api/adherents.feature
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ Feature:
"last_name": "Fullstack",
"image_url": null,
"birthdate": "1942-01-10T00:00:00+02:00",
"age": 82,
"age": 83,
"phone": null,
"nationality": null,
"tags": [
Expand Down Expand Up @@ -756,7 +756,7 @@ Feature:
"last_name": "Fullstack",
"image_url": null,
"birthdate": "1942-01-10T00:00:00+02:00",
"age": 82,
"age": 83,
"phone": null,
"nationality": null,
"tags": [
Expand Down
10 changes: 5 additions & 5 deletions features/api/profile.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Feature:
"""
{
"uuid": "e6977a4d-2646-5f6c-9c82-88e58dca8458",
"id": "C9E-7BB",
"id": "@string@",
"email_address": "[email protected]",
"first_name": "Carl",
"last_name": "Mirabeau",
Expand Down Expand Up @@ -784,7 +784,7 @@ Feature:
"""
{
"uuid": "313bd28f-efc8-57c9-8ab7-2106c8be9699",
"id": "B6H-873",
"id": "@string@",
"first_name": "Simple",
"last_name": "User",
"certified": false,
Expand Down Expand Up @@ -1325,7 +1325,7 @@ Feature:
"party_membership": "exclusive",
"other_party_membership": false,
"uuid": "b4219d47-3138-5efd-9762-2ef9f9495084",
"id": "G86-5E4",
"id": "@string@",
"first_name": "Gisele",
"last_name": "Berthoux",
"committee_membership": {
Expand Down Expand Up @@ -1437,7 +1437,7 @@ Feature:
"party_membership": "exclusive",
"other_party_membership": false,
"uuid": "b4219d47-3138-5efd-9762-2ef9f9495084",
"id": "G86-5E4",
"id": "@string@",
"first_name": "Gisele",
"last_name": "Berthoux",
"committee_membership": {
Expand Down Expand Up @@ -1523,7 +1523,7 @@ Feature:
"party_membership": "exclusive",
"other_party_membership": false,
"uuid": "b4219d47-3138-5efd-9762-2ef9f9495084",
"id": "G86-5E4",
"id": "@string@",
"first_name": "Gisele",
"last_name": "Berthoux",
"committee_membership": {
Expand Down
4 changes: 2 additions & 2 deletions features/api/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Feature:
And the JSON should be equal to:
"""
{
"id": "C9E-7BB",
"id": "@string@",
"uuid": "e6977a4d-2646-5f6c-9c82-88e58dca8458",
"email_address": "[email protected]",
"email_subscribed": true,
Expand Down Expand Up @@ -67,7 +67,7 @@ Feature:
And the JSON should be equal to:
"""
{
"id": "D5C-399",
"id": "@string@",
"nickname": "kikouslove",
"email_address": "[email protected]",
"uuid": "a046adbe-9c7b-56a9-a676-6151a6785dda",
Expand Down
52 changes: 52 additions & 0 deletions src/Adherent/PublicIdGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Adherent;

use App\Repository\AdherentRepository;

class PublicIdGenerator
{
public function __construct(public readonly AdherentRepository $adherentRepository)
{
}

public function generate(): string
{
$publicId = self::build();

return !$this->checkIfAlreadyExists($publicId)
? $publicId
: $this->generate();
}

public static function build(): string
{
$characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';

$charactersArray = str_split($characters);

shuffle($charactersArray);

$block1 = self::generateRandomBlock($charactersArray, 3);
$block2 = self::generateRandomBlock($charactersArray, 3);

return $block1.'-'.$block2;
}

private static function generateRandomBlock(array $characters, int $length): string
{
$block = '';
$maxIndex = \count($characters) - 1;

for ($i = 0; $i < $length; ++$i) {
$block .= $characters[random_int(0, $maxIndex)];
}

return $block;
}

private function checkIfAlreadyExists(string $publicId): bool
{
return $this->adherentRepository->count(['publicId' => $publicId]) > 0;
}
}
2 changes: 1 addition & 1 deletion src/DataFixtures/ORM/LoadBannedAdherentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class LoadBannedAdherentData extends Fixture
{
public function load(ObjectManager $manager): void
{
$adherent = Adherent::createBlank('male', 'test', 'test', 'FR', PostAddress::createEmptyAddress(), '[email protected]', null, new \DateTime('-18 years'));
$adherent = Adherent::createBlank('ABC-234', 'male', 'test', 'test', 'FR', PostAddress::createEmptyAddress(), '[email protected]', null, new \DateTime('-18 years'));

$manager->persist(BannedAdherent::createFromAdherent($adherent));
$manager->flush();
Expand Down
1 change: 1 addition & 0 deletions src/Doctrine/Hydrators/EventHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected function hydrateRowData(array $row, array &$result)
if ($uuidOrganizer = $row['adherent_uuid'] ? Uuid::fromString($row['adherent_uuid']) : null) {
$organizer = Adherent::create(
$uuidOrganizer,
$row['adherent_public_id'],
$row['adherent_email_address'],
$uuidOrganizer,
$row['adherent_gender'],
Expand Down
7 changes: 4 additions & 3 deletions src/Entity/Adherent.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
use App\Scope\ScopeEnum;
use App\Subscription\SubscriptionTypeEnum;
use App\Utils\AreaUtils;
use App\Utils\PublicIdGenerator;
use App\Validator\UniqueMembership;
use App\Validator\ZoneBasedRoles as AssertZoneBasedRoles;
use App\ValueObject\Genders;
Expand Down Expand Up @@ -555,6 +554,7 @@ public function __construct()
}

public static function createBlank(
string $publicId,
string $gender,
string $firstName,
string $lastName,
Expand All @@ -569,7 +569,7 @@ public static function createBlank(
$adherent = new self();

$adherent->uuid = Uuid::uuid4();
$adherent->publicId = PublicIdGenerator::generatePublicIdFromUuid($adherent->uuid);
$adherent->publicId = $publicId;
$adherent->gender = $gender;
$adherent->firstName = $firstName;
$adherent->lastName = $lastName;
Expand All @@ -588,6 +588,7 @@ public static function createBlank(

public static function create(
UuidInterface $uuid,
string $publicId,
string $emailAddress,
?string $password,
?string $gender,
Expand All @@ -609,7 +610,7 @@ public static function create(
$adherent = new self();

$adherent->uuid = $uuid;
$adherent->publicId = PublicIdGenerator::generatePublicIdFromUuid($uuid);
$adherent->publicId = $publicId;
$adherent->password = $password;
$adherent->gender = $gender;
$adherent->firstName = $firstName;
Expand Down
16 changes: 15 additions & 1 deletion src/Membership/AdherentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Membership;

use App\Address\PostAddressFactory;
use App\Adherent\PublicIdGenerator;
use App\Adherent\Tag\TagEnum;
use App\Adhesion\AdhesionStepEnum;
use App\Adhesion\Request\MembershipRequest;
Expand All @@ -21,11 +22,13 @@
class AdherentFactory
{
private PasswordHasherInterface $hasher;
private PublicIdGenerator $publicIdGenerator;
private PostAddressFactory $addressFactory;

public function __construct(PasswordHasherFactoryInterface $hasherFactory, ?PostAddressFactory $addressFactory = null)
public function __construct(PasswordHasherFactoryInterface $hasherFactory, PublicIdGenerator $publicIdGenerator, ?PostAddressFactory $addressFactory = null)
{
$this->hasher = $hasherFactory->getPasswordHasher(Adherent::class);
$this->publicIdGenerator = $publicIdGenerator;
$this->addressFactory = $addressFactory ?: new PostAddressFactory();
}

Expand All @@ -42,6 +45,7 @@ private function createFromAvecVousMembershipRequest(AvecVousMembershipRequest $
{
$adherent = Adherent::create(
Adherent::createUuid($request->getEmailAddress()),
$this->generatePublicId(),
$request->getEmailAddress(),
$this->hashPassword(Uuid::uuid4()),
null,
Expand All @@ -62,6 +66,7 @@ private function createFromJeMengageMembershipRequest(JeMengageMembershipRequest
{
$adherent = Adherent::create(
Adherent::createUuid($request->getEmailAddress()),
$this->generatePublicId(),
$request->getEmailAddress(),
$this->hashPassword(Uuid::uuid4()),
$request->gender,
Expand All @@ -83,6 +88,7 @@ public function createFromRenaissanceMembershipRequest(MembershipRequest $member
{
$adherent = Adherent::create(
uuid: Uuid::uuid4(),
publicId: $this->generatePublicId(),
emailAddress: $membershipRequest->email,
password: null,
gender: $membershipRequest->civility,
Expand All @@ -102,6 +108,7 @@ public function createFromBesoinDEuropeMembershipRequest(InscriptionRequest $ins
{
$adherent = Adherent::create(
uuid: Uuid::uuid4(),
publicId: $this->generatePublicId(),
emailAddress: $inscriptionRequest->email,
password: null,
gender: $inscriptionRequest->civility,
Expand All @@ -128,6 +135,7 @@ public function createFromAdminAdherentCreateCommand(
Administrator $administrator,
): Adherent {
$adherent = Adherent::createBlank(
$this->generatePublicId(),
$command->gender,
$command->firstName,
$command->lastName,
Expand Down Expand Up @@ -155,6 +163,7 @@ public function createFromArray(array $data): Adherent

return Adherent::create(
isset($data['uuid']) ? Uuid::fromString($data['uuid']) : Adherent::createUuid($data['email']),
$data['public_id'] ?? $this->generatePublicId(),
$data['email'],
$this->hashPassword($data['password']),
$data['gender'] ?? null,
Expand Down Expand Up @@ -191,4 +200,9 @@ private function hashPassword(string $password): string
{
return $this->hasher->hash($password);
}

private function generatePublicId(): string
{
return $this->publicIdGenerator->generate();
}
}
4 changes: 3 additions & 1 deletion src/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ public function searchAllEvents(SearchParametersFilter $search): array
committees.address_address AS committee_address_address, committees.address_country AS committee_address_country,
committees.address_city_name AS committee_address_city_name, committees.address_city_insee AS committee_address_city_insee,
committees.address_postal_code AS committee_address_postal_code, committees.address_latitude AS committee_address_latitude,
committees.address_longitude AS committee_address_longitude, adherents.uuid AS adherent_uuid,
committees.address_longitude AS committee_address_longitude,
adherents.uuid AS adherent_uuid,
adherents.public_id AS adherent_public_id,
adherents.email_address AS adherent_email_address,
adherents.gender AS adherent_gender, adherents.first_name AS adherent_first_name,
adherents.last_name AS adherent_last_name, adherents.birthdate AS adherent_birthdate,
Expand Down
30 changes: 0 additions & 30 deletions src/Utils/PublicIdGenerator.php

This file was deleted.

1 change: 1 addition & 0 deletions tests/Committee/CommitteeAdherentMandateManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ private function createNewAdherent(string $gender = Genders::MALE, ?string $birt
{
return Adherent::create(
Uuid::fromString('c0d66d5f-e124-4641-8fd1-1dd72ffda563'),
'ABC-234',
'[email protected]',
'password',
$gender,
Expand Down
1 change: 1 addition & 0 deletions tests/Committee/CommitteeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testCreateCommitteeFromCommitteeCreationCommand()

$adherent = Adherent::create(
$uuid,
'ABC-234',
$email,
'password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/Donation/DonationRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function testCreateDonationRequestFromAdherent()

$adherent = Adherent::create(
$uuid,
'ABC-234',
$email,
'password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/Entity/AdherentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private function createNewAdherent(

return Adherent::create(
Adherent::createUuid($email),
'ABC-234',
$email,
'super-password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/Entity/CommitteeMembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private function createNewAdherent(): Adherent
{
return Adherent::create(
Uuid::fromString(self::ADHERENT_UUID),
'ABC-234',
'[email protected]',
'password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/Entity/OAuth/UserAuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function createUser(): Adherent
{
return Adherent::create(
Uuid::uuid4(),
'ABC-234',
'[email protected]',
'',
'male',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private function createNewAdherent(string $address): Adherent
{
return Adherent::create(
Uuid::fromString('d3522426-1bac-4da4-ade8-5204c9e2caae'),
'ABC-234',
'[email protected]',
'super-password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/Security/AdherentLoginTimestampRecorderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private function createAdherent(): Adherent
{
return Adherent::create(
Adherent::createUuid('[email protected]'),
'ABC-234',
'[email protected]',
'super-password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/TestHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ protected function createAdherent(?string $email = null): Adherent

return Adherent::create(
Adherent::createUuid($email),
'ABC-234',
$email,
'super-password',
'male',
Expand Down
Loading