Implemented isApplication() and getFullName() APIs

This commit is contained in:
Luca Frosini 2022-03-24 14:26:18 +01:00
parent 6e271a5772
commit 66fd1afc7c
2 changed files with 128 additions and 13 deletions

View File

@ -22,18 +22,6 @@ public class GCubeUser implements User {
@JsonIgnore
protected Map<String, Object> additionalProperties;
public GCubeUser() {
this.additionalProperties = new HashMap<>();
// This info are not always present. Setting an empty string to avoid null
this.givenName = "";
this.familyName = "";
this.eMail = "";
this.jobTitle = "";
this.picture = "";
this.middleName = "";
}
@JsonProperty("id")
protected String username;
@JsonProperty("roles")
@ -53,6 +41,23 @@ public class GCubeUser implements User {
@JsonProperty("middle_name")
protected String middleName;
@JsonIgnore
protected boolean application;
public GCubeUser() {
this.additionalProperties = new HashMap<>();
// This info are not always present. Setting an empty string to avoid null
this.givenName = "";
this.familyName = "";
this.eMail = "";
this.jobTitle = "";
this.picture = "";
this.middleName = "";
this.application = false;
}
@Override
public String getUsername() {
return username;
@ -62,6 +67,15 @@ public class GCubeUser implements User {
this.username = username;
}
@Override
public boolean isApplication() {
return application;
}
public void setApplication(boolean application) {
this.application = application;
}
@Override
public Collection<String> getRoles() {
return roles;
@ -117,4 +131,49 @@ public class GCubeUser implements User {
this.additionalProperties.put(key, value);
}
@Override
public String getFullName() {
return getFullName(false);
}
@Override
public String getFullName(boolean nameSurname) {
if(isApplication()) {
return getUsername();
}
StringBuffer stringBuffer = new StringBuffer();
boolean found = false;
String surname = getFamilyName();
String name = getGivenName();
if(nameSurname) {
if(name!=null && name.trim().length()>0) {
stringBuffer.append(name.trim());
found = true;
}
if(surname!=null && surname.trim().length()>0) {
if(found) {
stringBuffer.append(" ");
}
stringBuffer.append(surname.trim());
found = true;
}
}else {
if(surname!=null && surname.trim().length()>0) {
stringBuffer.append(surname.trim());
found = true;
}
if(name!=null && name.trim().length()>0) {
if(found) {
stringBuffer.append(" ");
}
stringBuffer.append(name.trim());
found = true;
}
}
return stringBuffer.toString();
}
}

View File

@ -15,8 +15,10 @@ public class KeycloakUser extends AccessToken implements User {
*/
private static final long serialVersionUID = -7083648026885406300L;
protected Collection<String> roles;
public static final String CLIENT_ID_PROPERTY = "clientId";
protected Collection<String> roles;
protected Boolean application;
@Override
@JsonIgnore
@ -24,6 +26,14 @@ public class KeycloakUser extends AccessToken implements User {
return getPreferredUsername();
}
@Override
public boolean isApplication() {
if(application==null) {
application = getOtherClaims().get(CLIENT_ID_PROPERTY)!=null;
}
return application;
}
@Override
@JsonIgnore
public Collection<String> getRoles() {
@ -41,4 +51,50 @@ public class KeycloakUser extends AccessToken implements User {
return "";
}
@Override
public String getFullName() {
return getFullName(false);
}
@Override
public String getFullName(boolean nameSurname) {
if(isApplication()) {
String clientID = (String) getOtherClaims().getOrDefault("clientId", getUsername());
return clientID;
}
StringBuffer stringBuffer = new StringBuffer();
boolean found = false;
String surname = getFamilyName();
String name = getGivenName();
if(nameSurname) {
if(name!=null && name.trim().length()>0) {
stringBuffer.append(name.trim());
found = true;
}
if(surname!=null && surname.trim().length()>0) {
if(found) {
stringBuffer.append(" ");
}
stringBuffer.append(surname.trim());
found = true;
}
}else {
if(surname!=null && surname.trim().length()>0) {
stringBuffer.append(surname.trim());
found = true;
}
if(name!=null && name.trim().length()>0) {
if(found) {
stringBuffer.append(" ");
}
stringBuffer.append(name.trim());
found = true;
}
}
return stringBuffer.toString();
}
}