From 7b9e94a278da62a4edc8540ba5eda5fe4f526883 Mon Sep 17 00:00:00 2001 From: Rodrigo Prestes Machado Date: Sat, 29 Nov 2025 22:02:04 -0300 Subject: [PATCH] Updates the automatic generation of user IDs and adjusts the SQL import table. --- .../orion/users/adapters/controllers/UserController.kt | 10 +++++++--- .../users/adapters/gateways/entities/UserEntity.kt | 3 ++- .../adapters/gateways/repository/UserRepositoryImpl.kt | 2 ++ src/main/resources/import.sql | 8 ++++++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/dev/orion/users/adapters/controllers/UserController.kt b/src/main/kotlin/dev/orion/users/adapters/controllers/UserController.kt index f140f93..2c898c3 100644 --- a/src/main/kotlin/dev/orion/users/adapters/controllers/UserController.kt +++ b/src/main/kotlin/dev/orion/users/adapters/controllers/UserController.kt @@ -94,6 +94,8 @@ class UserController : BasicController() { fun createUser(name: String, email: String, password: String): Uni { val user: User = createUC.createUser(name, email, password) val entity: UserEntity = mapper.map(user, UserEntity::class.java) + // Allow the ID to be null for auto-generation by the database + entity.id = null return userRepository.createUser(entity) .onItem().ifNotNull().transform { u -> u } .onItem().ifNotNull().call { user -> this.sendValidationEmail(user) } @@ -133,7 +135,9 @@ class UserController : BasicController() { // generates a JWT return userRepository.authenticate(entity) .onItem().ifNotNull() - .transform { this.generateJWT(it) } + .transform { + this.generateJWT(it) + } } /** @@ -168,7 +172,6 @@ class UserController : BasicController() { response.authentication = dto response.requires2FA = false } - response } } @@ -236,6 +239,8 @@ class UserController : BasicController() { // User doesn't exist, create it // Generate a secure password (user won't use it, but DB requires it) entity.password = DigestUtils.sha256Hex(UUID.randomUUID().toString()) + // Garantir que o ID seja null para permitir geração automática pelo banco + entity.id = null userRepository.createUser(entity) .onItem().ifNotNull().transform { newUser -> val response = LoginResponseDTO() @@ -813,4 +818,3 @@ class UserController : BasicController() { } } - diff --git a/src/main/kotlin/dev/orion/users/adapters/gateways/entities/UserEntity.kt b/src/main/kotlin/dev/orion/users/adapters/gateways/entities/UserEntity.kt index 2253f97..155fa34 100644 --- a/src/main/kotlin/dev/orion/users/adapters/gateways/entities/UserEntity.kt +++ b/src/main/kotlin/dev/orion/users/adapters/gateways/entities/UserEntity.kt @@ -22,6 +22,7 @@ import jakarta.persistence.Column import jakarta.persistence.Entity import jakarta.persistence.FetchType import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType import jakarta.persistence.Id import jakarta.persistence.JoinColumn import jakarta.persistence.JoinTable @@ -45,7 +46,7 @@ class UserEntity : PanacheEntityBase() { /** Primary key. */ @Id - @GeneratedValue + @GeneratedValue(strategy = GenerationType.IDENTITY) @JsonIgnore var id: Long? = null diff --git a/src/main/kotlin/dev/orion/users/adapters/gateways/repository/UserRepositoryImpl.kt b/src/main/kotlin/dev/orion/users/adapters/gateways/repository/UserRepositoryImpl.kt index e57955b..8cd3ae0 100644 --- a/src/main/kotlin/dev/orion/users/adapters/gateways/repository/UserRepositoryImpl.kt +++ b/src/main/kotlin/dev/orion/users/adapters/gateways/repository/UserRepositoryImpl.kt @@ -251,6 +251,8 @@ class UserRepositoryImpl @Inject constructor( .failWith(IOException("Role not found")) .onItem().ifNotNull() .transformToUni { role -> + // Allow the ID to be null for auto-generation by the database + user.id = null user.addRole(role) Panache.withTransaction { user.persist() } .onItem().transform { user } diff --git a/src/main/resources/import.sql b/src/main/resources/import.sql index d2afc65..6ea847f 100644 --- a/src/main/resources/import.sql +++ b/src/main/resources/import.sql @@ -6,7 +6,11 @@ INSERT INTO Role (id, name) VALUES (2, 'user'); INSERT INTO User (id, hash, name, email, password, emailValid, emailValidationCode, isUsing2FA, secret2FA, require2FAForBasicLogin, require2FAForSocialLogin) VALUES (1, '00000000-0000-0000-0000-000000000001', 'Administrator', 'admin@orion.dev', '24febcc27e4a5762911a4481a941a3563cc4bf5e5f61f0ea3799333871d2a89b', true, '00000000-0000-0000-0000-000000000001', false, NULL, false, false); --- Associar roles ao usuário admin (admin e user) --- A tabela de junção está definida explicitamente na entidade UserEntity como "User_Role" +-- Associate roles to admin user (admin and user) +-- The junction table is explicitly defined in the UserEntity as "User_Role" INSERT INTO User_Role (User_id, roles_id) VALUES (1, 1); -- admin INSERT INTO User_Role (User_id, roles_id) VALUES (1, 2); -- user + +-- Update the auto-increment of the User table to avoid primary key conflict +-- This ensures that the next automatically generated ID will be 2 +ALTER TABLE User AUTO_INCREMENT = 2;