Skip to content

Commit

Permalink
invite attributes (#153)
Browse files Browse the repository at this point in the history
* invite attributes stubbed

* updated OrganizationModel with getInvitation(id). added get method for invite by id.

* working tests for attributes. added attributes to ftl context.

* added test for multiple valued attributes

* no wildcard imports

* adding #158 for resending emails

* clean up and set the inviterUrl

* fixed test
  • Loading branch information
xgp authored Feb 1, 2024
1 parent 86fe178 commit 25f84d5
Show file tree
Hide file tree
Showing 16 changed files with 466 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Set;
import org.keycloak.models.UserModel;

public interface InvitationModel {
public interface InvitationModel extends WithAttributes {

String getId();

Expand Down
37 changes: 3 additions & 34 deletions src/main/java/io/phasetwo/service/model/OrganizationModel.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.phasetwo.service.model;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.MoreCollectors;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import org.keycloak.models.IdentityProviderModel;
Expand All @@ -12,7 +9,7 @@
import org.keycloak.models.UserModel;
import org.keycloak.provider.ProviderEvent;

public interface OrganizationModel {
public interface OrganizationModel extends WithAttributes {

String getId();

Expand All @@ -38,36 +35,6 @@ public interface OrganizationModel {

UserModel getCreatedBy();

Map<String, List<String>> getAttributes();

default Stream<String> getAttributesStream(String name) {
List<String> attrs = getAttributes().get(name);
if (attrs != null && attrs.size() > 0) {
return attrs.stream();
} else {
return Stream.empty();
}
}

default String getFirstAttribute(String name) {
List<String> attrs = getAttributes().get(name);
if (attrs != null && attrs.size() > 0) {
return attrs.get(0);
} else {
return null;
}
}

void removeAttributes();

void removeAttribute(String name);

void setAttribute(String name, List<String> values);

default void setSingleAttribute(String name, String value) {
setAttribute(name, ImmutableList.of(value));
}

Long getMembersCount();

Stream<UserModel> getMembersStream();
Expand All @@ -86,6 +53,8 @@ default Stream<InvitationModel> getInvitationsByEmail(String email) {
return getInvitationsStream().filter(i -> i.getEmail().equals(email));
}

InvitationModel getInvitation(String id);

void revokeInvitation(String id);

void revokeInvitations(String email);
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/io/phasetwo/service/model/WithAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.phasetwo.service.model;

import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public interface WithAttributes {
Map<String, List<String>> getAttributes();

default Stream<String> getAttributesStream(String name) {
List<String> attrs = getAttributes().get(name);
if (attrs != null && attrs.size() > 0) {
return attrs.stream();
} else {
return Stream.empty();
}
}

default String getFirstAttribute(String name) {
List<String> attrs = getAttributes().get(name);
if (attrs != null && attrs.size() > 0) {
return attrs.get(0);
} else {
return null;
}
}

void removeAttributes();

void removeAttribute(String name);

void setAttribute(String name, List<String> values);

default void setSingleAttribute(String name, String value) {
setAttribute(name, ImmutableList.of(value));
}
}
38 changes: 38 additions & 0 deletions src/main/java/io/phasetwo/service/model/jpa/InvitationAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
import io.phasetwo.service.model.InvitationModel;
import io.phasetwo.service.model.OrganizationModel;
import io.phasetwo.service.model.OrganizationProvider;
import io.phasetwo.service.model.jpa.entity.InvitationAttributeEntity;
import io.phasetwo.service.model.jpa.entity.InvitationEntity;
import jakarta.persistence.EntityManager;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.jpa.JpaModel;
import org.keycloak.models.utils.KeycloakModelUtils;

public class InvitationAdapter implements InvitationModel, JpaModel<InvitationEntity> {

Expand Down Expand Up @@ -99,4 +104,37 @@ public Set<String> getRoles() {
public void setRoles(Collection<String> roles) {
invitation.setRoles(Sets.newHashSet(roles));
}

@Override
public Map<String, List<String>> getAttributes() {
MultivaluedHashMap<String, String> result = new MultivaluedHashMap<>();
for (InvitationAttributeEntity attr : invitation.getAttributes()) {
result.add(attr.getName(), attr.getValue());
}
return result;
}

@Override
public void removeAttribute(String name) {
invitation.getAttributes().removeIf(attribute -> attribute.getName().equals(name));
}

@Override
public void removeAttributes() {
invitation.getAttributes().clear();
}

@Override
public void setAttribute(String name, List<String> values) {
removeAttribute(name);
for (String value : values) {
InvitationAttributeEntity a = new InvitationAttributeEntity();
a.setId(KeycloakModelUtils.generateId());
a.setName(name);
a.setValue(value);
a.setInvitation(invitation);
em.persist(a);
invitation.getAttributes().add(a);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ public List<InvitationEntity> getInvitationsByRealmAndEmail(String realmName, St
return org.getInvitations().stream().map(i -> new InvitationAdapter(session, realm, em, i));
}

@Override
public InvitationModel getInvitation(String id) {
InvitationEntity ie = em.find(InvitationEntity.class, id);
if (ie != null && ie.getOrganization().equals(org)) {
return new InvitationAdapter(session, realm, em, ie);
} else {
return null;
}
}

@Override
public void revokeInvitation(String id) {
org.getInvitations().removeIf(inv -> inv.getId().equals(id));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.phasetwo.service.model.jpa.entity;

import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import java.util.Objects;
import org.hibernate.annotations.Nationalized;

/** */
@Table(
name = "INVITATION_ATTRIBUTE",
uniqueConstraints = {@UniqueConstraint(columnNames = {"INVITATION_ID", "NAME"})})
@Entity
public class InvitationAttributeEntity {

@Id
@Column(name = "ID", length = 36)
@Access(
AccessType.PROPERTY) // we do this because relationships often fetch id, but not entity. This
// avoids an extra SQL
protected String id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "INVITATION_ID")
protected InvitationEntity invitation;

@Column(name = "NAME")
protected String name;

@Nationalized
@Column(name = "VALUE")
protected String value;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public InvitationEntity getInvitation() {
return invitation;
}

public void setInvitation(InvitationEntity invitation) {
this.invitation = invitation;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (!(o instanceof InvitationAttributeEntity)) return false;

InvitationAttributeEntity key = (InvitationAttributeEntity) o;

if (!name.equals(key.name)) return false;
if (!invitation.equals(key.invitation)) return false;

return true;
}

@Override
public int hashCode() {
return Objects.hash(invitation, name);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.phasetwo.service.model.jpa.entity;

import static io.phasetwo.service.model.jpa.entity.Entities.setCollection;

import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import java.util.ArrayList;
Expand Down Expand Up @@ -56,6 +58,10 @@ public class InvitationEntity {
@JoinColumn(name = "ORGANIZATION_ID")
private OrganizationEntity organization;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "invitation")
protected Collection<InvitationAttributeEntity> attributes =
new ArrayList<InvitationAttributeEntity>();

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name = "INVITATION_TEAM",
Expand Down Expand Up @@ -146,6 +152,14 @@ public void setRoles(Set<String> roles) {
this.roles = roles;
}

public Collection<InvitationAttributeEntity> getAttributes() {
return attributes;
}

public void setAttributes(Collection<InvitationAttributeEntity> attributes) {
setCollection(attributes, this.attributes);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class OrganizationEntityProvider implements JpaEntityProvider {
TeamAttributeEntity.class,
TeamMemberEntity.class,
UserOrganizationRoleMappingEntity.class,
InvitationEntity.class
InvitationEntity.class,
InvitationAttributeEntity.class
};

@Override
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/io/phasetwo/service/representation/Invitation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import jakarta.validation.Valid;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;

Expand All @@ -20,6 +22,7 @@ public class Invitation {
private @Valid String organizationId = null;
private @Valid List<String> teamIds = Lists.newArrayList();
private @Valid List<String> roles = Lists.newArrayList();
private @Valid Map<String, List<String>> attributes = Maps.newHashMap();

public Invitation id(String id) {
this.id = id;
Expand Down Expand Up @@ -167,6 +170,25 @@ public void setRoles(List<String> roles) {
this.roles = roles;
}

public Invitation attribute(String name, String value) {
List<String> list = this.attributes.get(name);
if (list == null) {
list = Lists.newArrayList();
}
if (!list.contains(value)) list.add(value);
this.attributes.put(name, list);
return this;
}

@JsonProperty("attributes")
public Map<String, List<String>> getAttributes() {
return attributes;
}

public void setAttributes(Map<String, List<String>> attributes) {
this.attributes = attributes;
}

@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
Expand All @@ -184,12 +206,14 @@ public boolean equals(java.lang.Object o) {
&& Objects.equals(organizationId, invitation.organizationId)
&& Objects.equals(invitationUrl, invitation.invitationUrl)
&& Objects.equals(roles, invitation.roles)
&& Objects.equals(teamIds, invitation.teamIds);
&& Objects.equals(teamIds, invitation.teamIds)
&& Objects.equals(attributes, invitation.attributes);
}

@Override
public int hashCode() {
return Objects.hash(id, email, createdAt, inviterId, organizationId, invitationUrl, roles);
return Objects.hash(
id, email, createdAt, inviterId, organizationId, invitationUrl, roles, attributes);
}

@Override
Expand All @@ -205,6 +229,7 @@ public String toString() {
sb.append(" invitationUrl: ").append(toIndentedString(invitationUrl)).append("\n");
sb.append(" teamIds: ").append(toIndentedString(teamIds)).append("\n");
sb.append(" roles: ").append(toIndentedString(roles)).append("\n");
sb.append(" attributes: ").append(toIndentedString(attributes)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down
Loading

0 comments on commit 25f84d5

Please sign in to comment.