Skip to content

Commit

Permalink
Merge pull request #28 from akaene/main
Browse files Browse the repository at this point in the history
Prevent accidental update of user types when referencing current user
  • Loading branch information
blcham authored Dec 6, 2023
2 parents f141fbd + 66bc367 commit bff6079
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
42 changes: 36 additions & 6 deletions src/main/java/cz/cvut/kbss/study/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,21 @@ public void addType(String type) {
getTypes().add(type);
}

public String getToken() { return token; }
public String getToken() {
return token;
}

public void setToken(String token) { this.token = token; }
public void setToken(String token) {
this.token = token;
}

public Boolean getIsInvited() { return isInvited; }
public Boolean getIsInvited() {
return isInvited;
}

public void setIsInvited(Boolean isInvited) { this.isInvited = isInvited; }
public void setIsInvited(Boolean isInvited) {
this.isInvited = isInvited;
}

/**
* Encodes password of this person.
Expand All @@ -178,6 +186,27 @@ public void erasePassword() {
this.password = null;
}

/**
* Creates a copy of this instance.
*
* @return New user instance
*/
public User copy() {
final User copy = new User();
copy.setUri(uri);
copy.setFirstName(firstName);
copy.setLastName(lastName);
copy.setUsername(username);
copy.setEmailAddress(emailAddress);
copy.setPassword(password);
copy.setDateCreated(dateCreated);
copy.setInstitution(institution);
copy.setIsInvited(isInvited);
copy.setToken(token);
types.forEach(copy::addType);
return copy;
}

@Override
public void generateUri() {
if (uri != null) {
Expand All @@ -191,8 +220,9 @@ public void generateUri() {
}
try {
this.uri = URI.create(Constants.BASE_URI +
URLEncoder.encode(firstName + "-" + lastName + "-" + IdentificationUtils.generateRandomURINumber(),
StandardCharsets.UTF_8.toString()));
URLEncoder.encode(
firstName + "-" + lastName + "-" + IdentificationUtils.generateRandomURINumber(),
StandardCharsets.UTF_8.toString()));
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Cannot generate Person URI due to unsupported encoding.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public User getCurrentUser() {
return resolveAccountFromOAuthPrincipal((Jwt) principal);
} else {
final String username = context.getAuthentication().getName();
final User user = userDao.findByUsername(username);
final User user = userDao.findByUsername(username).copy();
if (context.getAuthentication().getAuthorities().stream().anyMatch(a -> a.getAuthority().equals(
SwitchUserWebFilter.ROLE_PREVIOUS_ADMINISTRATOR))) {
user.addType(Vocabulary.s_c_impersonator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.hamcrest.Matchers.hasItem;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -198,4 +199,16 @@ void getCurrentUserEnhancesRetrievedUserWithImpersonatorTypeWhenItHasSwitchAutho
assertEquals(user, result);
assertThat(result.getTypes(), hasItem(Vocabulary.s_c_impersonator));
}

@Test
void getCurrentUserReturnsCopyOfInstanceRetrievedFromRepository() {
final UserDetails userDetails =
new UserDetails(user, Set.of(new SimpleGrantedAuthority(SwitchUserFilter.ROLE_PREVIOUS_ADMINISTRATOR)));
SecurityUtils.setCurrentUser(userDetails);
when(userDao.findByUsername(user.getUsername())).thenReturn(user);
final User result = sut.getCurrentUser();

assertNotSame(user, result);
assertEquals(user, result);
}
}

0 comments on commit bff6079

Please sign in to comment.