major: Migration from better-auth to keycloak
This commit is contained in:
@@ -3,9 +3,7 @@ package com.betriebsratkanzlei.legalconsenthub.user
|
||||
import com.betriebsratkanzlei.legalconsenthub.error.UserAlreadyExistsException
|
||||
import com.betriebsratkanzlei.legalconsenthub.error.UserNotFoundException
|
||||
import com.betriebsratkanzlei.legalconsenthub.security.CustomJwtTokenPrincipal
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateUserDto
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.UserDto
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.UserStatus
|
||||
import jakarta.transaction.Transactional
|
||||
import org.springframework.security.core.context.SecurityContextHolder
|
||||
import org.springframework.stereotype.Service
|
||||
@@ -13,7 +11,7 @@ import org.springframework.stereotype.Service
|
||||
@Service
|
||||
class UserService(
|
||||
private val userRepository: UserRepository,
|
||||
private val roleConverter: UserRoleConverter
|
||||
private val userMapper: UserMapper
|
||||
) {
|
||||
|
||||
fun getCurrentUser(): User {
|
||||
@@ -24,21 +22,32 @@ class UserService(
|
||||
.orElseThrow { UserNotFoundException(userId) }
|
||||
}
|
||||
|
||||
fun createUser(createUserDto: CreateUserDto): User {
|
||||
if (userRepository.existsById(createUserDto.id)) {
|
||||
throw UserAlreadyExistsException(createUserDto.id)
|
||||
@Transactional
|
||||
fun createUpdateUserFromJwt(userDto: UserDto): User {
|
||||
val existingUser = userRepository.findById(userDto.keycloakId)
|
||||
|
||||
if (existingUser.isEmpty) {
|
||||
return createUser(userDto)
|
||||
} else {
|
||||
val user = existingUser.get()
|
||||
if (user.organizationId == null && userDto.organizationId != null) {
|
||||
user.organizationId = userDto.organizationId
|
||||
}
|
||||
return updateUser(userMapper.toUserDto(user))
|
||||
}
|
||||
}
|
||||
|
||||
fun createUser(userDto: UserDto): User {
|
||||
if (userRepository.existsById(userDto.keycloakId)) {
|
||||
throw UserAlreadyExistsException(userDto.keycloakId)
|
||||
}
|
||||
|
||||
val user = User(
|
||||
id = createUserDto.id,
|
||||
name = createUserDto.name,
|
||||
status = createUserDto.status
|
||||
keycloakId = userDto.keycloakId,
|
||||
name = userDto.name,
|
||||
organizationId = userDto.organizationId
|
||||
)
|
||||
|
||||
createUserDto.organizationRoles?.forEach { (orgId, roles) ->
|
||||
roleConverter.setRolesForOrganization(user.organizationRoles, orgId, roles)
|
||||
}
|
||||
|
||||
return userRepository.save(user)
|
||||
}
|
||||
|
||||
@@ -48,43 +57,19 @@ class UserService(
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun updateUser(userId: String, userDto: UserDto): User {
|
||||
val user = userRepository.findById(userId)
|
||||
.orElseThrow { UserNotFoundException(userId) }
|
||||
fun updateUser(userDto: UserDto): User {
|
||||
val user = userRepository.findById(userDto.keycloakId)
|
||||
.orElseThrow { UserNotFoundException(userDto.keycloakId) }
|
||||
|
||||
user.name = userDto.name
|
||||
user.status = userDto.status
|
||||
|
||||
user.organizationRoles.clear()
|
||||
userDto.organizationRoles.forEach { (orgId, roles) ->
|
||||
roleConverter.setRolesForOrganization(user.organizationRoles, orgId, roles)
|
||||
// Only update organization if it's not already set
|
||||
if (user.organizationId == null && userDto.organizationId != null) {
|
||||
user.organizationId = userDto.organizationId
|
||||
}
|
||||
|
||||
return userRepository.save(user)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun updateUserFromJwt(userId: String, jwtOrganizationId: String?, jwtRoles: List<String>?): User {
|
||||
val existingUser = userRepository.findById(userId)
|
||||
.orElseThrow { UserNotFoundException(userId) }
|
||||
|
||||
if (jwtOrganizationId != null && !jwtRoles.isNullOrEmpty()) {
|
||||
existingUser.organizationRoles.removeIf { it.organizationId == jwtOrganizationId }
|
||||
|
||||
jwtRoles.forEach { role ->
|
||||
val normalizedRole = role.lowercase().replace("_", "_")
|
||||
existingUser.organizationRoles.add(
|
||||
UserOrganizationRole(
|
||||
organizationId = jwtOrganizationId,
|
||||
role = normalizedRole
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return userRepository.save(existingUser)
|
||||
}
|
||||
|
||||
fun deleteUser(userId: String) {
|
||||
userRepository.deleteById(userId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user