OIM API To Create User and Assign Role


  1. package com.oimacademy.role;
  2. import java.util.Date;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. import oracle.iam.identity.rolemgmt.api.RoleManager;
  7. import oracle.iam.identity.rolemgmt.vo.Role;
  8. import oracle.iam.identity.rolemgmt.vo.RoleManagerResult;
  9. import oracle.iam.identity.usermgmt.api.UserManager;
  10. import oracle.iam.identity.usermgmt.vo.User;
  11. import oracle.iam.identity.usermgmt.vo.UserManagerResult;
  12. import com.oimacademy.connection.Platform; 
  13. public class AssignRole  {  
  14.     /* protected static OrganizationManager m_orgManagerService ; */     /*
  15.       * Initialize the context and login with client supplied environment
  16.      */
  17. public static void main(String [] args)
  18. {
  19.   try {
  20.         System.out.println("Creating client....");
  21.        int j=3;
  22.     String roleName = "loadrole"+j;
  23. RoleManager roleMgr = Platform.getService(RoleManager.class);
  24. RoleManagerResult roleResult = null;
  25. HashMap<String, Object> createAttributes = new HashMap<String, Object>();
  26. createAttributes.put("Role Name", roleName);
  27. createAttributes.put("Role Display Name", roleName);
  28. createAttributes.put("Role Description", roleName + " Description");
  29. Role role = new Role(createAttributes);
  30. roleResult = roleMgr.create(role);
  31. String entityId = roleResult.getEntityId();
  32. System.out.println("Created role with key = " + entityId);
  33. /* Create 50 users with same firstname */
  34.     UserManager userService = Platform.getService(UserManager.class);
  35.   for(int i=0;i<51;i++) {
  36.       String FirstName = "TEST";
  37.       String LastName = "USER"+i;
  38.       System.out.println("User First name is:" +FirstName );
  39.       System.out.println("User Last Name is :" +LastName);
  40.      HashMap<String, Object> userAttributeValueMap = new HashMap<String, Object>();
  41.      userAttributeValueMap.put("act_key", 1L);
  42.      userAttributeValueMap.put("First Name", FirstName);
  43.      userAttributeValueMap.put("Last Name", LastName);
  44.      userAttributeValueMap.put("Role","Full-Time");
  45.      userAttributeValueMap.put("Xellerate Type","End-User");      
  46.      UserManagerResult result = userService.create(new User(null, userAttributeValueMap)); 
  47.      String usr_key = result.getEntityId();
  48.      System.out.println("User key is :" +result.getEntityId());     
  49.      Set userKeys = new HashSet();
  50.      userKeys.add(usr_key);
  51.      System.out.print("Timestamp before calling grantRole :" +new Date());
  52.      roleMgr.grantRole("58811",userKeys);
  53.      System.out.print("Timestamp after calling grantRole :" +new Date());
  54.   }
  55.   }catch (Exception e){
  56.    System.out.println("In exception block");
  57.    e.printStackTrace();
  58.   }
  59.  }
  60. }

1 comment: