From 8078fe7ffee3908ba1aba5fb80ad32f8fdf8202a Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 20 Jul 2023 11:17:49 +0300 Subject: [PATCH] Name and Source for User are required ### What's done: - small refactoring to mark name and source are not nullable in database and entity It's part of #2336 --- .../authservice/repository/AuthenticationUserRepository.kt | 2 +- .../save/authservice/utils/IdentitySourceAwareUserDetails.kt | 2 +- db/v-2/tables/original-login.xml | 5 +++++ db/v-2/tables/user.xml | 4 ++++ .../backend/controllers/LnkUserOrganizationController.kt | 4 ++-- .../save/backend/controllers/PermissionController.kt | 2 +- .../save/backend/controllers/internal/UsersController.kt | 2 +- .../saveourtool/save/backend/service/UserDetailsService.kt | 2 +- .../commonMain/kotlin/com/saveourtool/save/info/UserInfo.kt | 4 ++-- .../jvmMain/kotlin/com/saveourtool/save/entities/Comment.kt | 2 +- .../kotlin/com/saveourtool/save/entities/OriginalLogin.kt | 4 ++-- .../com/saveourtool/save/entities/TestsSourceVersion.kt | 4 +--- .../src/jvmMain/kotlin/com/saveourtool/save/entities/User.kt | 4 ++-- 13 files changed, 24 insertions(+), 17 deletions(-) diff --git a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt index 631d4b93d0..c460574f29 100644 --- a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt +++ b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt @@ -35,7 +35,7 @@ class AuthenticationUserRepository( private fun Map.toUserEntity(): User { val record = this return User( - name = record["name"] as String?, + name = record["name"] as String, password = record["password"] as String?, role = record["role"] as String?, source = record["source"] as String, diff --git a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/IdentitySourceAwareUserDetails.kt b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/IdentitySourceAwareUserDetails.kt index 06a553943a..92ea699b45 100644 --- a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/IdentitySourceAwareUserDetails.kt +++ b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/IdentitySourceAwareUserDetails.kt @@ -58,7 +58,7 @@ fun Mono.getIdentitySourceAwareUserDetails(username: String, source: Strin */ @Suppress("UnsafeCallOnNullableType") private fun User.toIdentitySourceAwareUserDetails(): IdentitySourceAwareUserDetails = IdentitySourceAwareUserDetails( - username = this.name!!, + username = this.name, password = this.password.orEmpty(), authorities = this.role, identitySource = this.source, diff --git a/db/v-2/tables/original-login.xml b/db/v-2/tables/original-login.xml index 1de0764d9a..c9f740e15a 100644 --- a/db/v-2/tables/original-login.xml +++ b/db/v-2/tables/original-login.xml @@ -33,4 +33,9 @@ referencedTableName="user" onDelete="CASCADE"/> + + + + + diff --git a/db/v-2/tables/user.xml b/db/v-2/tables/user.xml index 8a046ba939..e4bd47f55e 100644 --- a/db/v-2/tables/user.xml +++ b/db/v-2/tables/user.xml @@ -67,4 +67,8 @@ + + + + diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/LnkUserOrganizationController.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/LnkUserOrganizationController.kt index 6ed7296559..bb73048107 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/LnkUserOrganizationController.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/LnkUserOrganizationController.kt @@ -106,7 +106,7 @@ class LnkUserOrganizationController( authentication: Authentication?, ): Mono = authentication?.let { getUserAndOrganizationWithPermissions( - authentication.toUser().name!!, + authentication.toUser().name, organizationName, Permission.READ, authentication, @@ -274,7 +274,7 @@ class LnkUserOrganizationController( .map { OrganizationWithUsers( organization = it.organization.toDto(), - userRoles = mapOf(it.user.name!! to it.role), + userRoles = mapOf(it.user.name to it.role), ) } diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/PermissionController.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/PermissionController.kt index 39a4aa25e8..5096ff3cc5 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/PermissionController.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/PermissionController.kt @@ -78,7 +78,7 @@ class PermissionController( @PathVariable projectName: String, @RequestParam(required = false) userName: String?, authentication: Authentication, - ): Mono = getUserAndProjectOrNotFound(userName ?: authentication.toUser().name!!, projectName, organizationName, authentication) + ): Mono = getUserAndProjectOrNotFound(userName ?: authentication.toUser().name, projectName, organizationName, authentication) .map { (user, project) -> permissionService.getRole(user, project) .also { diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt index ab4059fed2..78abdb4297 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt @@ -44,7 +44,7 @@ class UsersController( @PostMapping("/new") @Transactional fun saveNewUser(@RequestBody user: User) { - val userName = requireNotNull(user.name) { "Provided user $user doesn't have a name" } + val userName = user.name val userFind = originalLoginRepository.findByNameAndSource(userName, user.source) diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/service/UserDetailsService.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/service/UserDetailsService.kt index 3019b3c0b9..8dcf10002c 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/service/UserDetailsService.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/service/UserDetailsService.kt @@ -80,7 +80,7 @@ class UserDetailsService( return if (oldName == null) { userRepository.save(user) UserSaveStatus.UPDATE - } else if (userName != null && userRepository.validateName(userName) != 0L) { + } else if (userRepository.validateName(userName) != 0L) { userRepository.deleteHighLevelName(oldName) userRepository.saveHighLevelName(userName) userRepository.save(user) diff --git a/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserInfo.kt b/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserInfo.kt index a1a1ce2ff3..85482701e5 100644 --- a/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserInfo.kt +++ b/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserInfo.kt @@ -34,8 +34,8 @@ data class UserInfo( val name: String, val id: Long? = null, val oldName: String? = null, - val originalLogins: List = emptyList(), - val source: String? = null, + val originalLogins: List = emptyList(), + val source: String, val projects: Map = emptyMap(), val organizations: Map = emptyMap(), val email: String? = null, diff --git a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/Comment.kt b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/Comment.kt index 12130d58aa..eb298d1fb1 100644 --- a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/Comment.kt +++ b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/Comment.kt @@ -29,7 +29,7 @@ class Comment( ) : BaseEntityWithDateAndDto() { override fun toDto() = CommentDto( message = message, - userName = user.name ?: "Unknown", + userName = user.name, userRating = user.rating, userAvatar = user.avatar, createDate = createDate?.toKotlinLocalDateTime(), diff --git a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/OriginalLogin.kt b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/OriginalLogin.kt index a5d5558b65..44433d4687 100644 --- a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/OriginalLogin.kt +++ b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/OriginalLogin.kt @@ -15,10 +15,10 @@ import javax.persistence.ManyToOne */ @Entity class OriginalLogin( - var name: String?, + var name: String, @ManyToOne @JoinColumn(name = "user_id") @JsonBackReference var user: User, - var source: String?, + var source: String, ) : BaseEntity() diff --git a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/TestsSourceVersion.kt b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/TestsSourceVersion.kt index f8ce0a307e..181736f139 100644 --- a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/TestsSourceVersion.kt +++ b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/TestsSourceVersion.kt @@ -53,9 +53,7 @@ class TestsSourceVersion( version = name, type = type, creationTime = creationTime.toKotlinLocalDateTime(), - createdByUserName = requireNotNull(createdByUser.name) { - "username is not set for ${createdByUser.requiredId()}" - } + createdByUserName = createdByUser.name, ) companion object { diff --git a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/User.kt b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/User.kt index 00c3bd27da..2ad3888523 100644 --- a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/User.kt +++ b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/User.kt @@ -29,7 +29,7 @@ import javax.persistence.OneToMany @Entity @Suppress("LongParameterList") class User( - var name: String?, + var name: String, var password: String?, var role: String?, var source: String, @@ -57,7 +57,7 @@ class User( */ fun toUserInfo(projects: Map = emptyMap(), organizations: Map = emptyMap()) = UserInfo( id = id, - name = name ?: "Undefined", + name = name, originalLogins = originalLogins.map { it.name }, source = source, projects = projects,