Populating a manager

In the event of requiring

  • a manager and Direct reports to be populated;
  • managerID to be populated in the custom field ManagerID in the User Info. upon syncing users into OpenIAM.

To add the respective attribute add the following code.

def addAttribute(ProvisionUser pUser, Attribute attr) {
if (attr?.name) {
def userAttr = new UserAttribute(attr.name, attr.value)
userAttr.operation = AttributeOperationEnum.ADD
if (!isNewUser) {
for (String name : pUser.userAttributes.keySet()) {
if (name.equalsIgnoreCase(attr.name)) {
pUser.userAttributes.remove(name)
userAttr.operation = AttributeOperationEnum.REPLACE
break
}
}
}
pUser.userAttributes.put(attr.name, userAttr)
println("Attribute '" + attr.name + "' added to the user object.")
}
}

To add the supervisor use the following code in the transformation script.

def addSupervisor(ProvisionUser pUser, String managerPrincipal, String metadataTypeName) {
UserSearchBean usb = new UserSearchBean()
LoginSearchBean sb = new LoginSearchBean()
sb.setLoginMatchToken(new SearchParam(managerPrincipal, MatchType.EXACT))
usb.setPrincipal(sb)
List<User> userList = userManager.findBeans(usb, null, 0, 2)
if (userList && userList.size() > 1) {
println "found more the one supervisor by managerPrincipal, it's wrong, stop adding supervisor"
return
}
MetadataType metadataType = null
if (metadataTypeName) {
metadataType = getMetadataTypeByNameAndGrouping(metadataTypeName, MetadataTypeGrouping.SUPERVISOR_TYPE)
if (!metadataType) {
println("Cannot find metadata type with name: ${metadataTypeName}")
return
}
}
println("(staff) original user: ${pUser.firstName}.${pUser.lastName}")
if (CollectionUtils.isNotEmpty(userList) && userList.get(0) != null) {
User user = userList.get(0)
if (isNewUser || CollectionUtils.isEmpty(pUser.getSupervisors())) {
println("staff is new user and doesn't have managers yet")
/*MetadataType type = new MetadataType()
type.setId(supervisorMetadataTypeId)*/
pUser.addSupervisor(user, metadataType)
} else {
pUser.getSupervisors().each {
if (it.getType() == null || it.getType().getId() == null) {
it.setOperation(AttributeOperationEnum.DELETE)
}
}
// println("! => staff is new user and doesn't have managers yet")
Supervisor sp = pUser.getSupervisors().find { it ->
// println(" manager is " + it.getId()?.getSupervisor()?.getDisplayName())
// println(" manager type is " + it.getType()?.getId())
it.getType()?.getId() == metadataType?.getId()
}
println("Supervisor found: ${sp}")
if (sp == null || sp.getId()?.getSupervisor()?.getId() != user.getId()) {
if (sp && sp.getType()?.getId() == metadataType?.getId()) {
println("delete current manager")
sp.setOperation(AttributeOperationEnum.DELETE)
}
println("add manager " + user.getDisplayName() + " " + metadataType.name)
pUser.addSupervisor(user, metadataType)
} else {
println("type: ${sp.getType()?.toString()}")
println("typeID: ${sp.getType()?.getId()}")
if (sp.getType() == null || sp.getType().getId() == null) {
sp.setType(metadataType)
sp.setOperation(AttributeOperationEnum.ADD)
}
}
}
} else {
println("can't find supervisors")
}
}t