Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent accidental update of user types when referencing current user #28

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}