Skip to content

Instantly share code, notes, and snippets.

@vdelacou
Created June 14, 2018 09:32
Show Gist options
  • Save vdelacou/faed59e551d5b1c5959aaf1da73715eb to your computer and use it in GitHub Desktop.
Save vdelacou/faed59e551d5b1c5959aaf1da73715eb to your computer and use it in GitHub Desktop.
UserDTO adapt for Auth0 User in JHipster
package com.seelix.api.service.dto;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Set;
import java.util.stream.Collectors;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import com.auth0.json.mgmt.users.User;
import com.seelix.api.config.Constants;
import com.seelix.api.service.Auth0ManagementApiService;
/**
* A DTO representing a user, with his authorities.
*/
public class UserDTO {
private String id;
@NotBlank
@Pattern(regexp = Constants.LOGIN_REGEX)
@Size(min = 1, max = 50)
private String login;
@Size(max = 50)
private String firstName;
@Size(max = 50)
private String lastName;
@Email
@Size(min = 5, max = 100)
private String email;
@Size(max = 256)
private String imageUrl;
private boolean activated = false;
@Size(min = 2, max = 6)
private String langKey;
private Instant createdDate;
private Instant lastModifiedDate;
private Set<String> authorities;
public UserDTO() {
// Empty constructor needed for Jackson.
}
@SuppressWarnings("unchecked")
public UserDTO(User user) {
this.id = user.getId();
this.login = user.getUsername();
if (user.getUserMetadata() != null
&& user.getUserMetadata().get(Auth0ManagementApiService.GIVEN_NAME) != null) {
this.firstName = (@Size(max = 50) String) user.getUserMetadata().get(Auth0ManagementApiService.GIVEN_NAME);
} else {
this.firstName = user.getGivenName();
}
if (user.getUserMetadata() != null
&& user.getUserMetadata().get(Auth0ManagementApiService.FAMILY_NAME) != null) {
this.lastName = (@Size(max = 50) String) user.getUserMetadata().get(Auth0ManagementApiService.FAMILY_NAME);
} else {
this.lastName = user.getFamilyName();
}
this.email = user.getEmail();
boolean activated = true;
if (user.isBlocked() != null) {
activated = !user.isBlocked();
}
this.activated = activated;
if (user.getUserMetadata() != null && user.getUserMetadata().get(Auth0ManagementApiService.PIC) != null) {
this.imageUrl = (@Size(max = 256) String) user.getUserMetadata().get(Auth0ManagementApiService.PIC);
} else {
this.imageUrl = user.getPicture();
}
if (user.getUserMetadata() != null) {
this.langKey = (@Size(min = 2, max = 6) String) user.getUserMetadata().get(Auth0ManagementApiService.LANG);
}
this.createdDate = user.getCreatedAt().toInstant();
this.lastModifiedDate = user.getUpdatedAt().toInstant();
if (user.getAppMetadata() != null) {
this.authorities = ((ArrayList<String>) user.getAppMetadata().get(Auth0ManagementApiService.ROLES)).stream()
.collect(Collectors.toSet());
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public boolean isActivated() {
return activated;
}
public void setActivated(boolean activated) {
this.activated = activated;
}
public String getLangKey() {
return langKey;
}
public void setLangKey(String langKey) {
this.langKey = langKey;
}
public Instant getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Instant createdDate) {
this.createdDate = createdDate;
}
public Instant getLastModifiedDate() {
return lastModifiedDate;
}
public void setLastModifiedDate(Instant lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
public Set<String> getAuthorities() {
return authorities;
}
public void setAuthorities(Set<String> authorities) {
this.authorities = authorities;
}
@Override
public String toString() {
return "UserDTO [id=" + id + ", login=" + login + ", firstName=" + firstName + ", lastName=" + lastName
+ ", email=" + email + ", imageUrl=" + imageUrl + ", activated=" + activated + ", langKey=" + langKey
+ ", createdDate=" + createdDate + ", lastModifiedDate=" + lastModifiedDate + ", authorities="
+ authorities + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment