authorization-utils/src/main/java/org/gcube/common/authorization/utils/user/GCubeUser.java

180 lines
3.7 KiB
Java
Raw Normal View History

2021-12-07 13:50:39 +01:00
package org.gcube.common.authorization.utils.user;
import java.util.Collection;
2021-12-07 16:09:01 +01:00
import java.util.HashMap;
2022-03-24 17:58:33 +01:00
import java.util.HashSet;
2021-12-07 16:09:01 +01:00
import java.util.Map;
2021-12-07 13:50:39 +01:00
import java.util.Set;
2021-12-07 16:09:01 +01:00
import org.gcube.com.fasterxml.jackson.annotation.JsonAnyGetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonAnySetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
2021-12-07 13:50:39 +01:00
import org.gcube.com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class GCubeUser implements User {
2021-12-07 16:09:01 +01:00
/**
* Used to allow to have any additional properties
*/
@JsonIgnore
protected Map<String, Object> additionalProperties;
2021-12-07 14:03:06 +01:00
@JsonProperty("id")
2021-12-07 13:50:39 +01:00
protected String username;
2021-12-07 14:03:06 +01:00
@JsonProperty("roles")
2021-12-07 13:50:39 +01:00
protected Set<String> roles;
2021-12-07 14:03:06 +01:00
@JsonProperty("given_name")
protected String givenName;
@JsonProperty("family_name")
protected String familyName;
@JsonProperty("email")
2021-12-07 13:50:39 +01:00
protected String eMail;
2021-12-07 14:12:56 +01:00
@JsonProperty("job_title")
protected String jobTitle;
2021-12-07 13:50:39 +01:00
2021-12-07 14:03:06 +01:00
@JsonProperty("picture")
protected String picture;
@JsonProperty("middle_name")
protected String middleName;
2021-12-07 13:50:39 +01:00
2022-03-24 17:58:33 +01:00
@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;
}
2021-12-07 13:50:39 +01:00
@Override
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
2022-03-24 17:58:33 +01:00
@Override
public boolean isApplication() {
return application;
}
2022-03-24 17:58:33 +01:00
public void setApplication(boolean application) {
this.application = application;
}
2021-12-07 13:50:39 +01:00
@Override
public Collection<String> getRoles() {
return roles;
}
2022-03-24 17:58:33 +01:00
@Override
public void setRoles(Collection<String> roles) {
this.roles = new HashSet<>(roles);
}
2021-12-07 13:50:39 +01:00
@Override
2021-12-07 14:03:06 +01:00
public String getGivenName() {
return givenName;
2021-12-07 13:50:39 +01:00
}
@Override
2021-12-07 14:03:06 +01:00
public String getFamilyName() {
return familyName;
2021-12-07 13:50:39 +01:00
}
@Override
public String getEmail() {
return eMail;
}
2021-12-07 14:03:06 +01:00
2021-12-07 14:12:56 +01:00
@Override
public String getAbout() {
return jobTitle;
}
2021-12-07 14:03:06 +01:00
public String getPicture() {
return picture;
2021-12-07 13:50:39 +01:00
}
2021-12-07 14:03:06 +01:00
public String getMiddleName() {
return middleName;
2021-12-07 13:50:39 +01:00
}
2021-12-07 16:09:01 +01:00
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return additionalProperties;
}
public void setAdditionalProperties(Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
}
public Object getAdditionalProperty(String key) {
return additionalProperties.get(key);
}
@JsonAnySetter
public void setAdditionalProperty(String key, Object value) {
this.additionalProperties.put(key, value);
}
2021-12-07 14:12:56 +01:00
2022-03-24 17:58:33 +01:00
@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();
}
2021-12-07 13:50:39 +01:00
}