From c6833232a020fdc1523cf624ab75bc7e1c52a3f9 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 11 Feb 2026 15:45:39 +0100 Subject: [PATCH] Generate random prefix for organization projects --- migrations/Version20260211142643.php | 32 +++++++++++++++++++++++ src/Controller/OrganizationController.php | 1 + src/Entity/Organizations.php | 15 +++++++++++ src/Service/LoggerService.php | 14 +++++----- src/Service/OrganizationsService.php | 15 +++++++++++ 5 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 migrations/Version20260211142643.php diff --git a/migrations/Version20260211142643.php b/migrations/Version20260211142643.php new file mode 100644 index 0000000..584ce6c --- /dev/null +++ b/migrations/Version20260211142643.php @@ -0,0 +1,32 @@ +addSql('ALTER TABLE organizations ADD project_prefix VARCHAR(4) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE organizations DROP project_prefix'); + } +} diff --git a/src/Controller/OrganizationController.php b/src/Controller/OrganizationController.php index f3ce97e..89813ed 100644 --- a/src/Controller/OrganizationController.php +++ b/src/Controller/OrganizationController.php @@ -97,6 +97,7 @@ class OrganizationController extends AbstractController $this->organizationsService->handleLogo($organization, $logoFile); } try { + $organization->setProjectPrefix($this->organizationsService->generateUniqueProjectPrefix()); $this->entityManager->persist($organization); $this->entityManager->flush(); $this->loggerService->logOrganizationInformation($organization->getId(), $actingUser->getUserIdentifier(), "Organization Created"); diff --git a/src/Entity/Organizations.php b/src/Entity/Organizations.php index 281697c..6cbce6a 100644 --- a/src/Entity/Organizations.php +++ b/src/Entity/Organizations.php @@ -61,6 +61,9 @@ class Organizations #[ORM\OneToMany(targetEntity: UserOrganizatonApp::class, mappedBy: 'organization')] private Collection $userOrganizatonApps; + #[ORM\Column(length: 4, nullable: true)] + private ?string $projectPrefix = null; + public function __construct() { $this->apps = new ArrayCollection(); @@ -256,4 +259,16 @@ class Organizations return $this; } + + public function getProjectPrefix(): ?string + { + return $this->projectPrefix; + } + + public function setProjectPrefix(?string $projectPrefix): static + { + $this->projectPrefix = $projectPrefix; + + return $this; + } } diff --git a/src/Service/LoggerService.php b/src/Service/LoggerService.php index f7167b4..75f4e86 100644 --- a/src/Service/LoggerService.php +++ b/src/Service/LoggerService.php @@ -23,7 +23,7 @@ readonly class LoggerService // User Management Logs - public function logUserCreated(int $userId, int|string $actingUserId): void + public function logUserCreated(int|string $userId, int|string $actingUserId): void { $this->userManagementLogger->notice("New user created: $userId", [ 'target_user_id' => $userId, @@ -34,7 +34,7 @@ readonly class LoggerService } // Organization Management Logs - public function logUserOrganizationLinkCreated(int $userId, int $orgId, int|string $actingUserId, ?int $uoId): void + public function logUserOrganizationLinkCreated(int|string $userId, int $orgId, int|string $actingUserId, ?int $uoId): void { $this->organizationManagementLogger->notice('User-Organization link created', [ 'target_user_id' => $userId, @@ -46,7 +46,7 @@ readonly class LoggerService ]); } - public function logExistingUserAddedToOrg(int $userId, int $orgId, int|string $actingUserId, int $uoId): void + public function logExistingUserAddedToOrg(int|string $userId, int $orgId, int|string $actingUserId, int $uoId): void { $this->organizationManagementLogger->notice('Existing user added to organization', [ 'target_user_id' => $userId, @@ -59,7 +59,7 @@ readonly class LoggerService } // Email Notification Logs - public function logEmailSent(int $userId, ?int $orgId, string $message): void + public function logEmailSent(int|string $userId, ?int $orgId, string $message): void { $this->emailNotificationLogger->notice($message, [ 'target_user_id' => $userId, @@ -69,7 +69,7 @@ readonly class LoggerService ]); } - public function logExistingUserNotificationSent(int $userId, int $orgId): void + public function logExistingUserNotificationSent(int|string $userId, int $orgId): void { $this->emailNotificationLogger->notice("Existing user notification email sent to $userId", [ 'target_user_id' => $userId, @@ -87,7 +87,7 @@ readonly class LoggerService ])); } - public function logSuperAdmin(int $userId, int|string $actingUserId, string $message, ?int $orgId = null): void + public function logSuperAdmin(int|string $userId, int|string $actingUserId, string $message, ?int $orgId = null): void { $this->adminActionsLogger->notice($message, [ 'target_user_id' => $userId, @@ -202,7 +202,7 @@ readonly class LoggerService ]); } - public function logRoleEntityAssignment(int $userId, int $organizationId, int $roleId, int|string $actingUserId, string $message): void + public function logRoleEntityAssignment(int|string $userId, int $organizationId, int $roleId, int|string $actingUserId, string $message): void { $this->accessControlLogger->info($message, [ 'target_user_id' => $userId, diff --git a/src/Service/OrganizationsService.php b/src/Service/OrganizationsService.php index ec7c095..cfb6263 100644 --- a/src/Service/OrganizationsService.php +++ b/src/Service/OrganizationsService.php @@ -229,4 +229,19 @@ class OrganizationsService } } + /* Function that check if the project prefix was provided and if it is unique, if not it will generate a random one and check again until it is unique */ + public function generateUniqueProjectPrefix(): string{ + $prefix = $this->generateRandomPrefix(); + while ($this->entityManager->getRepository(Organizations::class)->findOneBy(['projectPrefix' => $prefix])) { + $prefix = $this->generateRandomPrefix(); + } + return $prefix; + } + + private function generateRandomPrefix(): string + { + return substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, 4); + } + + }