Skip to content

Commit

Permalink
Fix bug when aup entity was updated/ removed from enrollment configur…
Browse files Browse the repository at this point in the history
…ation entity
  • Loading branch information
cgeorgilakis-grnet committed Feb 3, 2025
1 parent 7d7c7df commit f3aae67
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 38 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes in keycloak-group-management will be documented in this file
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.4.1]- 2025-02-03

### Fixed
- Fix bug when aup entity was updated/ removed from enrollment configuration entity

## [1.4.0] - 2025-01-23

### Changed
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<quarkus.version>3.2.7.Final</quarkus.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<agm-version>1.5.0</agm-version>
<agm-version>1.4.1</agm-version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public static GroupEnrollmentConfigurationRepresentation toRepresentation(GroupE

private static GroupAupRepresentation toRepresentation(GroupAupEntity entity) {
GroupAupRepresentation rep = new GroupAupRepresentation();
rep.setId(entity.getId());
rep.setType(entity.getType());
rep.setContent(entity.getContent());
rep.setMimeType(entity.getMimeType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ public class GroupAupEntity {

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

@OneToOne
@MapsId
@JoinColumn(name = "ID")
private GroupEnrollmentConfigurationEntity groupEnrollmentConfiguration;

@Column(name="TYPE")
@Enumerated(EnumType.STRING)
private GroupAupTypeEnum type;
Expand All @@ -37,6 +41,14 @@ public void setId(String id) {
this.id = id;
}

public GroupEnrollmentConfigurationEntity getGroupEnrollmentConfiguration() {
return groupEnrollmentConfiguration;
}

public void setGroupEnrollmentConfiguration(GroupEnrollmentConfigurationEntity groupEnrollmentConfiguration) {
this.groupEnrollmentConfiguration = groupEnrollmentConfiguration;
}

public GroupAupTypeEnum getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,9 @@
import java.time.LocalDate;
import java.util.List;

import jakarta.persistence.*;
import org.keycloak.models.jpa.entities.GroupEntity;

import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;

@Entity
@Table(name = "GROUP_ENROLLMENT_CONFIGURATION")
@NamedQueries({
Expand All @@ -45,8 +31,8 @@ public class GroupEnrollmentConfigurationEntity {
@Column(name = "ACTIVE")
private Boolean active;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "AUP_ID")
@OneToOne(mappedBy = "groupEnrollmentConfiguration", cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private GroupAupEntity aupEntity;

@Column(name = "REQUIRE_APPROVAL")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public void createDefault(GroupModel group, String groupName, String realmId) {
String aupDefault = configurationRulesList.stream().filter(x -> "aupEntity".equals(x.getField())).findAny().orElse(new GroupEnrollmentConfigurationRulesEntity()).getDefaultValue();
if (aupDefault != null) {
GroupAupEntity aup = new GroupAupEntity();
aup.setId(KeycloakModelUtils.generateId());
aup.setId(entity.getId());
aup.setGroupEnrollmentConfiguration(entity);
aup.setType(GroupAupTypeEnum.URL);
aup.setUrl(aupDefault);
entity.setAupEntity(aup);
Expand Down Expand Up @@ -123,11 +124,6 @@ private void toEntity(GroupEnrollmentConfigurationEntity entity, GroupEnrollment
entity.setInvitationConclusion(rep.getInvitationConclusion());
entity.setInvitationIntroduction(rep.getInvitationIntroduction());
entity.setMultiselectRole(rep.getMultiselectRole());
if (rep.getAup() != null) {
entity.setAupEntity(toEntity(rep.getAup(), entity.getAupEntity()));
} else {
entity.setAupEntity(null);
}
if (rep.getGroupRoles() != null) {
entity.setGroupRoles(rep.getGroupRoles().stream().map(x -> {
GroupRolesEntity r = groupRolesRepository.getGroupRolesByNameAndGroup(x, groupId);
Expand All @@ -144,6 +140,11 @@ private void toEntity(GroupEnrollmentConfigurationEntity entity, GroupEnrollment
} else {
entity.setGroupRoles(null);
}
if (rep.getAup() != null) {
entity.setAupEntity(toEntity(rep.getAup(), entity));
} else {
entity.setAupEntity(null);
}
entity.setCommentsNeeded(rep.getCommentsNeeded());
if (rep.getCommentsNeeded()){
entity.setCommentsLabel(rep.getCommentsLabel());
Expand All @@ -155,11 +156,14 @@ private void toEntity(GroupEnrollmentConfigurationEntity entity, GroupEnrollment

}

private GroupAupEntity toEntity(GroupAupRepresentation rep, GroupAupEntity entity) {
if (entity == null)
private GroupAupEntity toEntity(GroupAupRepresentation rep, GroupEnrollmentConfigurationEntity configuration) {
GroupAupEntity entity = configuration.getAupEntity();
if (entity == null) {
entity = new GroupAupEntity();
entity.setId(configuration.getId());
entity.setGroupEnrollmentConfiguration(configuration);
}

entity.setId(rep.getId() != null ? rep.getId() : KeycloakModelUtils.generateId());
entity.setType(rep.getType());
entity.setContent(rep.getContent());
entity.setMimeType(rep.getMimeType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

public class GroupAupRepresentation {

private String id;
private GroupAupTypeEnum type;
private String mimeType;
private Object content;
Expand All @@ -13,14 +12,6 @@ public class GroupAupRepresentation {

public GroupAupRepresentation(){}

public String getId() {
return id;
}

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

public GroupAupTypeEnum getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ public Response saveGroupEnrollmentConfiguration(GroupEnrollmentConfigurationRep
}

rule = groupEnrollmentConfigurationRulesRepository.getByRealmAndTypeAndField(realm.getId(), group.getParentId() != null ? GroupTypeEnum.SUBGROUP : GroupTypeEnum.TOP_LEVEL, "aup");
if ( rep.getAup() != null && rep.getAup().getUrl() == null){
rep.setAup(null);
}
if (rule != null && rule.getRequired() && rep.getAup() == null) {
throw new BadRequestException("Aup must not be empty");
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/resources/META-INF/group-management-changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -829,4 +829,27 @@
</update>
</changeSet>

<changeSet author="[email protected]" id="update_group_aup">
<dropIndex indexName="KC_GROUP_CONFIG_IDX" tableName="GROUP_ENROLLMENT_CONFIGURATION"/>
<dropIndex indexName="KC_GROUP_CONFIG_AID_IDX" tableName="GROUP_ENROLLMENT_CONFIGURATION"/>
<dropForeignKeyConstraint baseTableName="GROUP_ENROLLMENT_CONFIGURATION" constraintName="fk_kc_grp_cfg_aid"/>
<sql>
UPDATE GROUP_AUP
SET ID = (
SELECT f.ID
FROM GROUP_ENROLLMENT_CONFIGURATION f
WHERE f.AUP_ID = GROUP_AUP.ID
LIMIT 1
);
</sql>
<dropColumn tableName="GROUP_ENROLLMENT_CONFIGURATION" columnName="AUP_ID"/>
<addForeignKeyConstraint
constraintName="fk_group_aup_enrollment_configuration"
baseTableName="GROUP_AUP"
baseColumnNames="ID"
referencedTableName="GROUP_ENROLLMENT_CONFIGURATION"
referencedColumnNames="ID"
/>
</changeSet>

</databaseChangeLog>

0 comments on commit f3aae67

Please sign in to comment.