Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- refactored code
  • Loading branch information
temi committed May 26, 2023
1 parent 981bd36 commit f276cc8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
25 changes: 11 additions & 14 deletions grails-app/services/au/org/ala/ecodata/forms/UserInfoService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class UserInfoService {
private static ThreadLocal<UserDetails> _currentUser = new ThreadLocal<UserDetails>()

String getCurrentUserDisplayName() {
_currentUser.get()?.displayName
getCurrentUser()?.displayName
}

UserDetails getCurrentUser() {
return _currentUser.get()
_currentUser.get()
}

/**
Expand All @@ -59,15 +59,13 @@ class UserInfoService {
* @param userId
*/
UserDetails setCurrentUser() {
UserDetails userDetails = getCurrentUser()
if (!userDetails) {
userDetails = getCurrentUserSupportedMethods()

if (userDetails) {
_currentUser.set(userDetails)
} else {
log.warn("Failed to get user details! No details set on thread local.")
}
clearCurrentUser()
UserDetails userDetails = getCurrentUserFromSupportedMethods()

if (userDetails) {
_currentUser.set(userDetails)
} else {
log.warn("Failed to get user details! No details set on thread local.")
}

userDetails
Expand Down Expand Up @@ -133,10 +131,9 @@ class UserInfoService {
/**
* Get details of the current user either from CAS or lookup to user details server.
* Authentication details are provide in header userName and authKey
* @return Map with following key
* ['displayName': "", 'userName': "", 'userId': ""]
* @return UserDetails
*/
UserDetails getCurrentUserSupportedMethods() {
UserDetails getCurrentUserFromSupportedMethods() {
def user

// First, check if CAS can get logged in user details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class UserInfoServiceSpec extends Specification implements ServiceUnitTest<UserI
Optional<UserProfile> userProfile = new Optional<UserProfile>(person)

when:
result = service.getCurrentUserSupportedMethods()
result = service.getCurrentUserFromSupportedMethods()

then:
1 * authService.userDetails() >> userDetails
Expand All @@ -121,7 +121,7 @@ class UserInfoServiceSpec extends Specification implements ServiceUnitTest<UserI

when:
request.addHeader('Authorization', 'Bearer abcdef')
result = service.getCurrentUserSupportedMethods()
result = service.getCurrentUserFromSupportedMethods()

then:
alaOidcClient.getCredentials(*_) >> credentials
Expand All @@ -134,7 +134,7 @@ class UserInfoServiceSpec extends Specification implements ServiceUnitTest<UserI

when: "Authorization header is not set and authKeyEnabled is false"
request.removeHeader('Authorization')
result = service.getCurrentUserSupportedMethods()
result = service.getCurrentUserFromSupportedMethods()

then:
1 * authService.userDetails() >> null
Expand All @@ -145,7 +145,7 @@ class UserInfoServiceSpec extends Specification implements ServiceUnitTest<UserI
request.removeHeader('Authorization')
request.addHeader(UserInfoService.AUTH_KEY_HEADER_FIELD, key)
request.addHeader(UserInfoService.USER_NAME_HEADER_FIELD, userName)
result = service.getCurrentUserSupportedMethods()
result = service.getCurrentUserFromSupportedMethods()

then:
1 * authService.userDetails() >> null
Expand Down

0 comments on commit f276cc8

Please sign in to comment.