Share all details about your problem, including any error messages you may have received.
I’m trying to create a rule that will take the input of any “role name” and return the list of users that would qualify for that role. The rule needs to be generic enough so that it can accept the identitySelector or MatchExpression and return that list of users.
Something along the lines of this (even though this isn’t working):
Is something like this possible? Does anyone have a working example?
If possible, it will also need to expand to include the other role assignment criteria (Match List, Filter, Script, Rule, Population), where it accepts the criteria regardless of type, and searches for users that match the criteria.
Firstly retrieve the bundle object , if the bundle exists and the type of role is business then retrieve the selector from the bundle object , as only business roles have assignment rules option by default.
From the selector object , retrieve the MatchExpression and from the MatchExpression retrieve the MatchTerms.
From the list of MatchTerms , retrieve the filter criteria used for assigning the corresponding role and create a QueryOptions object with the Filters in MatchTerms.
And then use this QueryOptions object to get the identities which match the assignment rule of the bundle.
Try this approach and let me know if it is working.
I put this together and tested it successfully in my local environment with one of my roles. Either way, this should give you an idea of something to build on.
Just a heads up, this approach could have performance issues with large user populations since it runs at O(n) complexity.
public List getUsersForRole(String roleName) {
List qualifyingUsers = new ArrayList();
// Get the bundle/role
Bundle bundle = context.getObjectByName(Bundle.class, roleName);
if (bundle == null) {
log.warn("Bundle not found: " + roleName);
return qualifyingUsers;
}
// Get the selector
IdentitySelector selector = bundle.getSelector();
if (selector == null) {
log.warn("No selector found for bundle: " + roleName);
return qualifyingUsers;
}
// Get the match expression
IdentitySelector.MatchExpression matchExpr = selector.getMatchExpression();
if (matchExpr == null) {
log.warn("No match expression found for bundle: " + roleName);
return qualifyingUsers;
}
// Build filter from MatchExpression
Filter filter = buildFilterFromMatchExpression(matchExpr);
// Build query options
QueryOptions qo = new QueryOptions();
qo.addFilter(filter);
// Search for matching identities
Iterator it = context.search(Identity.class, qo);
try {
while (it != null && it.hasNext()) {
Identity identity = it.next();
if (identity != null) {
qualifyingUsers.add(identity);
}
}
} finally {
Util.flushIterator(it);
}
return qualifyingUsers;
}
private Filter buildFilterFromMatchExpression(IdentitySelector.MatchExpression matchExpr) {
List terms = matchExpr.getTerms();
boolean isAnd = matchExpr.isAnd();
if (terms == null || terms.isEmpty()) {
return null;
}
List filters = new ArrayList();
for (IdentitySelector.MatchTerm term : terms) {
String attributeName = term.getName();
Object value = term.getValue();
Filter f = Filter.eq(attributeName, value);
filters.add(f);
}
// Combine filters based on AND/OR logic
if (filters.size() == 1) {
return filters.get(0);
}
if (isAnd) {
return Filter.and(filters);
} else {
return Filter.or(filters);
}
}
return getUsersForRole("IT Staff"); // Enter the role you want to return a list of users for
Try starting small out small and verify that what I provided works. Import this file and change the role name to something in your system. You can run this rule in Debug and it should work. Pasted below and attached for convenience.
When trying to use the rule with a Filter as an assignment rule, the rule errors since it’s returning a compoundFilter instead of a filter (is my guess).
private List getUsersByFilter(CompoundFilter filter) {
List qualifyingUsers = new ArrayList();
if (filter == null) {
return qualifyingUsers;
}
QueryOptions qo = new QueryOptions();
qo.addFilter(filter.getFilter()); // Need to get the filter from the object
Iterator it = context.search(Identity.class, qo);
try {
while (it != null && it.hasNext()) {
Identity identity = it.next();
if (identity != null) {
qualifyingUsers.add(identity);
}
}
} finally {
Util.flushIterator(it);
}
return qualifyingUsers;
}
This has an overloaded method to support CompoundFilter in case you didn’t know where to include it. Not sure what else you are looking for since you earlier marked this solved.