Please refer to KP Soft's website at https://kpsoft.digital/ for more information.
Monday, February 13, 2012
OIM 11g Create and disable accounts
/**
* @author VKanigicherla
*
* Utility for bulk loading users into the OIM system Data must be
* exported to an excel spreadsheet with the following attributes in the
* order specified below
* samid dn desc sid upn fn mi ln display office tel email title dept company loscr disabled
*
* In this example we used Active Directory to export user account data
* Ideally, connector reconciliation tasks must be used for importing users, in case you need
* do this manually for whatever reason, the below program could be used.
* Libraries needed are primarily from oim 11g environment and apache POI project for reading excel xml workbooks
*
*/
public class OimReconBulkUserLoad {
// Read the input feed from the excel file
public static String filename = "C:\\kpsoft\\junk\\Users1.xlsx";
public static List sheetData = new ArrayList();
public static FileInputStream fis = null;
public static ArrayList<OimBulkUser> oimUsers = new ArrayList<OimBulkUser>();
public static tcUtilityFactory ioUtilityFactory;
public static String updatedByUserName = "BULKLOADER";
static final Logger logger = Logger.getLogger(OimReconBulkUserLoad.class);
// OIM Env Data
public static String id;
public static OIMClient oimClient;
private static String OIMUserName = "prasad";
private static String OIMPassword = "xxxxxx";
private static final String OIM_PROVIDER_URL = "t3://sandbox-oim.kpsoft.org:14000/oim";
private static String OIMInitialContextFactory = "weblogic.jndi.WLInitialContextFactory";
private static UserManager userMgrSrvc = null;
private static tcOrganizationOperationsIntf orgSrvc = null;
private static String orgName = "Terminations";
public OimReconBulkUserLoad() {
super();
}
/**
* main control method for running the bulk load initiates connection with
* OIM reads the bulk load contents executes API for loading users cleans up
* the connection
*
* @param args
* @throws Exception
*/
public static void main(String args[]) throws Exception {
init();
readBulkLoadInputFile();
loadBulkLoadInputFile();
// printOimUsers();
execBulkLoadInputFile(orgName);
clean();
}
private static void printOimUsers() {
// TODO Auto-generated method stub
LOGGER("Printing OIM Users");
for (int i = 0; i < oimUsers.size(); i++) {
LOGGER(oimUsers.get(i).getSamAccountId());
}
}
/**
* creates a connection to the OIM with the passed in credentials
*
* @throws LoginException
*/
private static void init() throws LoginException {
LOGGER("Creating client....");
Hashtable env = new Hashtable();
env.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, OIMInitialContextFactory);
env.put(OIMClient.JAVA_NAMING_PROVIDER_URL, OIM_PROVIDER_URL);
/**
* Passing environment in constructor disables lookup for environment in
* setup. In any case, we can always enforce manual environment settings
* by OIMClient.setLookupEnv(configEnv) method.
*/
oimClient = new OIMClient(env);
LOGGER("Logging in");
oimClient.login(OIMUserName, OIMPassword);
LOGGER("Log in successful");
// get the user Manager
userMgrSrvc = oimClient.getService(UserManager.class);
orgSrvc = oimClient.getService(tcOrganizationOperationsIntf.class);
}
/**
* parse the input file and iterate through the content
*
* @throws IOException
*/
protected static void readBulkLoadInputFile() throws IOException {
try {
fis = new FileInputStream(filename);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator rows = sheet.rowIterator();
// get rid of the title columns
if (rows.hasNext())
rows.next();
while (rows.hasNext()) {
XSSFRow row = (XSSFRow) rows.next();
Iterator cells = row.cellIterator();
List data = new ArrayList();
while (cells.hasNext()) {
XSSFCell cell = (XSSFCell) cells.next();
data.add(cell);
}
sheetData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
}
/**
* create a arraylist of model user objects
*/
protected static void loadBulkLoadInputFile() {
//
// Iterates the data and print it out to the console.
// TODO - update these things based on teh excel spreadsheet header
//
int col_samid = 0;
int col_dn = 1;
int col_desc = 2;
int col_sid = 3;
int col_upn = 4;
int col_fn = 5;
int col_mi = 6;
int col_ln = 7;
int col_display = 8;
int col_office = 9;
int col_tel = 10;
int col_email = 11;
int col_title = 12;
int col_dept = 13;
int col_company = 14;
int col_loscr = 15;
int col_disabled = 16;
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
OimBulkUser ssoUser = new OimBulkUser();
for (int j = 0; j < list.size(); j++) {
XSSFCell cell = (XSSFCell) list.get(j);
int cellType = cell.getCellType();
int cellColumnIndex = cell.getColumnIndex();
String cellValue = "";
if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
cellValue = Float.toString((float) cell
.getNumericCellValue());
} else if (cellType == HSSFCell.CELL_TYPE_STRING) {
cellValue = cell.getRichStringCellValue().getString();
}
if (cellColumnIndex == col_samid) {
ssoUser.setSamAccountId(cellValue);
} else if (cellColumnIndex == col_dn) {
ssoUser.setDistinguishedName(cellValue);
ssoUser.setOrganization(getOrganizationFromDN(cellValue));
} else if (cellColumnIndex == col_desc) {
ssoUser.setDescription(cellValue);
} else if (cellColumnIndex == col_sid) {
ssoUser.setSid(cellValue);
} else if (cellColumnIndex == col_upn) {
ssoUser.setUpnName(cellValue);
} else if (cellColumnIndex == col_fn) {
ssoUser.setFirstName(cellValue);
} else if (cellColumnIndex == col_mi) {
ssoUser.setMiddleName(cellValue);
} else if (cellColumnIndex == col_ln) {
ssoUser.setLastName(cellValue);
} else if (cellColumnIndex == col_display) {
ssoUser.setDisplayName(cellValue);
} else if (cellColumnIndex == col_office) {
ssoUser.setOffice(cellValue);
} else if (cellColumnIndex == col_tel) {
ssoUser.setTelephoneNumber(cellValue);
} else if (cellColumnIndex == col_email) {
ssoUser.setEmailAddress(cellValue);
} else if (cellColumnIndex == col_title) {
ssoUser.setTitle(cellValue);
} else if (cellColumnIndex == col_dept) {
ssoUser.setDepartmentName(cellValue);
} else if (cellColumnIndex == col_company) {
ssoUser.setCompany(cellValue);
} else if (cellColumnIndex == col_loscr) {
ssoUser.setLogikpsoftript(cellValue);
} else if (cellColumnIndex == col_disabled) {
ssoUser.setDisabledString(cellValue);
}
}
oimUsers.add(ssoUser);
}
}
private static String getOrganizationFromDN(String cellValue) {
// TODO Auto-generated method stub
String orgUnit = null;
try {
LdapName dnName = new LdapName(cellValue);
// get OU from the dn whis is always in teh second place 0
// (dc=org),1 (dc=kpsoft), 2, 3
orgUnit = dnName.get(3);
orgUnit = orgUnit.substring(orgUnit.indexOf('=') + 1);
} catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return orgUnit;
}
/**
* calls the create user API in SsoProvisioningServiceImpl logs the
* exceptions if any
*
* @throws SsoOrganizationException
*/
protected static void execBulkLoadInputFile(String orgName) {
for (OimBulkUser su : oimUsers) {
// do it only for organization specified
if (su.getOrganization().equals(orgName)) {
User blkUser = new User("");
// first name
blkUser.setAttribute("First Name", su.getFirstName());
// last name
blkUser.setAttribute("Last Name", su.getLastName());
// middle name
blkUser.setAttribute("Middle Name", su.getMiddleName());
// physicalDeliveryOfficeName
blkUser.setAttribute("Office", su.getOffice());
// telephoneNumber
blkUser.setAttribute("Telephone Number",
su.getTelephoneNumber());
// givenName
// displayName
blkUser.setAttribute("Display Name", su.getDisplayName());
// sAMAccountName
blkUser.setAttribute("User Login", su.getSamAccountId());
// title
blkUser.setAttribute("Title", su.getDescription());
blkUser.setAttribute("Email", su.getEmailAddress());
// system stuff
blkUser.setAttribute("Xellerate Type", "End-User");
// full time employee
blkUser.setAttribute("Role", "Full-Time Employee");
// get org key
long adUsersOimOrg = 1;
try {
adUsersOimOrg = Long.parseLong(findOimOrganizationKey(su
.getOrganization()));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// set the organization
blkUser.setAttribute("act_key", adUsersOimOrg);
// create the user now
UserManagerResult result;
UserManagerResult disableResult;
try {
result = userMgrSrvc.create(blkUser);
String usrKey = result.getEntityId();
LOGGER("User created in OIM for AD User: "
+ su.getSamAccountId() + " with user id: " + usrKey);
// if the user is disabled in AD export spreadsheet
// disable here as well
if (su.getDisabledString().equalsIgnoreCase("yes")) {
try {
disableResult = userMgrSrvc.disable(usrKey, false);
} catch (UserDisableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchUserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (ValidationFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UserAlreadyExistsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UserCreateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AccessDeniedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static String findOimOrganizationKey(String orgName)
throws Exception {
LOGGER("Entering Method findOrganizationKey");
String organizationKey = null;
try {
LOGGER("Getting utility interfaces...");
tcOrganizationOperationsIntf moOrganizationUtility = (tcOrganizationOperationsIntf) oimClient
.getService(Thor.API.Operations.tcOrganizationOperationsIntf.class);
// find users
Hashtable mhSearchCriteria = new Hashtable();
if (orgName.startsWith("HR"))
mhSearchCriteria.put("Organizations.Organization Name", "HR*");
else
mhSearchCriteria
.put("Organizations.Organization Name", orgName);
tcResultSet moResultSet = moOrganizationUtility
.findOrganizations(mhSearchCriteria);
if (moResultSet == null || moResultSet.getRowCount() == 0)
throw new Exception("Organizations not found");
for (int i = 0; i < moResultSet.getRowCount(); i++) {
moResultSet.goToRow(i);
organizationKey = moResultSet
.getStringValue("Organizations.Key");
LOGGER("Organization Key is: " + organizationKey);
}
LOGGER("Done with getting organization key");
} catch (Exception e) {
LOGGER(e.getMessage());
throw new Exception(e);
}
LOGGER("Exiting Method");
return organizationKey;
}
/**
*
*/
private static void clean() {
LOGGER("OIM Logging out....");
// release resources
System.out.println("Releasing resources");
oimClient.logout();
LOGGER("OIM Log out successful");
}
public static void LOGGER(String msg) {
System.out.println("LOGGER >> " + msg);
}
}