Generate random prefix for organization projects
This commit is contained in:
parent
f1d219544b
commit
c6833232a0
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DoctrineMigrations;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-generated Migration: Please modify to your needs!
|
||||||
|
*/
|
||||||
|
final class Version20260211142643 extends AbstractMigration
|
||||||
|
{
|
||||||
|
public function getDescription(): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function up(Schema $schema): void
|
||||||
|
{
|
||||||
|
// this up() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -97,6 +97,7 @@ class OrganizationController extends AbstractController
|
||||||
$this->organizationsService->handleLogo($organization, $logoFile);
|
$this->organizationsService->handleLogo($organization, $logoFile);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
$organization->setProjectPrefix($this->organizationsService->generateUniqueProjectPrefix());
|
||||||
$this->entityManager->persist($organization);
|
$this->entityManager->persist($organization);
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
$this->loggerService->logOrganizationInformation($organization->getId(), $actingUser->getUserIdentifier(), "Organization Created");
|
$this->loggerService->logOrganizationInformation($organization->getId(), $actingUser->getUserIdentifier(), "Organization Created");
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,9 @@ class Organizations
|
||||||
#[ORM\OneToMany(targetEntity: UserOrganizatonApp::class, mappedBy: 'organization')]
|
#[ORM\OneToMany(targetEntity: UserOrganizatonApp::class, mappedBy: 'organization')]
|
||||||
private Collection $userOrganizatonApps;
|
private Collection $userOrganizatonApps;
|
||||||
|
|
||||||
|
#[ORM\Column(length: 4, nullable: true)]
|
||||||
|
private ?string $projectPrefix = null;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->apps = new ArrayCollection();
|
$this->apps = new ArrayCollection();
|
||||||
|
|
@ -256,4 +259,16 @@ class Organizations
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getProjectPrefix(): ?string
|
||||||
|
{
|
||||||
|
return $this->projectPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setProjectPrefix(?string $projectPrefix): static
|
||||||
|
{
|
||||||
|
$this->projectPrefix = $projectPrefix;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ readonly class LoggerService
|
||||||
|
|
||||||
|
|
||||||
// User Management Logs
|
// 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", [
|
$this->userManagementLogger->notice("New user created: $userId", [
|
||||||
'target_user_id' => $userId,
|
'target_user_id' => $userId,
|
||||||
|
|
@ -34,7 +34,7 @@ readonly class LoggerService
|
||||||
}
|
}
|
||||||
|
|
||||||
// Organization Management Logs
|
// 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', [
|
$this->organizationManagementLogger->notice('User-Organization link created', [
|
||||||
'target_user_id' => $userId,
|
'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', [
|
$this->organizationManagementLogger->notice('Existing user added to organization', [
|
||||||
'target_user_id' => $userId,
|
'target_user_id' => $userId,
|
||||||
|
|
@ -59,7 +59,7 @@ readonly class LoggerService
|
||||||
}
|
}
|
||||||
|
|
||||||
// Email Notification Logs
|
// 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, [
|
$this->emailNotificationLogger->notice($message, [
|
||||||
'target_user_id' => $userId,
|
'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", [
|
$this->emailNotificationLogger->notice("Existing user notification email sent to $userId", [
|
||||||
'target_user_id' => $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, [
|
$this->adminActionsLogger->notice($message, [
|
||||||
'target_user_id' => $userId,
|
'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, [
|
$this->accessControlLogger->info($message, [
|
||||||
'target_user_id' => $userId,
|
'target_user_id' => $userId,
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue