Skip to content

Instantly share code, notes, and snippets.

@neilherbertuk
Last active December 16, 2022 10:15
Show Gist options
  • Save neilherbertuk/ddf799e0b10048a1db4c5ff3de02e3d5 to your computer and use it in GitHub Desktop.
Save neilherbertuk/ddf799e0b10048a1db4c5ff3de02e3d5 to your computer and use it in GitHub Desktop.
Find a user by First and Last name within OpenIAM Groovy Script
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, 1) // Search Bean, UserCollection[], from, size
log.info("Found: ${users?.size() ?: 0}")
if (users.size() > 0) {
User user = users.first() // Docs - https://download.openiam.com/release/enterprise/4.2.1.2/javadoc/org/openiam/idm/srvc/user/dto/User.html
log.info("${user.getId()} - First Name: ${user.getFirstName()}")
log.info("${user.getId()} - Last Name: ${user.getLastName()}")
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