Skip to content

Commit

Permalink
Replaced Authentication.toUser() by Authentication.username()
Browse files Browse the repository at this point in the history
### What's done:
- a small refactoring

It's part of #2336
  • Loading branch information
nulls committed Jul 20, 2023
1 parent 7b67a57 commit 19f0143
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class StoringServerAuthenticationSuccessHandler(
/**
* @return [User] with data from this [Authentication]
*/
fun Authentication.toUser(): User = User(
private fun Authentication.toUser(): User = User(
userName(),
null,
authorities.joinToString(",") { it.authority },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,6 @@ fun Authentication.extractUserNameAndIdentitySource(): Pair<String, String> = th
identitySource
}

/**
* Convert [Authentication] to [User] based on convention in backend.
* We assume here that all authentications are created by [ConvertingAuthenticationManager],
* so `principal` is a String, containing identity source.
*
* @return [User]
*/
fun Authentication.toUser(): User {
val (identitySource, name) = (principal as String).split(':')
return User(
name = name,
password = null,
email = null,
role = (this as UsernamePasswordAuthenticationToken).authorities.joinToString(separator = ","),
source = identitySource,
status = UserStatus.CREATED,
)
}

/**
* Set role hierarchy for spring security
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

package com.saveourtool.save.backend.controllers

import com.saveourtool.save.authservice.utils.toUser
import com.saveourtool.save.authservice.utils.userId
import com.saveourtool.save.authservice.utils.username
import com.saveourtool.save.backend.security.OrganizationPermissionEvaluator
import com.saveourtool.save.backend.service.LnkUserOrganizationService
import com.saveourtool.save.backend.service.OrganizationService
Expand Down Expand Up @@ -106,7 +106,7 @@ class LnkUserOrganizationController(
authentication: Authentication?,
): Mono<Role> = authentication?.let {
getUserAndOrganizationWithPermissions(
authentication.toUser().name,
authentication.username(),
organizationName,
Permission.READ,
authentication,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.saveourtool.save.backend.controllers

import com.saveourtool.save.authservice.utils.toUser
import com.saveourtool.save.authservice.utils.username
import com.saveourtool.save.backend.security.OrganizationPermissionEvaluator
import com.saveourtool.save.backend.security.ProjectPermissionEvaluator
import com.saveourtool.save.backend.service.OrganizationService
Expand Down 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.username(), projectName, organizationName, authentication)
.map { (user, project) ->
permissionService.getRole(user, project)
.also {
Expand Down

0 comments on commit 19f0143

Please sign in to comment.