Skip to content

Commit

Permalink
Merge pull request #458 from BBMRI-ERIC/fix/updates-userinfo-from-per…
Browse files Browse the repository at this point in the history
…unaccess-token

fix: updates person if user info of perun token changed
  • Loading branch information
RadovanTomik authored Sep 25, 2024
2 parents e36c059 + b231120 commit a6e76a2
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ private NegotiatorJwtAuthenticationToken parseJWTAsUserToken(Jwt jwt) {
Person person =
personRepository
.findBySubjectId(subjectIdentifier)
.map(existingPerson -> updatePersonIfNecessary(existingPerson, userInfo))
.orElseGet(() -> saveNewUserAsPerson(userInfo));

return new NegotiatorJwtAuthenticationToken(person, jwt, parseUserAuthorities(userInfo));
}

Expand Down Expand Up @@ -193,6 +195,26 @@ private Person saveNewUserAsPerson(Map<String, Object> userInfo) {
return person;
}

private Person updatePersonIfNecessary(Person person, Map<String, Object> userInfo) {
boolean isUpdated = false;

if (!person.getName().equals(userInfo.get("name").toString())) {
person.setName(userInfo.get("name").toString());
isUpdated = true;
}
if (!person.getEmail().equals(userInfo.get("email").toString())) {
person.setEmail(userInfo.get("email").toString());
isUpdated = true;
}

if (isUpdated) {
personRepository.save(person);
log.info(String.format("User with sub: %s updated in the database", person.getSubjectId()));
}

return person;
}

static void cleanCache() {
log.debug("Clearing userInfo cache.");
userInfoCache.clear();
Expand Down

0 comments on commit a6e76a2

Please sign in to comment.