Saturday 16 June 2018

OIM API - Code Snippet to Assign Admin Role to User.

public void assignAdminRoleToUser(String adminRoleName, String userLogin, String orgName) {
        //initialize admin role service
        AdminRoleService adminRoleSvc = Platform.getService(AdminRoleService.class);
       
        //get admin role object by admin role name
        AdminRole adminRole = getAdminRoleByName(adminRoleName);
        if(null == adminRole){
            System.out.println("Admin role <"+adminRole+"> is not present");
            return;
        }
       
        //get organization key by organization name
        String orgKey = getOrganizationID(orgName);
        if(null == orgKey){
            System.out.println("Organization <"+orgName+"> is not present");
            return;
        }
       
        //get user key by user login
        String usrKey = getUserKeyByUserLogin(userLogin);
        if(null == usrKey){
            System.out.println("User <"+userLogin+"> is not present");
            return;
        }
       
        //Create a new membership object
        AdminRoleMembership membership = new AdminRoleMembership();
        membership.setAdminRole(adminRole);
        membership.setUserId(usrKey);
        membership.setScopeId(orgKey);
        membership.setHierarchicalScope(true);
       
        adminRoleSvc.addAdminRoleMembership(membership); 
        System.out.println("Admin Role Successfully Assigned to the User: " + userLogin);
}


private AdminRole getAdminRoleByName(String adminRoleName) {
        //initialize admin role service
        AdminRoleService adminRoleSvc = Platform.getService(AdminRoleService.class);
       
        List<AdminRole> adminRoles = adminRoleSvc.getScopedAdminRoles();
        Iterator adminRolesIter = adminRoles.iterator();
        while (adminRolesIter.hasNext()) {
            AdminRole adminRole = (AdminRole)adminRolesIter.next();
          if (adminRole.getRoleName().equals(adminRoleName)) {
              return adminRole;
          }
        }
        return null;
}
 
private String getOrganizationID(String orgName) {
        OrganizationManager orgManager = Platform.getService(OrganizationManager.class);
        Organization org;
        try{
          org = orgManager.getDetails(orgName, null, true);
          return org.getEntityId();
        } catch (OrganizationManagerException e) {
            System.out.println("Exception occured while fetching org key");
            return null;
        }
}
   
public String getUserKeyByUserLogin(String userLogin){
        HashSet<String> attrsToFetch = new HashSet<String>();
        attrsToFetch.add(UserManagerConstants.AttributeName.USER_KEY.getId());
   
        try{    
            //get user manager service
            UserManager userService = Platform.getService(UserManager.class);
          
            User user = userService.getDetails(userLogin, attrsToFetch, true);
            return user.getEntityId();
        }catch(Exception e){
            System.out.println("Exception occured while fetching user key");
            return null;
        }
}


Happy Learning!!!

No comments:

Post a Comment