To execute below code you have to add following jars in classpath:
- commons-logging.jar
- eclipselink.jar
- jrf-api.jar
- oimclient.jar
- spring.jar
- wlfullclient.jar
Stand Alone Code:
package com.oimacademy.users;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import javax.security.auth.login.LoginException;
import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.usermgmt.api.UserManagerConstants;
import oracle.iam.identity.usermgmt.vo.User;
import oracle.iam.platform.OIMClient;
import oracle.iam.platform.entitymgr.vo.SearchCriteria;
import oracle.iam.provisioning.api.ApplicationInstanceService;
import oracle.iam.provisioning.api.ProvisioningService;
import oracle.iam.provisioning.vo.Account;
import oracle.iam.provisioning.vo.AccountData;
import oracle.iam.provisioning.vo.ApplicationInstance;
import oracle.iam.provisioning.vo.FormField;
import oracle.iam.provisioning.vo.FormInfo;
public class ProvisionAccountToUser {
OIMClient client = null;
//identity self service details
String username = "xelsysadm";
String password = "<password>"; //xelsysadm password
String t3url = "t3://<hostname>:<port>"; //OIM HostName and Port
String authwl_location = "<location of authwl.conf file in your local machine>";
public void getOIMConnection(){
System.out.println("getOIMConnection() : Start");
//set system properties
System.setProperty("java.security.auth.login.config", authwl_location);
System.setProperty("OIM.AppServerType", "wls");
System.setProperty("APPSERVER_TYPE", "wls");
Hashtable oimenv = new Hashtable();
oimenv.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, "weblogic.jndi.WLInitialContextFactory");
oimenv.put(OIMClient.JAVA_NAMING_PROVIDER_URL,t3url);
client = new OIMClient(oimenv);
try {
client.login(username, password.toCharArray());
System.out.println("Connected Successfully...");
} catch (LoginException e) {
e.printStackTrace();
}
System.out.println("getOIMConnection() : End");
}
public void provisionAccount(String appInsName, String userId) throws Exception{
ProvisioningService service= client.getService(ProvisioningService.class);
ApplicationInstance appInstance = null;
String usrKey = "";
try {
//get application instance object by application instance name
appInstance = getApplicationInstanceByName(appInsName);
//get user key by user login
usrKey = getUserKey(userId);
//prepare process form
FormInfo formInfo = appInstance.getAccountForm();
FormField f1 = new FormField();
f1.setName("Account Name");
f1.setDefaultValue(userId);
formInfo.addFormField(f1);
String formKey = String.valueOf(formInfo.getFormKey());
AccountData accountData = new AccountData(formKey, null, null);
Account account = new Account(appInstance, accountData);
//set Account type
account.setAccountType(Account.ACCOUNT_TYPE.Primary);
service.provision(usrKey, account);
System.out.println(appInsName + " Provisioned to " + userId);
}catch(Exception e) {
e.printStackTrace();
}
}
public String getUserKey(String userLogin) throws Exception{
String userKey = "" ;
Set<String> retAttrs = new HashSet<String>();
retAttrs.add(UserManagerConstants.AttributeName.USER_KEY.getId());
SearchCriteria criteria1 = new SearchCriteria(UserManagerConstants.AttributeName.USER_LOGIN.getId(), userLogin, SearchCriteria.Operator.EQUAL);
UserManager userService = client.getService(UserManager.class);
List<User> users = userService.search(criteria1, retAttrs, null);
if (!users.isEmpty()) {
for (User user : users) {
userKey = user.getEntityId();
}
}
return userKey;
}
public ApplicationInstance getApplicationInstanceByName(String applicationInstanceName) throws Exception {
ApplicationInstanceService service= client.getService(ApplicationInstanceService.class);
ApplicationInstance appInstance = service.findApplicationInstanceByName(applicationInstanceName);
return appInstance;
}
public static void main(String[] args) throws Exception {
ProvisionAccountToUser provisionAccountToUser = new ProvisionAccountToUser();
provisionAccountToUser.getOIMConnection();
//e.g: provisionAccountToUser.provisionAccount("ServiceNow","Test1");
provisionAccountToUser.provisionAccount("<Application Instance Name>","<User Login>");
}
}
No comments:
Post a Comment