Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilgrosso/3d56e9c57db283bfaef0068bdbc9b697 to your computer and use it in GitHub Desktop.
Save ilgrosso/3d56e9c57db283bfaef0068bdbc9b697 to your computer and use it in GitHub Desktop.
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.list.SetUniqueList;
// Given this user...
UserTO userTO = ...
List<String> groupKeys = SetUniqueList.setUniqueList(new ArrayList<String>());
// First statically assigned groups...
CollectionUtils.collect(userTO.getMemberships(), new Transformer<MembershipTO, String>() {
@Override
public String transform(final MembershipTO input) {
return input.getGroupKey();
}
}, groupKeys);
// ...then dinamically assigned groups
groupKeys.addAll(userTO.getDynGroups());
if (!groupKeys.isEmpty()) {
String fiql;
if (groupKeys.size() == 1) {
fiql = SyncopeClient.getGroupSearchConditionBuilder().
is("key").
equalTo(groupKeys.get(0)).
query();
} else {
fiql = SyncopeClient.getGroupSearchConditionBuilder().
is("key").
equalTo(groupKeys.get(0),
groupKeys.subList(1, groupKeys.size() - 1).toArray(new String[groupKeys.size() - 1])).
query();
}
// without page / size parameters in the AnyQuery instance above, this will only
// return the first 25 groups at most
PagedResult<GroupTO> result = syncopeClient.getService(GroupService.class).search(
new AnyQuery.Builder().
realm(SyncopeConstants.ROOT_REALM).
fiql(fiql).
build());
List<GroupTO> groups = result.getResult();
}
@ilgrosso
Copy link
Author

ilgrosso commented Jul 15, 2016

This is reported working with Apache Syncope 2.0.0-M4; for later versions the code above simply becomes

String userKey = ...; // user key as UUID
PagedResult<GroupTO> groups = groupService.search(
                new AnyQuery.Builder().
                realm(SyncopeConstants.ROOT_REALM).
                fiql(SyncopeClient.getGroupSearchConditionBuilder().withMembers(userKey).query()).
                build());
List<GroupTO> groups = result.getResult();

@gonzalad
Copy link

Hi Francesco, thanks very much for the sample !

Just a minor glitch in https://gist.github.com/ilgrosso/3d56e9c57db283bfaef0068bdbc9b697#file-apache-syncope-get-all-groups-for-user-java-L33, should be :
fiql = SyncopeClient.getGroupSearchConditionBuilder().is("key").equalTo(groupKeys.get(0),
groupKeys.subList(1, groupKeys.size()).toArray(new String[groupKeys.size() - 1])).query();

@ilgrosso
Copy link
Author

@gonzalad fixed, thanks for reporting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment