diff --git a/src/Adherent/PublicIdGenerator.php b/src/Adherent/PublicIdGenerator.php new file mode 100644 index 00000000000..05c15dc2754 --- /dev/null +++ b/src/Adherent/PublicIdGenerator.php @@ -0,0 +1,52 @@ +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; + } +} diff --git a/src/DataFixtures/ORM/LoadBannedAdherentData.php b/src/DataFixtures/ORM/LoadBannedAdherentData.php index 4d2688b7dd8..0dbf5627380 100644 --- a/src/DataFixtures/ORM/LoadBannedAdherentData.php +++ b/src/DataFixtures/ORM/LoadBannedAdherentData.php @@ -12,7 +12,7 @@ class LoadBannedAdherentData extends Fixture { public function load(ObjectManager $manager): void { - $adherent = Adherent::createBlank('male', 'test', 'test', 'FR', PostAddress::createEmptyAddress(), 'disabled-email@test.com', null, new \DateTime('-18 years')); + $adherent = Adherent::createBlank('ABC-234', 'male', 'test', 'test', 'FR', PostAddress::createEmptyAddress(), 'disabled-email@test.com', null, new \DateTime('-18 years')); $manager->persist(BannedAdherent::createFromAdherent($adherent)); $manager->flush(); diff --git a/src/Doctrine/Hydrators/EventHydrator.php b/src/Doctrine/Hydrators/EventHydrator.php index 102c8f1f6ee..6a859c2339f 100644 --- a/src/Doctrine/Hydrators/EventHydrator.php +++ b/src/Doctrine/Hydrators/EventHydrator.php @@ -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'], diff --git a/src/Entity/Adherent.php b/src/Entity/Adherent.php index e23f03247d6..584d727ae9a 100644 --- a/src/Entity/Adherent.php +++ b/src/Entity/Adherent.php @@ -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; @@ -555,6 +554,7 @@ public function __construct() } public static function createBlank( + string $publicId, string $gender, string $firstName, string $lastName, @@ -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; @@ -588,6 +588,7 @@ public static function createBlank( public static function create( UuidInterface $uuid, + string $publicId, string $emailAddress, ?string $password, ?string $gender, @@ -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; diff --git a/src/Membership/AdherentFactory.php b/src/Membership/AdherentFactory.php index 910ee936a3e..a4ec0c0168a 100644 --- a/src/Membership/AdherentFactory.php +++ b/src/Membership/AdherentFactory.php @@ -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; @@ -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(); } @@ -42,6 +45,7 @@ private function createFromAvecVousMembershipRequest(AvecVousMembershipRequest $ { $adherent = Adherent::create( Adherent::createUuid($request->getEmailAddress()), + $this->generatePublicId(), $request->getEmailAddress(), $this->hashPassword(Uuid::uuid4()), null, @@ -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, @@ -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, @@ -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, @@ -128,6 +135,7 @@ public function createFromAdminAdherentCreateCommand( Administrator $administrator, ): Adherent { $adherent = Adherent::createBlank( + $this->generatePublicId(), $command->gender, $command->firstName, $command->lastName, @@ -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, @@ -191,4 +200,9 @@ private function hashPassword(string $password): string { return $this->hasher->hash($password); } + + private function generatePublicId(): string + { + return $this->publicIdGenerator->generate(); + } } diff --git a/src/Repository/EventRepository.php b/src/Repository/EventRepository.php index c936ed18d0c..13108abf720 100644 --- a/src/Repository/EventRepository.php +++ b/src/Repository/EventRepository.php @@ -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, diff --git a/src/Utils/PublicIdGenerator.php b/src/Utils/PublicIdGenerator.php deleted file mode 100644 index a20d646519a..00000000000 --- a/src/Utils/PublicIdGenerator.php +++ /dev/null @@ -1,30 +0,0 @@ -toString()); - $segment1 = ''; - $segment2 = ''; - - for ($i = 0; $i < self::SEGMENT_LENGTH; ++$i) { - $segment1 .= self::convertHexToAllowedCharacter($hash[$i]); - $segment2 .= self::convertHexToAllowedCharacter($hash[$i + self::SEGMENT_LENGTH]); - } - - return "$segment1-$segment2"; - } - - private static function convertHexToAllowedCharacter(string $hexCharacter): string - { - return self::ALLOWED_CHARACTERS[hexdec($hexCharacter) % \strlen(self::ALLOWED_CHARACTERS)]; - } -} diff --git a/tests/Committee/CommitteeAdherentMandateManagerTest.php b/tests/Committee/CommitteeAdherentMandateManagerTest.php index dacacf9bfff..4d321f63db6 100644 --- a/tests/Committee/CommitteeAdherentMandateManagerTest.php +++ b/tests/Committee/CommitteeAdherentMandateManagerTest.php @@ -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', 'd.dupont@test.com', 'password', $gender, diff --git a/tests/Committee/CommitteeFactoryTest.php b/tests/Committee/CommitteeFactoryTest.php index b8ff3fd4b89..f8ae5f8a22a 100644 --- a/tests/Committee/CommitteeFactoryTest.php +++ b/tests/Committee/CommitteeFactoryTest.php @@ -27,6 +27,7 @@ public function testCreateCommitteeFromCommitteeCreationCommand() $adherent = Adherent::create( $uuid, + 'ABC-234', $email, 'password', 'male', diff --git a/tests/Donation/DonationRequestTest.php b/tests/Donation/DonationRequestTest.php index b56d60ec887..6bf0753b317 100644 --- a/tests/Donation/DonationRequestTest.php +++ b/tests/Donation/DonationRequestTest.php @@ -19,6 +19,7 @@ public function testCreateDonationRequestFromAdherent() $adherent = Adherent::create( $uuid, + 'ABC-234', $email, 'password', 'male', diff --git a/tests/Entity/AdherentTest.php b/tests/Entity/AdherentTest.php index ce0d0ee6f07..f342b0075c3 100644 --- a/tests/Entity/AdherentTest.php +++ b/tests/Entity/AdherentTest.php @@ -136,6 +136,7 @@ private function createNewAdherent( return Adherent::create( Adherent::createUuid($email), + 'ABC-234', $email, 'super-password', 'male', diff --git a/tests/Entity/CommitteeMembershipTest.php b/tests/Entity/CommitteeMembershipTest.php index c21279acd67..f65f63c4f63 100644 --- a/tests/Entity/CommitteeMembershipTest.php +++ b/tests/Entity/CommitteeMembershipTest.php @@ -65,6 +65,7 @@ private function createNewAdherent(): Adherent { return Adherent::create( Uuid::fromString(self::ADHERENT_UUID), + 'ABC-234', 'foo@bar.com', 'password', 'male', diff --git a/tests/Entity/OAuth/UserAuthorizationTest.php b/tests/Entity/OAuth/UserAuthorizationTest.php index 97362f476ee..7be3cd79510 100644 --- a/tests/Entity/OAuth/UserAuthorizationTest.php +++ b/tests/Entity/OAuth/UserAuthorizationTest.php @@ -58,6 +58,7 @@ public function createUser(): Adherent { return Adherent::create( Uuid::uuid4(), + 'ABC-234', 'fake@example.org', '', 'male', diff --git a/tests/Geocoder/Subscriber/EntityAddressGeocodingSubscriberTest.php b/tests/Geocoder/Subscriber/EntityAddressGeocodingSubscriberTest.php index e4577640dba..6e314b95ea5 100644 --- a/tests/Geocoder/Subscriber/EntityAddressGeocodingSubscriberTest.php +++ b/tests/Geocoder/Subscriber/EntityAddressGeocodingSubscriberTest.php @@ -130,6 +130,7 @@ private function createNewAdherent(string $address): Adherent { return Adherent::create( Uuid::fromString('d3522426-1bac-4da4-ade8-5204c9e2caae'), + 'ABC-234', 'john.smith@example.org', 'super-password', 'male', diff --git a/tests/Security/AdherentLoginTimestampRecorderTest.php b/tests/Security/AdherentLoginTimestampRecorderTest.php index ed0422e5d33..24995982d9c 100644 --- a/tests/Security/AdherentLoginTimestampRecorderTest.php +++ b/tests/Security/AdherentLoginTimestampRecorderTest.php @@ -40,6 +40,7 @@ private function createAdherent(): Adherent { return Adherent::create( Adherent::createUuid('john.smith@example.org'), + 'ABC-234', 'john.smith@example.org', 'super-password', 'male', diff --git a/tests/TestHelperTrait.php b/tests/TestHelperTrait.php index 2795444bd05..c71208ef33c 100644 --- a/tests/TestHelperTrait.php +++ b/tests/TestHelperTrait.php @@ -363,6 +363,7 @@ protected function createAdherent(?string $email = null): Adherent return Adherent::create( Adherent::createUuid($email), + 'ABC-234', $email, 'super-password', 'male',