Skip to content

Commit

Permalink
changed principal standardize() calls which were accidentally creatin…
Browse files Browse the repository at this point in the history
…g updates for Principal rows
  • Loading branch information
dkatzel-ncats committed Nov 9, 2021
1 parent 68146ba commit be50cf5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public UserProfile createNewUserProfile(ValidatedNewUserRequest newUserRequest){
principal.username = newUserRequest.getUsername();
principal.email = newUserRequest.getEmail();
principal.admin = Boolean.TRUE.equals(newUserRequest.isAdmin());
principal.standardize();
principal.standardizeAndUpdate();

UserProfile up = new UserProfile();
up.user = principal;
Expand Down
56 changes: 40 additions & 16 deletions gsrs-core-entities/src/main/java/ix/core/models/Principal.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import gsrs.model.AbstractNonAuditingGsrsEntity;
import ix.ginas.models.serialization.GsrsDateDeserializer;
import ix.ginas.models.serialization.GsrsDateSerializer;
import org.hibernate.annotations.DiscriminatorOptions;

import javax.persistence.*;
import javax.validation.constraints.Email;
Expand Down Expand Up @@ -36,24 +35,29 @@ public class Principal extends AbstractNonAuditingGsrsEntity implements Fetchabl

public boolean deprecated;


public Principal standardize() {
this.username=this.username.toUpperCase();
return this;
/**
* Return the standardized username.
* @return a String or {@code null} if username is {@code null}.
*/
public String computeStandardizedName() {
if(this.username ==null){
return null;
}
return this.username.toUpperCase();
}

@PrePersist
private void markCreated(){
Date date =TimeUtil.getCurrentDate();
created = date;
modified= date;
this.standardize();
this.username = computeStandardizedName();
}
@PreUpdate
private void markUpdated(){
Date date =TimeUtil.getCurrentDate();
modified= date;
this.standardize();
this.username = computeStandardizedName();
}

@Override
Expand Down Expand Up @@ -100,18 +104,38 @@ public Principal(String username, String email) {
this.username = username;
this.email = email;
}

@JsonIgnore
public String toString(){
return this.standardize().username;

/**
* Create a new Principal object with a standardized username.
*
* @param username
* @param email
* @return
*/
public static Principal createStandardized(String username, String email){
Principal p = new Principal(username, email);
if(username !=null) {
p.username = p.computeStandardizedName();
}
return p;

}

/**
* Standardize this principal object and update the fields
* and return the updated principal. Update here does not imply
* it is persisted anywhere just that the fields are updated.
* @return a Principal, usually {@code this} but might not always be.
*/
public Principal standardizeAndUpdate(){
this.username = computeStandardizedName();
return this;
}
//TODO katzelda Octobe 2020 : don't think we need this userprofile factory call? its used in a few places in GSRS 2.x but in all cases we could use a repository instead?
/*
@JsonIgnore
public UserProfile getUserProfile(){
return UserProfileFactory.getUserProfileForPrincipal(this);
public String toString(){
return this.computeStandardizedName();
}
*/

public boolean isAdmin () {
return admin;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public UserProfile(Principal user) {

//Needed for JSON
public String getIdentifier() {
return user.standardize().username;
return user.computeStandardizedName();
}

public List<Role> getRoles() {
Expand Down Expand Up @@ -128,7 +128,7 @@ public boolean hasRole(Role role) {
@JsonIgnore
@Indexable(indexed = false)
public String getComputedToken(){
return getComputedToken(this.user.standardize().username, this.key);
return getComputedToken(this.user.computeStandardizedName(), this.key);
}
public static String getComputedToken(String username, String key) {
String date = "" + Util.getCanonicalCacheTimeStamp();
Expand All @@ -142,7 +142,7 @@ public Long getTokenTimeToExpireMS() {

private String getPreviousComputedToken() {
String date = "" + (Util.getCanonicalCacheTimeStamp() - 1);
return Util.sha1(date + this.user.standardize().username + this.key);
return Util.sha1(date + this.user.computeStandardizedName() + this.key);
}

public boolean acceptKey(String key) {
Expand Down Expand Up @@ -195,7 +195,7 @@ public boolean isRoleQueryOnly(){
}

public UserProfile standardize() {
this.user.standardize();
this.user.standardizeAndUpdate();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private UserProfile autoregisterNewUser(String username ) {
return autoregisterNewUser(username, null, null);
}
private UserProfile autoregisterNewUser(String username, String email, List<Role> roles ) {
Principal p = new Principal(username, email).standardize();
Principal p = Principal.createStandardized(username, email);
UserProfile up = new UserProfile(p);
if (authenticationConfiguration.isAutoregisteractive()) {
up.active = true;
Expand Down

0 comments on commit be50cf5

Please sign in to comment.