Skip to content

Instantly share code, notes, and snippets.

@neilherbertuk
Last active December 16, 2022 10:16
Show Gist options
  • Save neilherbertuk/1356f7dda7f2e805487655b9f6a89677 to your computer and use it in GitHub Desktop.
Save neilherbertuk/1356f7dda7f2e805487655b9f6a89677 to your computer and use it in GitHub Desktop.
Find multiple users by First and Last name within OpenIAM
import java.util.List
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.openiam.base.ws.MatchType
import org.openiam.base.ws.SearchParam
import org.openiam.common.beans.mq.UserRabbitMQService
import org.openiam.idm.searchbeans.UserSearchBean
import org.openiam.idm.srvc.user.dto.User
import org.openiam.idm.srvc.user.dto.UserCollection
import org.openiam.idm.srvc.user.dto.UserStatusEnum
import org.springframework.context.ApplicationContext
// Dependencies
Log log = LogFactory.getLog("findUserTest") // Logging
ApplicationContext appContext = (ApplicationContext) context // IoC Container
UserRabbitMQService userRabbitMQService = appContext.getBean(UserRabbitMQService.class) // Docs - https://download.openiam.com/release/enterprise/4.2.1.2/javadoc/org/openiam/common/beans/mq/UserRabbitMQService.html
log.info("Finding Scott Nelson")
UserSearchBean usb = new UserSearchBean() // Docs - https://download.openiam.com/release/enterprise/4.2.1.2/javadoc/org/openiam/idm/searchbeans/UserSearchBean.html
usb.addFirstNameMatchToken(new SearchParam("Scott", MatchType.EXACT)) // Add Scott as the first name to look for
usb.addLastNameMatchToken(new SearchParam("Nelson", MatchType.EXACT)) // Add Nelson as the last name to look for
log.info("Performing search")
// Build a list of attributes you want returned for the user - Docs - https://download.openiam.com/release/enterprise/4.2.1.2/javadoc/org/openiam/idm/srvc/user/dto/UserCollection.html
List<UserCollection> userCollection = [UserCollection.PRINCIPALS, UserCollection.ATTRIBUTES, UserCollection.ORGANIZATIONS, UserCollection.EMAILS, UserCollection.SUPERVISORS]
List<User> users = userRabbitMQService.findBeans(usb, userCollection as UserCollection[], 0, 50) // Search Bean, UserCollection[], from, size
log.info("Found: ${users?.size() ?: 0}")
if (users.size() > 0) {
for (User user : users) {
log.info("${user.getId()} - First Name: ${user.getFirstName()}")
log.info("${user.getId()} - Last Name: ${user.getLastName()}")
log.info("${user.getId()} - Job Title: ${user.getTitle()}")
log.info("${user.getId()} - Email Address: ${user.emailAddresses?.find({ a -> a.mdTypeId = 'PRIMARY_EMAIL' }).emailAddress ?: 'Not found'}")
log.info("${user}")
}
} else {
log.info("Could not find user")
}
output = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment