Skip to content

Commit

Permalink
Name and Source for User are required
Browse files Browse the repository at this point in the history
### What's done:
- small refactoring to mark name and source are not nullable in database and entity

It's part of #2336
  • Loading branch information
nulls committed Jul 20, 2023
1 parent 480574c commit 8078fe7
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AuthenticationUserRepository(
private fun Map<String, Any>.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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fun Mono<User>.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,
Expand Down
5 changes: 5 additions & 0 deletions db/v-2/tables/original-login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@
referencedTableName="user"
onDelete="CASCADE"/>
</changeSet>

<changeSet id="original-login-name-and-source-not-null" author="nulls">
<addNotNullConstraint tableName="original_login" columnName="name" columnDataType="varchar(64)"/>
<addNotNullConstraint tableName="original_login" columnName="source" columnDataType="varchar(64)" defaultNullValue="basic"/>
</changeSet>
</databaseChangeLog>
4 changes: 4 additions & 0 deletions db/v-2/tables/user.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@
</addColumn>
</changeSet>

<changeSet id="user-name-not-nullable" author="nulls">
<addNotNullConstraint tableName="user" columnName="name" columnDataType="varchar(64)"/>
<addNotNullConstraint tableName="user" columnName="source" columnDataType="varchar(64)" defaultNullValue="basic"/>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class LnkUserOrganizationController(
authentication: Authentication?,
): Mono<Role> = authentication?.let {
getUserAndOrganizationWithPermissions(
authentication.toUser().name!!,
authentication.toUser().name,
organizationName,
Permission.READ,
authentication,
Expand Down Expand Up @@ -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),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class PermissionController(
@PathVariable projectName: String,
@RequestParam(required = false) userName: String?,
authentication: Authentication,
): Mono<Role> = getUserAndProjectOrNotFound(userName ?: authentication.toUser().name!!, projectName, organizationName, authentication)
): Mono<Role> = getUserAndProjectOrNotFound(userName ?: authentication.toUser().name, projectName, organizationName, authentication)
.map { (user, project) ->
permissionService.getRole(user, project)
.also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ data class UserInfo(
val name: String,
val id: Long? = null,
val oldName: String? = null,
val originalLogins: List<String?> = emptyList(),
val source: String? = null,
val originalLogins: List<String> = emptyList(),
val source: String,
val projects: Map<String, Role> = emptyMap(),
val organizations: Map<String, Role> = emptyMap(),
val email: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Comment(
) : BaseEntityWithDateAndDto<CommentDto>() {
override fun toDto() = CommentDto(
message = message,
userName = user.name ?: "Unknown",
userName = user.name,
userRating = user.rating,
userAvatar = user.avatar,
createDate = createDate?.toKotlinLocalDateTime(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -57,7 +57,7 @@ class User(
*/
fun toUserInfo(projects: Map<String, Role> = emptyMap(), organizations: Map<String, Role> = emptyMap()) = UserInfo(
id = id,
name = name ?: "Undefined",
name = name,
originalLogins = originalLogins.map { it.name },
source = source,
projects = projects,
Expand Down

0 comments on commit 8078fe7

Please sign in to comment.