Skip to content

Commit

Permalink
p2-inc#287 Add attributes to an OrganizationMembers
Browse files Browse the repository at this point in the history
  • Loading branch information
rtufisi committed Jan 3, 2025
1 parent 05c159e commit 3945c0f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.phasetwo.service.model;

import java.util.List;
import java.util.Map;

public interface OrganizationMemberModel extends WithAttributes {

String getId();

String getUserId();

OrganizationModel getOrganization();

List<String> getRoles();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public interface OrganizationModel extends WithAttributes {

Stream<UserModel> searchForMembersStream(String search, Integer firstResult, Integer maxResults);

Stream<OrganizationMembershipModel> getOrganizationMembersStream();
Stream<OrganizationMemberModel> getOrganizationMembersStream();

Stream<OrganizationMembershipModel> searchForOrganizationMembersStream(String search, Integer firstResult, Integer maxResults);
Stream<OrganizationMemberModel> searchForOrganizationMembersStream(String search, Integer firstResult, Integer maxResults);

boolean hasMembership(UserModel user);

Expand Down Expand Up @@ -78,7 +78,7 @@ default OrganizationRoleModel getRoleByName(String name) {
.orElse(null);
}

OrganizationMembershipModel getMembershipDetails(UserModel user);
OrganizationMemberModel getMembershipDetails(UserModel user);

void removeRole(String name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import com.google.common.base.Strings;
import io.phasetwo.service.model.DomainModel;
import io.phasetwo.service.model.InvitationModel;
import io.phasetwo.service.model.OrganizationMembershipModel;
import io.phasetwo.service.model.OrganizationMemberModel;
import io.phasetwo.service.model.OrganizationModel;
import io.phasetwo.service.model.OrganizationRoleModel;
import io.phasetwo.service.model.jpa.entity.DomainEntity;
import io.phasetwo.service.model.jpa.entity.ExtOrganizationEntity;
import io.phasetwo.service.model.jpa.entity.InvitationEntity;
import io.phasetwo.service.model.jpa.entity.OrganizationAttributeEntity;
import io.phasetwo.service.model.jpa.entity.OrganizationMemberEntity;
import io.phasetwo.service.model.jpa.entity.OrganizationMembershipAttributeEntity;
import io.phasetwo.service.model.jpa.entity.OrganizationRoleEntity;
import io.phasetwo.service.model.jpa.entity.UserOrganizationRoleMappingEntity;
import io.phasetwo.service.util.IdentityProviders;
Expand Down Expand Up @@ -191,16 +190,16 @@ public Stream<UserModel> searchForMembersStream(
}

@Override
public Stream<OrganizationMembershipModel> getOrganizationMembersStream() {
public Stream<OrganizationMemberModel> getOrganizationMembersStream() {
TypedQuery<OrganizationMemberEntity> query = em.createNamedQuery("getOrganizationMembers", OrganizationMemberEntity.class);
query.setParameter("organization", org);

return query.getResultStream()
.map(organizationMemberEntity -> new OrganizationMembershipAdapter(session, realm, em, organizationMemberEntity));
.map(organizationMemberEntity -> new OrganizationMemberAdapter(session, realm, em, organizationMemberEntity));
}

@Override
public Stream<OrganizationMembershipModel> searchForOrganizationMembersStream(String search, Integer firstResult, Integer maxResults) {
public Stream<OrganizationMemberModel> searchForOrganizationMembersStream(String search, Integer firstResult, Integer maxResults) {
return Stream.empty();
}

Expand Down Expand Up @@ -318,8 +317,8 @@ public Stream<OrganizationRoleModel> getRolesByUserStream(UserModel user) {
}

@Override
public OrganizationMembershipModel getMembershipDetails(UserModel user) {
TypedQuery<OrganizationMembershipModel> query = em.createNamedQuery("getOrganizationMemberByUserId", OrganizationMembershipModel.class);
public OrganizationMemberModel getMembershipDetails(UserModel user) {
TypedQuery<OrganizationMemberModel> query = em.createNamedQuery("getOrganizationMemberByUserId", OrganizationMemberModel.class);
query.setParameter("organization", org);
query.setParameter("id", user.getId());
return query.getSingleResult();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.phasetwo.service.model.jpa;

import io.phasetwo.service.model.OrganizationMembershipModel;
import io.phasetwo.service.model.OrganizationMemberModel;
import io.phasetwo.service.model.OrganizationModel;
import io.phasetwo.service.model.OrganizationProvider;
import io.phasetwo.service.model.jpa.entity.OrganizationMemberEntity;
import io.phasetwo.service.model.jpa.entity.OrganizationMembershipAttributeEntity;
import io.phasetwo.service.model.jpa.entity.OrganizationMemberAttributeEntity;
import jakarta.persistence.EntityManager;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.models.KeycloakSession;
Expand All @@ -15,14 +15,14 @@
import java.util.List;
import java.util.Map;

public class OrganizationMembershipAdapter implements OrganizationMembershipModel, JpaModel<OrganizationMemberEntity> {
public class OrganizationMemberAdapter implements OrganizationMemberModel, JpaModel<OrganizationMemberEntity> {

protected final KeycloakSession session;
protected final OrganizationMemberEntity organizationMemberEntity;
protected final EntityManager em;
protected final RealmModel realm;

public OrganizationMembershipAdapter(
public OrganizationMemberAdapter(
KeycloakSession session, RealmModel realm, EntityManager em, OrganizationMemberEntity organizationMemberEntity) {
this.session = session;
this.em = em;
Expand Down Expand Up @@ -52,10 +52,15 @@ public OrganizationModel getOrganization() {
.getOrganizationById(realm, organizationMemberEntity.getOrganization().getId());
}

@Override
public List<String> getRoles() {
return List.of();
}

@Override
public Map<String, List<String>> getAttributes() {
MultivaluedHashMap<String, String> result = new MultivaluedHashMap<>();
for (OrganizationMembershipAttributeEntity attr : organizationMemberEntity.getAttributes()) {
for (OrganizationMemberAttributeEntity attr : organizationMemberEntity.getAttributes()) {
result.add(attr.getName(), attr.getValue());
}
return result;
Expand All @@ -75,7 +80,7 @@ public void removeAttributes() {
public void setAttribute(String name, List<String> values) {
removeAttribute(name);
for (String value : values) {
OrganizationMembershipAttributeEntity a = new OrganizationMembershipAttributeEntity();
OrganizationMemberAttributeEntity a = new OrganizationMemberAttributeEntity();
a.setId(KeycloakModelUtils.generateId());
a.setName(name);
a.setValue(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class OrganizationEntityProvider implements JpaEntityProvider {
ExtOrganizationEntity.class,
OrganizationAttributeEntity.class,
OrganizationMemberEntity.class,
OrganizationMembershipAttributeEntity.class,
OrganizationMemberAttributeEntity.class,
OrganizationRoleEntity.class,
UserOrganizationRoleMappingEntity.class,
InvitationEntity.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
name = "ORGANIZATION_MEMBER_ATTRIBUTE",
uniqueConstraints = {@UniqueConstraint(columnNames = {"ORGANIZATION_MEMBER_ID", "NAME"})})
@Entity
public class OrganizationMembershipAttributeEntity {
public class OrganizationMemberAttributeEntity {

@Id
@Column(name = "ID", length = 36)
Expand Down Expand Up @@ -73,7 +73,7 @@ public void setValue(String value) {
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
OrganizationMembershipAttributeEntity that = (OrganizationMembershipAttributeEntity) o;
OrganizationMemberAttributeEntity that = (OrganizationMemberAttributeEntity) o;
return Objects.equals(id, that.id) && Objects.equals(organizationMember, that.organizationMember);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class OrganizationMemberEntity {
protected Date createdAt;

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

@PrePersist
protected void onCreate() {
Expand Down Expand Up @@ -105,11 +105,11 @@ public void setCreatedAt(Date at) {
createdAt = at;
}

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

public void setAttributes(Collection<OrganizationMembershipAttributeEntity> attributes) {
public void setAttributes(Collection<OrganizationMemberAttributeEntity> attributes) {
this.attributes = attributes;
}

Expand Down

0 comments on commit 3945c0f

Please sign in to comment.