Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
[#141] add more constraints and fields to AppUser
Browse files Browse the repository at this point in the history
  • Loading branch information
jenarp committed Jan 19, 2022
1 parent 734376e commit 4f417d3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
53 changes: 40 additions & 13 deletions src/main/java/de/bonndan/nivio/appuser/AppUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

@Entity(name = "AppUser")
@Table(
name = "user",
name = "app_user",
uniqueConstraints = {
@UniqueConstraint(name = "appuser_email_unique",
columnNames = "email")
columnNames = "email"),
@UniqueConstraint(name = "appuser_external_id_unique",
columnNames = {"external_id", "idp"})


}
)
public class AppUser implements UserDetails {
Expand All @@ -35,6 +39,20 @@ public class AppUser implements UserDetails {
)
private Long id;

@Column(
name = "external_id",
nullable = false,
columnDefinition = "VARCHAR"
)
private String externalId;

@Column(
name = "idp",
nullable = false,
columnDefinition = "VARCHAR"
)
private String idp;

@Column(
name = "name",
nullable = false,
Expand All @@ -52,14 +70,12 @@ public class AppUser implements UserDetails {

@Column(
name = "email",
nullable = false,
columnDefinition = "TEXT"
columnDefinition = "VARCHAR"
)
private String email;

@Column(
name = "avatar_url",
nullable = false,
columnDefinition = "TEXT"
)
private String avatarUrl;
Expand All @@ -73,16 +89,11 @@ public class AppUser implements UserDetails {
@Enumerated(EnumType.STRING)
private AppUserRole appUserRole;

@Column
private Boolean locked;
private Boolean enabled;

public AppUser(String name, String alias, String email, String avatarUrl, AppUserRole appUserRole) {
this.name = name;
this.alias = alias;
this.email = email;
this.avatarUrl = avatarUrl;
this.appUserRole = appUserRole;
}
@Column
private Boolean enabled;

public AppUser() {

Expand Down Expand Up @@ -188,4 +199,20 @@ public void setLocked(Boolean locked) {
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}

public String getExternalId() {
return externalId;
}

public void setExternalId(String externalId) {
this.externalId = externalId;
}

public String getIdp() {
return idp;
}

public void setIdp(String idp) {
this.idp = idp;
}
}
11 changes: 10 additions & 1 deletion src/main/java/de/bonndan/nivio/security/CustomOAuth2User.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class CustomOAuth2User implements OAuth2User {
private final String alias;
private final String name;
private final String avatarUrl;
@NonNull
private final String idp;
private final Map<String, Object> attributes;
private final Collection<? extends GrantedAuthority> authorities;

Expand All @@ -26,14 +28,16 @@ public CustomOAuth2User(@NonNull final String id,
@NonNull final String name,
@NonNull final Map<String, Object> attributes,
@NonNull final Collection<? extends GrantedAuthority> authorities,
@Nullable final String avatarUrl
@Nullable final String avatarUrl,
@NonNull final String idp
) {
this.id = Objects.requireNonNull(id, "id must not be null");
this.name = Objects.requireNonNull(name, "name must not be null");
this.alias = Objects.requireNonNull(alias, "alias must not be null");
this.attributes = Objects.requireNonNull(attributes, "attributes must not be null");
this.authorities = Objects.requireNonNull(authorities, "authorities must not be null");
this.avatarUrl = avatarUrl;
this.idp = idp;
}

@Override
Expand Down Expand Up @@ -65,4 +69,9 @@ public String getId() {
public String getAlias() {
return alias;
}

@NonNull
public String getIdp() {
return idp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public static CustomOAuth2User fromGitHubUser(@NonNull final OAuth2User user,
name,
user.getAttributes(),
user.getAuthorities(),
user.getAttribute("avatar_url"));
user.getAttribute("avatar_url"),
"github");
}


Expand All @@ -89,13 +90,14 @@ private void saveUser(CustomOAuth2User customOAuth2User) {
if (appUser.isEmpty()) {
LOGGER.info("No user found, generating profile for {}", customOAuth2User.getId());
AppUser newAppUser = new AppUser();
newAppUser.setEmail(customOAuth2User.getId());
newAppUser.setName(customOAuth2User.getName());
newAppUser.setAlias(customOAuth2User.getAlias());
newAppUser.setAvatarUrl(customOAuth2User.getAvatarUrl());
newAppUser.setAppUserRole(AppUserRole.USER);
newAppUser.setLocked(false);
newAppUser.setEnabled(true);
newAppUser.setExternalId(customOAuth2User.getId());
newAppUser.setIdp(customOAuth2User.getIdp());

appUserRepository.save(newAppUser);
}
Expand Down

0 comments on commit 4f417d3

Please sign in to comment.