Skip to content

Commit

Permalink
#174 Add cognito affiliation report support
Browse files Browse the repository at this point in the history
  • Loading branch information
sbearcsiro committed Nov 3, 2023
1 parent 90296a0 commit 374fefa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions userdetails-cognito/grails-app/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ environments:
cas:
appServerName: "https://auth.ala.org.au"

attributes:
affiliations:
enabled: true
attribute-name: 'custom:affiliation'

cognito:
mapping:
email: 'email'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class CognitoUserService implements IUserService<UserRecord, UserPropertyRecord,

EmailService emailService
TokenService tokenService
LocationService locationService

AWSCognitoIdentityProvider cognitoIdp
String poolId
Expand Down Expand Up @@ -461,7 +462,24 @@ class CognitoUserService implements IUserService<UserRecord, UserPropertyRecord,

@Override
List<String[]> countByProfileAttribute(String s, Date date, Locale locale) {
return null
def token
def counts = [:]
def results = cognitoIdp.listUsers(new ListUsersRequest().withUserPoolId(poolId))

while (results) {
def users = results.getUsers()
token = results.getPaginationToken()

users.each {
def value = it.attributes.find { it.name == "custom.$s" }?.value
counts[value ?: ''] = ((counts[value ?: '']) ?: 0)++
}

results = token ? cognitoIdp.listUsers(new ListUsersRequest().withUserPoolId(poolId).withPaginationToken(token)) : null
}
def affiliations = locationService.affiliationSurvey(locale)

return counts.collect { [affiliations[it.key] ?: it.key, it.value.toString()].toArray(new String[0]) }
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.opencsv.CSVWriterBuilder
import com.opencsv.RFC4180ParserBuilder
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.multipart.MultipartHttpServletRequest

Expand All @@ -33,6 +34,9 @@ class AdminController {
def profileService
def authorisedSystemService

@Value('${attributes.affiliations.attribute-name:affiliation}')
String affiliationAttribute = 'affiliation'

@Autowired
@Qualifier('userService')
IUserService userService
Expand Down Expand Up @@ -153,7 +157,7 @@ class AdminController {
}

def surveyResults() {
def results = userService.countByProfileAttribute('affiliation', null, request.locale)
def results = userService.countByProfileAttribute(affiliationAttribute, null, request.locale)
respondWithCsv(results, "user-survey-${new Date()}.csv")
}

Expand Down

0 comments on commit 374fefa

Please sign in to comment.