Tuesday 2 June 2015

Role Operation in OIM


Below code will add role to user, Revoke role from user, Fetch Role from user
======================================================================
For Client Code below JAR need to be added in Eclipse or JDeveloper.

1) commons-logging.jar
2) jrf-api.jar
3) oimclient.jar
4) spring.jar
5) wlfullclient.jar
==============================================================

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.security.auth.login.LoginException;
import oracle.iam.platform.OIMClient;
import oracle.iam.platform.authz.exception.AccessDeniedException;
import oracle.iam.identity.exception.RoleGrantException;
import oracle.iam.identity.exception.RoleGrantRevokeException;
import oracle.iam.identity.exception.UserMembershipException;
import oracle.iam.identity.exception.ValidationFailedException;
import oracle.iam.identity.rolemgmt.api.RoleManager;
import oracle.iam.identity.rolemgmt.vo.RoleManagerResult;

public class AddRole {
public static OIMClient oimClient;

public void OIMConnection(){

String oimInitialContextFactory = "weblogic.jndi.WLInitialContextFactory";
java.util.Hashtable<String, String> env = new java.util.Hashtable<String, String>();
env.put(oracle.iam.platform.OIMClient.JAVA_NAMING_FACTORY_INITIAL,oimInitialContextFactory);
//Provide the OIM t3 URL below
env.put(oracle.iam.platform.OIMClient.JAVA_NAMING_PROVIDER_URL, "t3://localhost:14000");
//provide the authwl.conf file path from your local system. Copy the authwl.conf from designconsole\config to your local system
System.setProperty("java.security.auth.login.config", "C:\\Software\\designconsole\\designconsole\\config\\authwl.conf");
System.setProperty("OIM.AppServerType", "wls");        
   System.setProperty("APPSERVER_TYPE", "wls");
   oimClient = new OIMClient(env);
   try {
//Provide the OIM admin Username and Password
oimClient.login("xelsysadm","Welcome123".toCharArray());
System.out.println("Connected Successfully");
} catch (LoginException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void addRole(){

//User Key value
Long userKey=(long) 13597;
String ukey=String.valueOf(userKey);//
Set hset=new HashSet();
hset.add(ukey);
RoleManager rolemgr=oimClient.getService(RoleManager.class);
//Role Key value
String roleKey="22";

    List roles=new ArrayList();//For storing the fetched roles

try {
RoleManagerResult rs=null;
//Adding the Role to user
rs=rolemgr.grantRole(roleKey,hset);
   System.out.println("Role has been added");
 //Revoking the Role from user-->Uncomment if want to revoke
//rs=rolemgr.revokeRoleGrant(roleKey, hset);
System.out.println("Role has been revoked");
//fetching the roles-->Uncomment if want to fetch roles details from user
//roles= rolemgr.getUserMemberships(ukey, true);
System.out.println("Role List is:::::"+"\n"+roles);
}  catch (AccessDeniedException | ValidationFailedException | RoleGrantException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args){
AddRole ar=new AddRole();
ar.OIMConnection();
ar.addRole();
}
}

No comments:

Post a Comment