Skip to content

Commit

Permalink
default Group Enrollment configuration taking into account rules
Browse files Browse the repository at this point in the history
  • Loading branch information
cgeorgilakis-grnet committed Mar 19, 2024
1 parent cc154ea commit b502910
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/rciam/plugins/groups/helpers/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static Response addGroupChild(GroupRepresentation rep, RealmModel realm,
if (groupEnrollmentConfigurationRepository.getByGroup(rep.getId()).collect(Collectors.toList()).isEmpty()) {
//group configuration creation
groupRolesRepository.create(Utils.defaultGroupRole, rep.getId());
groupEnrollmentConfigurationRepository.createDefault(child, rep.getName());
groupEnrollmentConfigurationRepository.createDefault(child, rep.getName(), realm.getId());
}
return Response.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public Response addTopLevelGroup(org.keycloak.representations.idm.GroupRepresent
if (groupEnrollmentConfigurationRepository.getByGroup(rep.getId()).collect(Collectors.toList()).isEmpty()) {
//group creation - group configuration no exist
groupRolesRepository.create(Utils.defaultGroupRole,rep.getId());
groupEnrollmentConfigurationRepository.createDefault(realm.getGroupById(rep.getId()), rep.getName());
groupEnrollmentConfigurationRepository.createDefault(realm.getGroupById(rep.getId()), rep.getName(), realm.getId());
}

adminEvent.operation(OperationType.CREATE).resourcePath(session.getContext().getUri(), child.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.rciam.plugins.groups.jpa.repositories;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -9,19 +10,24 @@
import org.keycloak.models.RealmModel;
import org.keycloak.models.jpa.entities.GroupEntity;
import org.keycloak.models.utils.KeycloakModelUtils;
import org.rciam.plugins.groups.enums.GroupAupTypeEnum;
import org.rciam.plugins.groups.enums.GroupTypeEnum;
import org.rciam.plugins.groups.helpers.Utils;
import org.rciam.plugins.groups.jpa.entities.GroupAupEntity;
import org.rciam.plugins.groups.jpa.entities.GroupEnrollmentConfigurationEntity;
import org.rciam.plugins.groups.jpa.entities.GroupEnrollmentConfigurationRulesEntity;
import org.rciam.plugins.groups.jpa.entities.GroupRolesEntity;
import org.rciam.plugins.groups.representations.GroupAupRepresentation;
import org.rciam.plugins.groups.representations.GroupEnrollmentConfigurationRepresentation;

public class GroupEnrollmentConfigurationRepository extends GeneralRepository<GroupEnrollmentConfigurationEntity> {

private GroupRolesRepository groupRolesRepository;
private final GroupEnrollmentConfigurationRulesRepository groupEnrollmentConfigurationRulesRepository;

public GroupEnrollmentConfigurationRepository(KeycloakSession session, RealmModel realm) {
super(session, realm);
this.groupEnrollmentConfigurationRulesRepository = new GroupEnrollmentConfigurationRulesRepository(session);
}

public void setGroupRolesRepository(GroupRolesRepository groupRolesRepository) {
Expand All @@ -43,29 +49,45 @@ public void create(GroupEnrollmentConfigurationRepresentation rep, String groupI
create(entity);
}

public void createDefault(GroupModel group, String groupName) {
public void createDefault(GroupModel group, String groupName, String realmId) {
//default values, hide by default
List<GroupEnrollmentConfigurationRulesEntity> configurationRulesList = groupEnrollmentConfigurationRulesRepository.getByRealmAndType(realmId, group.getParentId() == null ? GroupTypeEnum.TOP_LEVEL : GroupTypeEnum.SUBGROUP).collect(Collectors.toList());
GroupEnrollmentConfigurationEntity entity = new GroupEnrollmentConfigurationEntity();
entity.setId(KeycloakModelUtils.generateId());
GroupEntity groupEntity = new GroupEntity();
groupEntity.setId(group.getId());
entity.setGroup(groupEntity);
entity.setName(groupName);
entity.setRequireApproval(true);
entity.setRequireApprovalForExtension(true);
entity.setActive(true);
entity.setVisibleToNotMembers(true);
entity.setMultiselectRole(true);
entity.setRequireApproval(configurationRulesList.stream().noneMatch(x -> "requireApproval".equals(x.getField()) && "false".equals(x.getDefaultValue())) );
entity.setRequireApprovalForExtension(configurationRulesList.stream().noneMatch(x -> "requireApprovalForExtension".equals(x.getField()) && "false".equals(x.getDefaultValue())) );
entity.setActive(configurationRulesList.stream().noneMatch(x -> "active".equals(x.getField()) && "false".equals(x.getDefaultValue())));
entity.setVisibleToNotMembers(configurationRulesList.stream().noneMatch(x -> "visibleToNotMembers".equals(x.getField()) && "false".equals(x.getDefaultValue())) );
entity.setMultiselectRole(configurationRulesList.stream().noneMatch(x -> "multiselectRole".equals(x.getField()) && "false".equals(x.getDefaultValue())));
entity.setGroupRoles(groupRolesRepository.getGroupRolesByGroup(group.getId()).map(x -> {
GroupRolesEntity r = new GroupRolesEntity();
r.setId(x.getId());
r.setGroup(x.getGroup());
r.setName(x.getName());
return r;
}).collect(Collectors.toList()));
entity.setCommentsNeeded(true);
entity.setCommentsLabel("Comments");
entity.setCommentsDescription("Why do you want to join the group?");
entity.setCommentsNeeded(configurationRulesList.stream().noneMatch(x -> "multiselectRole".equals(x.getField()) && "false".equals(x.getDefaultValue())) );
if (entity.getCommentsNeeded()) {
String label = configurationRulesList.stream().filter(x -> "commentsLabel".equals(x.getField())).findAny().orElse(new GroupEnrollmentConfigurationRulesEntity()).getDefaultValue();
entity.setCommentsLabel(label != null ? label : "Comments");
String description = configurationRulesList.stream().filter(x -> "commentsDescription".equals(x.getField())).findAny().orElse(new GroupEnrollmentConfigurationRulesEntity()).getDefaultValue();
entity.setCommentsDescription(description != null ? description : "Why do you want to join the group?");
}
String membershipExpirationDaysDefault = configurationRulesList.stream().filter(x -> "membershipExpirationDays".equals(x.getField())).findAny().orElse(new GroupEnrollmentConfigurationRulesEntity()).getDefaultValue();
if (membershipExpirationDaysDefault != null)
entity.setMembershipExpirationDays(Long.valueOf(membershipExpirationDaysDefault));
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.setType(GroupAupTypeEnum.URL);
aup.setUrl(aupDefault);
entity.setAupEntity(aup);
}
create(entity);
group.setSingleAttribute(Utils.DEFAULT_CONFIGURATION_NAME, entity.getId());
}
Expand Down

0 comments on commit b502910

Please sign in to comment.