Skip to content

Commit

Permalink
split AuthenticationOriginalUserRepository and AuthenticationUserRepo…
Browse files Browse the repository at this point in the history
…sitory
  • Loading branch information
nulls committed Jul 20, 2023
1 parent 18ec7ac commit 6b6d148
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.saveourtool.save.authservice.repository

import com.saveourtool.save.authservice.utils.toUserEntity
import com.saveourtool.save.entities.User
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
import org.springframework.stereotype.Component

/**
* Repository for [com.saveourtool.save.entities.User]
*/
@Component
class AuthenticationOriginalUserRepository(
private val namedParameterJdbcTemplate: NamedParameterJdbcTemplate,
) {
/**
* @param name name of user
* @param source source of user
* @return user or null if no results have been found
*/
fun findByNameAndSource(name: String, source: String): User? =
namedParameterJdbcTemplate.queryForList(

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 12 but was 8 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 12 but was 8
"SELECT * FROM save_cloud.user WHERE id = (select user_id from save_cloud.original_login where name = :name AND source = :source)",

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12
mapOf("name" to name, "source" to source)

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12
).singleOrNull()?.toUserEntity()

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 12 but was 8 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 12 but was 8
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,28 @@ import org.springframework.stereotype.Component
*/
@Component
class AuthenticationUserRepository(
private val namedParameterJdbcTemplate: NamedParameterJdbcTemplate,
protected val namedParameterJdbcTemplate: NamedParameterJdbcTemplate,

Check failure

Code scanning / ktlint

[KDOC_NO_CONSTRUCTOR_PROPERTY] all properties from the primary constructor should be documented in a @Property tag in KDoc: add to KDoc Error

[KDOC_NO_CONSTRUCTOR_PROPERTY] all properties from the primary constructor should be documented in a @property tag in KDoc: add to KDoc
) {
/**
* @param name name of user
* @param source source of user
* @return user or null if no results have been found
*/
fun findByNameAndSource(name: String, source: String): User? {
val record = namedParameterJdbcTemplate.queryForList(
"SELECT * FROM save_cloud.user WHERE name = :name AND source = :source",
mapOf("name" to name, "source" to source)
).singleOrNull()
?: namedParameterJdbcTemplate.queryForList(
"SELECT * FROM save_cloud.user WHERE id = (select user_id from save_cloud.original_login where name = :name AND source = :source)",
mapOf("name" to name, "source" to source)
).singleOrNull()
.orNotFound {
"There is no user with name $name and source $source"
}
return record.toUserEntity()
fun findByNameAndSource(name: String): User? {
return namedParameterJdbcTemplate.queryForList(

Check failure

Code scanning / ktlint

[WRONG_NEWLINES] incorrect line breaking: functions with single return statement should be simplified to expression body Error

[WRONG_NEWLINES] incorrect line breaking: functions with single return statement should be simplified to expression body
"SELECT * FROM save_cloud.user WHERE name = :name",
mapOf("name" to name)
).singleOrNull()?.toUserEntity()
}

private fun Map<String, Any>.toUserEntity(): User {
val record = this
return User(
name = record["name"] as String,
password = record["password"] as String?,
role = record["role"] as String?,
email = record["email"] as String?,
avatar = record["avatar"] as String?,
company = record["company"] as String?,
location = record["location"] as String?,
linkedin = record["linkedin"] as String?,
gitHub = record["git_hub"] as String?,
twitter = record["twitter"] as String?,
isActive = record["is_active"] as Boolean,
rating = record["rating"] as Long,
).apply {
this.id = record["id"] as Long
/**
* @param name name of user
* @return user or error if no results have been found
*/
fun getByNameAndSource(name: String): User = findByNameAndSource(name)
.orNotFound {
"There is no user with name $name"
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fun Mono<User>.getIdAwareUserDetails(username: String, source: String? = null) =
*/
@Suppress("UnsafeCallOnNullableType")
private fun User.toIdAwareUserDetails(): IdAwareUserDetails = IdAwareUserDetails(
username = this.name.orResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR),
username = this.name,
password = this.password.orEmpty(),
authorities = this.role,
id = this.requiredId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.saveourtool.save.authservice.utils

import com.saveourtool.save.domain.Role
import com.saveourtool.save.entities.User
import org.springframework.security.access.hierarchicalroles.RoleHierarchy
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl
import org.springframework.security.access.hierarchicalroles.RoleHierarchyUtils
Expand Down Expand Up @@ -48,3 +49,26 @@ fun roleHierarchy(): RoleHierarchy = mapOf(
.let {
RoleHierarchyImpl().apply { setHierarchy(it) }
}

/**

Check failure

Code scanning / ktlint

[TOP_LEVEL_ORDER] the declaration part of a top level elements should be in the proper order: /**... Error

[TOP_LEVEL_ORDER] the declaration part of a top level elements should be in the proper order: /**...
* @return Entity [User] created from provided [Map]
*/
internal fun Map<String, Any>.toUserEntity(): User {
val record = this
return User(
name = record["name"] as String,
password = record["password"] as String?,
role = record["role"] as String?,
email = record["email"] as String?,
avatar = record["avatar"] as String?,
company = record["company"] as String?,
location = record["location"] as String?,
linkedin = record["linkedin"] as String?,
gitHub = record["git_hub"] as String?,
twitter = record["twitter"] as String?,
isActive = record["is_active"] as Boolean,
rating = record["rating"] as Long,
).apply {
this.id = record["id"] as Long
}
}

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): no newline at the end of file SecurityUtils.kt Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): no newline at the end of file SecurityUtils.kt

Check warning

Code scanning / detekt

Checks whether files end with a line separator. Warning

The file /home/runner/work/save-cloud/save-cloud/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/SecurityUtils.kt is not ending with a new line.

0 comments on commit 6b6d148

Please sign in to comment.