Skip to content

Commit

Permalink
[Adherent] Add public id (#11285)
Browse files Browse the repository at this point in the history
* [Adherent] Add public id

* Adjust tests
  • Loading branch information
Remg authored Jan 8, 2025
1 parent d68c717 commit 45542b3
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 11 deletions.
5 changes: 5 additions & 0 deletions features/api/profile.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Feature:
"""
{
"uuid": "e6977a4d-2646-5f6c-9c82-88e58dca8458",
"id": "C9E-7BB",
"email_address": "[email protected]",
"first_name": "Carl",
"last_name": "Mirabeau",
Expand Down Expand Up @@ -783,6 +784,7 @@ Feature:
"""
{
"uuid": "313bd28f-efc8-57c9-8ab7-2106c8be9699",
"id": "B6H-873",
"first_name": "Simple",
"last_name": "User",
"certified": false,
Expand Down Expand Up @@ -1323,6 +1325,7 @@ Feature:
"party_membership": "exclusive",
"other_party_membership": false,
"uuid": "b4219d47-3138-5efd-9762-2ef9f9495084",
"id": "G86-5E4",
"first_name": "Gisele",
"last_name": "Berthoux",
"committee_membership": {
Expand Down Expand Up @@ -1434,6 +1437,7 @@ Feature:
"party_membership": "exclusive",
"other_party_membership": false,
"uuid": "b4219d47-3138-5efd-9762-2ef9f9495084",
"id": "G86-5E4",
"first_name": "Gisele",
"last_name": "Berthoux",
"committee_membership": {
Expand Down Expand Up @@ -1519,6 +1523,7 @@ Feature:
"party_membership": "exclusive",
"other_party_membership": false,
"uuid": "b4219d47-3138-5efd-9762-2ef9f9495084",
"id": "G86-5E4",
"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": "e69785",
"id": "C9E-7BB",
"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": "a04686",
"id": "D5C-399",
"nickname": "kikouslove",
"email_address": "[email protected]",
"uuid": "a046adbe-9c7b-56a9-a676-6151a6785dda",
Expand Down
21 changes: 21 additions & 0 deletions migrations/Version20250108110304.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20250108110304 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE adherents ADD public_id VARCHAR(7) DEFAULT NULL');
$this->addSql('CREATE UNIQUE INDEX UNIQ_562C7DA3B5B48B91 ON adherents (public_id)');
}

public function down(Schema $schema): void
{
$this->addSql('DROP INDEX UNIQ_562C7DA3B5B48B91 ON adherents');
$this->addSql('ALTER TABLE adherents DROP public_id');
}
}
2 changes: 2 additions & 0 deletions src/Admin/AbstractAdherentAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ protected function configureDatagridFilters(DatagridMapper $filter): void
[
"$alias.id",
"$alias.uuid",
"$alias.publicId",
]
);

Expand Down Expand Up @@ -983,6 +984,7 @@ protected function configureListFields(ListMapper $list): void
$list
->add('id', null, [
'label' => 'ID',
'template' => 'admin/adherent/list_identifier.html.twig',
])
->add('lastName', null, [
'label' => 'Prénom Nom',
Expand Down
13 changes: 4 additions & 9 deletions src/Entity/Adherent.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
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 @@ -125,6 +126,7 @@ class Adherent implements UserInterface, UserEntityInterface, GeoPointInterface,
use EntityUTMTrait;
use EntityAdministratorBlameableTrait;
use ImageTrait;
use PublicIdTrait;

public const ENABLED = 'ENABLED';
public const TO_DELETE = 'TO_DELETE';
Expand Down Expand Up @@ -567,6 +569,7 @@ public static function createBlank(
$adherent = new self();

$adherent->uuid = Uuid::uuid4();
$adherent->publicId = PublicIdGenerator::generatePublicIdFromUuid($adherent->uuid);
$adherent->gender = $gender;
$adherent->firstName = $firstName;
$adherent->lastName = $lastName;
Expand Down Expand Up @@ -606,6 +609,7 @@ public static function create(
$adherent = new self();

$adherent->uuid = $uuid;
$adherent->publicId = PublicIdGenerator::generatePublicIdFromUuid($uuid);
$adherent->password = $password;
$adherent->gender = $gender;
$adherent->firstName = $firstName;
Expand Down Expand Up @@ -2440,15 +2444,6 @@ public function isBesoinDEuropeUser(): bool
return MembershipSourceEnum::BESOIN_D_EUROPE === $this->source;
}

#[Groups(['jemarche_user_profile'])]
#[SerializedName('id')]
public function getPublicId(): string
{
return 6 > ($idLength = \strlen($this->id))
? substr($this->uuid->toString(), 0, 6 - $idLength).$this->id
: substr($this->id, -6);
}

public function getImagePath(): string
{
return $this->imageName ? \sprintf('images/profile/%s', $this->getImageName()) : '';
Expand Down
25 changes: 25 additions & 0 deletions src/Entity/PublicIdTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\SerializedName;

trait PublicIdTrait
{
#[ORM\Column(length: 7, unique: true, nullable: true)]
protected ?string $publicId = null;

#[Groups(['jemarche_user_profile', 'profile_read'])]
#[SerializedName('id')]
public function getPublicId(): ?string
{
return $this->publicId;
}

public function setPublicId(string $publicId): void
{
$this->publicId = $publicId;
}
}
30 changes: 30 additions & 0 deletions src/Utils/PublicIdGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Utils;

use Ramsey\Uuid\UuidInterface;

class PublicIdGenerator
{
private const string ALLOWED_CHARACTERS = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
private const int SEGMENT_LENGTH = 3;

public static function generatePublicIdFromUuid(UuidInterface $uuid): string
{
$hash = md5($uuid->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)];
}
}
7 changes: 7 additions & 0 deletions templates/admin/adherent/list_identifier.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends '@SonataAdmin/CRUD/base_list_field.html.twig' %}

{% block field %}
{{ object.id }}
<br />
<b>{{ object.publicId }}</b>
{% endblock %}

0 comments on commit 45542b3

Please sign in to comment.