Class IdentityUtils
Provides methods to retrieve user information, managers, and users by memberships without depending on BDM objects, ensuring functional independence and portability.
- Since:
- 1.0
- Version:
- 2.0.0 - Fixed membership handling for group-only or role-only memberships
- Author:
- Bonitasoft
-
Method Summary
Modifier and TypeMethodDescriptionbuildCandidateUsers(Long stepUserId, boolean includeManager, List<?> membershipList, org.bonitasoft.engine.api.IdentityAPI identityAPI) Builds a set of candidate user IDs for task assignation based on step user, manager, and memberships.extractFieldFromJson(String jsonString, String fieldName, org.slf4j.Logger logger) Extracts a field value from a JSON string.extractUserIdsFromObjects(List<?> userObjects, String userIdMethodName) Extracts user IDs from a list of BDM user objects.filterAssignableUsers(Set<Long> candidateUserIds, Collection<Long> assignableUserIds) Filters assignable users based on candidate user IDs.static <T> TfindMostRecentInstance(Supplier<List<T>> instanceSupplier, String typeName, org.slf4j.Logger logger) Finds the most recent instance from a supplier result.getFilteredAssignableUsers(Long stepUserId, boolean includeManager, List<?> membershipList, Collection<Long> assignableUserIds, org.bonitasoft.engine.api.IdentityAPI identityAPI) Convenience method that combines building candidate users and filtering in one call.static Optional<com.fasterxml.jackson.databind.JsonNode>getJsonNode(com.fasterxml.jackson.databind.JsonNode parentNode, String nodeKey, org.slf4j.Logger logger) Extracts a child node from a parent JsonNode.getNodeArrayAsStringList(com.fasterxml.jackson.databind.JsonNode arrayNode, org.slf4j.Logger logger) Extracts a list of strings from a JsonNode array.getNodeText(com.fasterxml.jackson.databind.JsonNode node, org.slf4j.Logger logger) Extracts a non-empty text value from a JsonNode.static org.bonitasoft.engine.identity.UserRetrieves a Bonita User object by its ID.static LonggetUserIdFromObject(Object bdmObject, String methodName) Extracts a user ID from a BDM object using reflection.static LonggetUserManager(Long userId, org.bonitasoft.engine.api.IdentityAPI identityAPI) Gets the manager ID of a given user.static UserRecordgetUserRecord(Long userId, org.bonitasoft.engine.api.IdentityAPI identityAPI) Retrieves a UserRecord by user ID.getUsersByMemberships(Supplier<List<T>> membershipDataSupplier, Function<T, Long> groupIdExtractor, Function<T, Long> roleIdExtractor, org.bonitasoft.engine.api.IdentityAPI identityAPI, org.slf4j.Logger logger) Gets user IDs from memberships using a supplier for data access.getUsersByMemberships(List<?> membershipList, org.bonitasoft.engine.api.IdentityAPI identityAPI) Gets all users matching the given memberships (groups/roles).getUsersForMembership(Long groupId, Long roleId, org.bonitasoft.engine.api.IdentityAPI identityAPI, org.slf4j.Logger logger) Gets user IDs for a specific group/role combination.static Optional<com.fasterxml.jackson.databind.JsonNode>Parses a JSON string and returns the root JsonNode.static Optional<com.fasterxml.jackson.databind.JsonNode>parseJsonAndGetNode(String jsonContent, String nodeKey, org.slf4j.Logger logger) Parses JSON and extracts a specific node by key.
-
Method Details
-
getUser
public static org.bonitasoft.engine.identity.User getUser(Long userId, org.bonitasoft.engine.api.IdentityAPI identityAPI) Retrieves a Bonita User object by its ID.- Parameters:
userId- The ID of the user to retrieveidentityAPI- The Bonita Identity API instance- Returns:
- The User object, or
nullif not found, invalid ID, or on error
-
getUserRecord
public static UserRecord getUserRecord(Long userId, org.bonitasoft.engine.api.IdentityAPI identityAPI) Retrieves a UserRecord by user ID.Converts the Bonita User object into a lightweight
UserRecordcontaining the essential fields: id, userName, fullName, firstName, lastName, and email. The email is retrieved from the user's professional contact data.- Parameters:
userId- The ID of the user to retrieveidentityAPI- The Bonita Identity API instance- Returns:
- A
UserRecordwith user data, ornullif user not found or on error
-
getUserManager
Gets the manager ID of a given user.- Parameters:
userId- The ID of the user whose manager is to be retrievedidentityAPI- The Bonita Identity API instance- Returns:
- The manager's user ID, or
nullif not found or on error
-
getUsersByMemberships
public static Set<Long> getUsersByMemberships(List<?> membershipList, org.bonitasoft.engine.api.IdentityAPI identityAPI) Gets all users matching the given memberships (groups/roles).This method accepts a list of objects that contain group and role information. Each object must have
getGroupId()andgetRoleId()methods.IMPORTANT: This method correctly handles memberships that have:
- Both groupId AND roleId defined
- Only groupId defined (roleId is null or 0)
- Only roleId defined (groupId is null or 0)
- Parameters:
membershipList- List of objects containing group and role IDsidentityAPI- The Bonita Identity API instance- Returns:
- Set of user IDs matching the memberships, empty set if no users found
-
getUserIdFromObject
Extracts a user ID from a BDM object using reflection.This method attempts to call a getter method on the provided object to extract a user ID. It supports various method names commonly used in BDM objects such as
getUserId(),getStepUser(), or any custom getter name.The method handles null objects, null return values, and invalid ID values (0 or negative).
- Parameters:
bdmObject- The BDM object from which to extract the user ID (can be null)methodName- The name of the getter method to invoke (e.g., "getUserId", "getStepUser")- Returns:
- The user ID if valid (greater than 0), or
nullif object is null, method not found, or ID is invalid
-
buildCandidateUsers
public static Set<Long> buildCandidateUsers(Long stepUserId, boolean includeManager, List<?> membershipList, org.bonitasoft.engine.api.IdentityAPI identityAPI) Builds a set of candidate user IDs for task assignation based on step user, manager, and memberships.This method implements the common actor filter logic:
- If stepUserId is valid, add it to the candidates
- If includeManager is true and stepUserId has a manager, add the manager ID
- If membershipList is provided, add all users from those memberships
This method is designed to be used by Groovy scripts to reduce code duplication when implementing actor filters.
- Parameters:
stepUserId- The user ID from a previous step (can be null)includeManager- Whether to include the step user's managermembershipList- List of membership objects with getGroupId/getRoleId methods (can be null)identityAPI- The Bonita Identity API instance- Returns:
- A set of candidate user IDs (never null, may be empty)
-
filterAssignableUsers
public static Set<Long> filterAssignableUsers(Set<Long> candidateUserIds, Collection<Long> assignableUserIds) Filters assignable users based on candidate user IDs.This method takes a set of candidate user IDs and returns only those that are present in the provided collection of assignable user IDs. This is the final step in actor filter logic where candidates are intersected with users who are actually assignable to the task.
- Parameters:
candidateUserIds- Set of candidate user IDs to filterassignableUserIds- Collection of user IDs that are assignable to the task- Returns:
- A set containing only the user IDs that are both candidates AND assignable
-
getFilteredAssignableUsers
public static Set<Long> getFilteredAssignableUsers(Long stepUserId, boolean includeManager, List<?> membershipList, Collection<Long> assignableUserIds, org.bonitasoft.engine.api.IdentityAPI identityAPI) Convenience method that combines building candidate users and filtering in one call.This method performs the complete actor filter logic:
- Builds the candidate user set from step user, manager, and memberships
- Filters the candidates against the assignable users
Example usage in Groovy:
Set<Long> filteredUsers = IdentityUtils.getFilteredAssignableUsers( stepUserId, true, // include manager membershipList, assignableUserIds, identityAPI ) return filteredUsers.toList()- Parameters:
stepUserId- The user ID from a previous step (can be null)includeManager- Whether to include the step user's managermembershipList- List of membership objects with getGroupId/getRoleId methods (can be null)assignableUserIds- Collection of user IDs that are assignable to the taskidentityAPI- The Bonita Identity API instance- Returns:
- A set of user IDs that are both candidates AND assignable (never null)
-
extractUserIdsFromObjects
Extracts user IDs from a list of BDM user objects.This method iterates over a list of BDM objects and extracts user IDs using the specified getter method. It filters out null and invalid IDs (0 or negative).
This is useful for extracting user IDs from BDM objects like PBUserList where each object has a user ID field.
- Parameters:
userObjects- List of BDM objects containing user IDsuserIdMethodName- The getter method name to extract user ID (e.g., "getUserId")- Returns:
- A set of valid user IDs extracted from the objects (never null)
-
parseJson
public static Optional<com.fasterxml.jackson.databind.JsonNode> parseJson(String jsonContent, org.slf4j.Logger logger) Parses a JSON string and returns the root JsonNode.- Parameters:
jsonContent- JSON string to parselogger- Logger for error reporting (nullable)- Returns:
- Optional containing the root JsonNode, or empty if parsing fails
-
parseJsonAndGetNode
public static Optional<com.fasterxml.jackson.databind.JsonNode> parseJsonAndGetNode(String jsonContent, String nodeKey, org.slf4j.Logger logger) Parses JSON and extracts a specific node by key.- Parameters:
jsonContent- JSON string to parsenodeKey- Key of the node to extractlogger- Logger for error reporting (nullable)- Returns:
- Optional containing the extracted JsonNode, or empty if not found
-
getJsonNode
public static Optional<com.fasterxml.jackson.databind.JsonNode> getJsonNode(com.fasterxml.jackson.databind.JsonNode parentNode, String nodeKey, org.slf4j.Logger logger) Extracts a child node from a parent JsonNode.- Parameters:
parentNode- Parent node to search innodeKey- Key of the child nodelogger- Logger for error reporting (nullable)- Returns:
- Optional containing the child JsonNode, or empty if not found
-
getNodeText
public static Optional<String> getNodeText(com.fasterxml.jackson.databind.JsonNode node, org.slf4j.Logger logger) Extracts a non-empty text value from a JsonNode.- Parameters:
node- JsonNode to extract text fromlogger- Logger for debug reporting (nullable)- Returns:
- Optional containing the trimmed text, or empty if null/blank
-
getNodeArrayAsStringList
public static List<String> getNodeArrayAsStringList(com.fasterxml.jackson.databind.JsonNode arrayNode, org.slf4j.Logger logger) Extracts a list of strings from a JsonNode array.- Parameters:
arrayNode- JsonNode that should be an arraylogger- Logger for error reporting (nullable)- Returns:
- List of strings (empty if node is not an array or is empty)
-
getUsersByMemberships
public static <T> Set<Long> getUsersByMemberships(Supplier<List<T>> membershipDataSupplier, Function<T, Long> groupIdExtractor, Function<T, Long> roleIdExtractor, org.bonitasoft.engine.api.IdentityAPI identityAPI, org.slf4j.Logger logger) Gets user IDs from memberships using a supplier for data access. The supplier should return a collection of objects that have group/role information.- Type Parameters:
T- Type of membership data object- Parameters:
membershipDataSupplier- Supplier that provides membership data objectsgroupIdExtractor- Function to extract group ID from membership objectroleIdExtractor- Function to extract role ID from membership objectidentityAPI- Bonita Identity APIlogger- Logger for reporting (nullable)- Returns:
- Set of user IDs (empty if none found)
-
getUsersForMembership
public static Set<Long> getUsersForMembership(Long groupId, Long roleId, org.bonitasoft.engine.api.IdentityAPI identityAPI, org.slf4j.Logger logger) Gets user IDs for a specific group/role combination. Handles three cases: group+role, group-only, role-only.- Parameters:
groupId- Group ID (nullable)roleId- Role ID (nullable)identityAPI- Bonita Identity APIlogger- Logger for reporting (nullable)- Returns:
- Set of user IDs matching the membership criteria
-
findMostRecentInstance
public static <T> T findMostRecentInstance(Supplier<List<T>> instanceSupplier, String typeName, org.slf4j.Logger logger) Finds the most recent instance from a supplier result. Generic method that works with any step instance type.- Type Parameters:
T- Type of the step instance- Parameters:
instanceSupplier- Supplier that executes the DAO query and returns a listtypeName- Name of the type for logging purposeslogger- Logger for reporting (nullable)- Returns:
- The first (most recent) instance, or null if none found
-
extractFieldFromJson
public static Optional<String> extractFieldFromJson(String jsonString, String fieldName, org.slf4j.Logger logger) Extracts a field value from a JSON string.- Parameters:
jsonString- JSON string to parsefieldName- Field name to extractlogger- Logger for reporting (nullable)- Returns:
- Optional containing the field value as string, or empty if not found
-