Skip to content

Commit 1968093

Browse files
author
Dominik Frantisek Bucik
committed
feat: 🎸 Filter our embedded groups where user is member
Filter out embedded groups from options where user is already member. If empty set of options remains, filter out form item copletely.
1 parent baf35f7 commit 1968093

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

‎perun-registrar-lib/src/main/java/cz/metacentrum/perun/registrar/impl/RegistrarManagerImpl.java‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2997,13 +2997,24 @@ public List<ApplicationFormItemWithPrefilledValue> getFormItemsWithPrefilledValu
29972997
throw new MissingRequiredDataException("The administrator set up this form wrongly OR you don't match any conditions OR your IDP doesn't provide data required by this application form.", itemsWithMissingData);
29982998
}
29992999

3000-
itemsWithValues.stream()
3001-
.filter(item -> item.getFormItem().getType() == EMBEDDED_GROUP_APPLICATION)
3002-
.forEach(item -> setGroupsToCheckBoxForGroups(sess, item, vo, group));
3000+
Iterator<ApplicationFormItemWithPrefilledValue> itemsIt = itemsWithValues.iterator();
3001+
while (itemsIt.hasNext()) {
3002+
ApplicationFormItemWithPrefilledValue item = itemsIt.next();
3003+
// Process only EMBEDDED_GROUP_APPLICATION items in this block
3004+
if (item.getFormItem().getType() != EMBEDDED_GROUP_APPLICATION) {
3005+
continue;
3006+
}
3007+
// Generate options for EMBEDDED_GROUP_APPLICATION items.
3008+
setGroupsToCheckBoxForGroups(sess, item, user, vo, group);
3009+
// If the item has no options for the user to offer (bcs user is already member in all possible options,
3010+
// remove it from the form completely
3011+
if (StringUtils.isBlank(item.getFormItem().getI18n().get(ApplicationFormItem.EN).getOptions())) {
3012+
it.remove();
3013+
}
3014+
}
30033015

30043016
// return prefilled form
30053017
return itemsWithValues;
3006-
30073018
}
30083019

30093020
private List<Pair<String, String>> getPrincipalsReservedLogins(PerunSession sess) {
@@ -3022,16 +3033,16 @@ private List<Pair<String, String>> getPrincipalsReservedLogins(PerunSession sess
30223033

30233034
/**
30243035
* To the given EMBEDDED_GROUP_APPLICATION item, sets options of allowed groups.
3025-
*
30263036
* Example format:
30273037
* 111#GroupA|222#GroupB
30283038
*
3029-
* @param sess session
3030-
* @param item item, to which the group options will be set, only EMBEDDED_GROUP_APPLICATION is supported
3031-
* @param vo vo, from which the groups for auto registration are taken
3039+
* @param sess session
3040+
* @param item item, to which the group options will be set, only EMBEDDED_GROUP_APPLICATION is supported
3041+
* @param user
3042+
* @param vo vo, from which the groups for auto registration are taken
30323043
* @param registrationGroup group, from which the subgroups for auto registration are taken - if not null, then it is a group application
30333044
*/
3034-
private void setGroupsToCheckBoxForGroups(PerunSession sess, ApplicationFormItemWithPrefilledValue item, Vo vo, Group registrationGroup) {
3045+
private void setGroupsToCheckBoxForGroups(PerunSession sess, ApplicationFormItemWithPrefilledValue item, User user, Vo vo, Group registrationGroup) {
30353046
if (item.getFormItem().getType() != EMBEDDED_GROUP_APPLICATION) {
30363047
throw new InternalErrorException("Group options can be set only to the EMBEDDED_GROUP_APPLICATION item.");
30373048
}
@@ -3042,10 +3053,18 @@ private void setGroupsToCheckBoxForGroups(PerunSession sess, ApplicationFormItem
30423053
groups = perun.getGroupsManagerBl().getGroupsForAutoRegistration(sess, vo, item.getFormItem());
30433054
}
30443055

3045-
String groupOptions = groups.stream()
3046-
.sorted(Comparator.comparing(Group::getName))
3047-
.map(group -> group.getId() + "#" + group.getName())
3048-
.collect(Collectors.joining("|"));
3056+
if (user != null) {
3057+
List<Group> userGroups = groupsManager.getGroupsWhereUserIsActiveMember(sess, user, vo);
3058+
groups = groups.stream().filter(g -> !userGroups.contains(g)).collect(Collectors.toList());
3059+
}
3060+
3061+
String groupOptions = null;
3062+
if (!groups.isEmpty()) {
3063+
groupOptions = groups.stream()
3064+
.sorted(Comparator.comparing(Group::getName))
3065+
.map(group -> group.getId() + "#" + group.getName())
3066+
.collect(Collectors.joining("|"));
3067+
}
30493068

30503069
if (ApplicationFormItem.CS != null) {
30513070
item.getFormItem().getI18n().get(ApplicationFormItem.CS).setOptions(groupOptions);

0 commit comments

Comments
 (0)