forked from p2-inc/keycloak-orgs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
p2-inc#287 Add attributes to an OrganizationMembership
- Loading branch information
Showing
9 changed files
with
249 additions
and
1 deletion.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/io/phasetwo/service/model/OrganizationMembershipModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.phasetwo.service.model; | ||
|
||
public interface OrganizationMembershipModel extends WithAttributes { | ||
|
||
String getId(); | ||
|
||
String getUserId(); | ||
|
||
OrganizationModel getOrganization(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/io/phasetwo/service/model/jpa/OrganizationMembershipAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.phasetwo.service.model.jpa; | ||
|
||
import io.phasetwo.service.model.OrganizationMembershipModel; | ||
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 jakarta.persistence.EntityManager; | ||
import org.keycloak.common.util.MultivaluedHashMap; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.models.jpa.JpaModel; | ||
import org.keycloak.models.utils.KeycloakModelUtils; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OrganizationMembershipAdapter implements OrganizationMembershipModel, JpaModel<OrganizationMemberEntity> { | ||
|
||
protected final KeycloakSession session; | ||
protected final OrganizationMemberEntity organizationMemberEntity; | ||
protected final EntityManager em; | ||
protected final RealmModel realm; | ||
|
||
public OrganizationMembershipAdapter( | ||
KeycloakSession session, RealmModel realm, EntityManager em, OrganizationMemberEntity organizationMemberEntity) { | ||
this.session = session; | ||
this.em = em; | ||
this.organizationMemberEntity = organizationMemberEntity; | ||
this.realm = realm; | ||
} | ||
|
||
@Override | ||
public OrganizationMemberEntity getEntity() { | ||
return organizationMemberEntity; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return organizationMemberEntity.getId(); | ||
} | ||
|
||
@Override | ||
public String getUserId() { | ||
return organizationMemberEntity.getUserId(); | ||
} | ||
|
||
@Override | ||
public OrganizationModel getOrganization() { | ||
return session | ||
.getProvider(OrganizationProvider.class) | ||
.getOrganizationById(realm, organizationMemberEntity.getOrganization().getId()); | ||
} | ||
|
||
@Override | ||
public Map<String, List<String>> getAttributes() { | ||
MultivaluedHashMap<String, String> result = new MultivaluedHashMap<>(); | ||
for (OrganizationMembershipAttributeEntity attr : organizationMemberEntity.getAttributes()) { | ||
result.add(attr.getName(), attr.getValue()); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public void removeAttribute(String name) { | ||
organizationMemberEntity.getAttributes().removeIf(attribute -> attribute.getName().equals(name)); | ||
} | ||
|
||
@Override | ||
public void removeAttributes() { | ||
organizationMemberEntity.getAttributes().clear(); | ||
} | ||
|
||
@Override | ||
public void setAttribute(String name, List<String> values) { | ||
removeAttribute(name); | ||
for (String value : values) { | ||
OrganizationMembershipAttributeEntity a = new OrganizationMembershipAttributeEntity(); | ||
a.setId(KeycloakModelUtils.generateId()); | ||
a.setName(name); | ||
a.setValue(value); | ||
a.setOrganizationMember(organizationMemberEntity); | ||
em.persist(a); | ||
organizationMemberEntity.getAttributes().add(a); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...main/java/io/phasetwo/service/model/jpa/entity/OrganizationMembershipAttributeEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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 org.hibernate.annotations.Nationalized; | ||
|
||
import java.util.Objects; | ||
|
||
@Table( | ||
name = "ORGANIZATION_MEMBER_ATTRIBUTE", | ||
uniqueConstraints = {@UniqueConstraint(columnNames = {"ORGANIZATION_MEMBER_ID", "NAME"})}) | ||
@Entity | ||
public class OrganizationMembershipAttributeEntity { | ||
|
||
@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 = "ORGANIZATION_MEMBER_ID") | ||
protected OrganizationMemberEntity organizationMember; | ||
|
||
@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 OrganizationMemberEntity getOrganizationMember() { | ||
return organizationMember; | ||
} | ||
|
||
public void setOrganizationMember(OrganizationMemberEntity organizationMember) { | ||
this.organizationMember = organizationMember; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) return false; | ||
OrganizationMembershipAttributeEntity that = (OrganizationMembershipAttributeEntity) o; | ||
return Objects.equals(id, that.id) && Objects.equals(organizationMember, that.organizationMember); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, organizationMember); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/resources/META-INF/jpa-changelog-phasetwo-20241228.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.1" encoding="UTF-8" standalone="no"?> | ||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> | ||
<changeSet author="rtuf" id="organization-member-attr-1"> | ||
<createTable tableName="ORGANIZATION_MEMBER_ATTRIBUTE"> | ||
<column name="ID" type="VARCHAR(36)"> | ||
<constraints primaryKey="true" primaryKeyName="ORGANIZATION_MEMBER_ATTRIBUTEPK" nullable="false"/> | ||
</column> | ||
<column name="NAME" type="VARCHAR(255)"/> | ||
<column name="VALUE" type="NVARCHAR(255)"/> | ||
<column name="ORGANIZATION_MEMBER_ID" type="VARCHAR(36)"/> | ||
</createTable> | ||
<addForeignKeyConstraint baseColumnNames="ORGANIZATION_MEMBER_ID" baseTableName="ORGANIZATION_MEMBER_ATTRIBUTE" constraintName="FK_RA9TUF8D9JRH6MMR9PY7UFL7NBJP0R" deferrable="false" initiallyDeferred="false" referencedColumnNames="ID" referencedTableName="ORGANIZATION_MEMBER" onDelete="CASCADE"/> | ||
<createIndex indexName="IDX_ORGANIZATION_MEMBER_ATTRIBUTE" tableName="ORGANIZATION_MEMBER_ATTRIBUTE"> | ||
<column name="ORGANIZATION_MEMBER_ID" type="VARCHAR(36)"/> | ||
</createIndex> | ||
</changeSet> | ||
</databaseChangeLog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters