import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import sailpoint.api.SailPointContext;
import sailpoint.api.Terminator;
import sailpoint.object.Application;
import sailpoint.object.Bundle;
import sailpoint.object.Filter;
import sailpoint.object.GroupDefinition;
import sailpoint.object.Identity;
import sailpoint.object.IdentitySelector;
import sailpoint.object.Profile;
import sailpoint.tools.GeneralException;
import sailpoint.tools.RFC4180LineParser;
public class ITRole {
static SailPointContext context = null;
public static void deleteRole(HashMap roleHash) throws GeneralException {
String roleName = roleHash.get("Role Name").toString();
Bundle bundle = context.getObjectByName(Bundle.class, roleName);
if (bundle != null) {
Terminator terminator = new Terminator(context);
terminator.deleteObject(bundle);
} else {
System.out.println("Role doesn't exist / Role deleted...");
}
}
public static void bundBusinessRole(HashMap roleHash) throws GeneralException {
String orgRole = roleHash.get("Organization Role").toString();
String roleName = roleHash.get("Role Name").toString();
String roleType = roleHash.get("Role Type").toString();
String roleOwner = roleHash.get("Role Owner").toString();
String itRole = roleHash.get("IT Role").toString();
String assignmentRule = roleHash.get("Entilements").toString();
Bundle role = context.getObject(Bundle.class, roleName);
if (role == null) {
role = new Bundle();
}
role.setName(roleName);
if (roleType.toLowerCase().startsWith("it")) {
role.setType("it");
} else {
role.setType("business");
}
Identity ownerId = context.getObject(Identity.class, roleOwner);
if (ownerId == null) {
ownerId = context.getObjectById(Identity.class, "spadmin");
}
role.setOwner(ownerId);
role.setDisplayName(roleName);
role.setAllowDuplicateAccounts(false);
role.setAllowMultipleAssignments(false);
role.setMergeTemplates(false);
// Look up the role for the Inheritance here
Bundle bundleObj = context.getObject(Bundle.class, "LD");
role.addInheritance(bundleObj);
// Handle Business - role specific build out options here
if (role.getType().contains("business")) {
IdentitySelector identitySelector = new IdentitySelector();
GroupDefinition groupDefinition = context.getObjectByName(GroupDefinition.class, assignmentRule);
identitySelector.setPopulation(groupDefinition);
role.setSelector(identitySelector);
// Clear the Previous List of requirements for this role
if (null != role.getRoleAssignments()) {
role.getRequirements().clear();
}
// Add the required IT Roles for this business Role
Bundle requiredRole = context.getObjectByName(Bundle.class, itRole);
if (null == requiredRole) {
System.out.println("Required Roles not found...");
} else {
role.addRequirement(bundleObj);
}
}
context.saveObject(role);
context.commitTransaction();
context.decache(role);
return;
}
public static void bundRole(HashMap roleHash) throws GeneralException {
String roleName = roleHash.get("Role Name").toString();
String roleType = roleHash.get("Role Type").toString();
String roleOwner = roleHash.get("Role Owner").toString();
String appName = roleHash.get("Application Name").toString();
String attName = roleHash.get("Attribute").toString();
String entsList = roleHash.get("Entilements").toString();
Bundle role = null;
try {
role = context.getObject(Bundle.class, roleName);
if (role == null) {
role = new Bundle();
}
role.setName(roleName);
if (roleType.toLowerCase().startsWith("it")) {
role.setType("it");
} else {
role.setType("business");
}
Identity ownerId = context.getObject(Identity.class, roleOwner);
if (ownerId == null) {
ownerId = context.getObjectById(Identity.class, "spadmin");
}
role.setOwner(ownerId);
role.setDisplayName(roleName);
role.setAllowDuplicateAccounts(false);
role.setAllowMultipleAssignments(false);
role.setMergeTemplates(false);
// Lookup the application for the role, if one is required
Application appObj = null;
if (appName != null) {
appObj = context.getObject(Application.class, appName);
if (appObj == null) {
return;
}
}
// Convert the entitlements list into a list object
if (null != entsList) {
entsList.replace("\\/", "/");
}
RFC4180LineParser entParser = new RFC4180LineParser("|");
ArrayList<String> entitlements = entParser.parseLine(entsList);
// Look up the role for the Inheritance here
Bundle bundleObj = context.getObject(Bundle.class, "LD");
role.addInheritance(bundleObj);
// Handle IT - role specific build out options here
if (role.getType().contains("it")) {
// clear out the previous profiles on the role
if (null != role.getProfiles()) {
role.getProfiles().clear();
}
for (int e = 0; e < entitlements.size(); e++) {
Filter filter = Filter.eq(attName, entitlements.get(e));
Profile profile = new Profile();
profile.addConstraint(filter);
profile.setApplication(appObj);
role.add(profile);
}
}
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
}
context.saveObject(role);
context.commitTransaction();
context.decache(role);
return;
}
public static void main(String[] args) {
int lineCounter = 0;
String dlm = ",";
String thisLine = "";
String headerString = "";
String valueString = "";
HashMap lineHash = null;
List bundles = null;
List headerStrings = new ArrayList();
String csvFileName = "P:\\IDM\\Sailpoint\\Ent-users.csv";
System.out.println("Role Creation Started ...");
BufferedReader fileIn = null;
File bundleFile = null;
System.out.println("Reading Bundle Data from : " + csvFileName);
try {
bundleFile = new File(csvFileName);
if ((!bundleFile.exists()) || bundleFile.isDirectory()) {
System.out.println("Unable to find the bundle csv file: " + csvFileName);
return;
}
fileIn = new BufferedReader(new FileReader(csvFileName));
RFC4180LineParser parser = new RFC4180LineParser(dlm);
while (null != (thisLine = fileIn.readLine())) {
ArrayList tokens = parser.parseLine(thisLine);
if (lineCounter == 0) {
for (int i = 0; i < tokens.size(); i++) {
headerStrings.add((String) tokens.get(i));
}
} else {
lineHash = new HashMap();
for (int i = 0; i < tokens.size(); i++) {
headerString = headerStrings.get(i).toString();
valueString = tokens.get(i).toString();
}
if (null != valueString) {
valueString = valueString.trim();
} else {
valueString = "";
}
lineHash.put(headerString, valueString);
}
try {
// Creating Roles Data
bundRole(lineHash);
} catch (Exception e) {
System.out.println("Error while Creating Data Exception : " + lineHash);
}
}
lineCounter++;
if ((lineCounter % 20) == 0) {
context.decache();
}
}
// fileIn.close();
catch (FileNotFoundException e) {
System.out.println("FileNotFoundException : " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException : " + e.getMessage());
} catch (GeneralException e) {
System.out.println("GeneralException : " + e.getMessage());
}
}
}