Sunday, April 28, 2024

ITRoles Creation Utlity

  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 java.util.Map;

  import org.apache.commons.logging.Log;

  import sailpoint.api.SailPointContext;

  import sailpoint.object.Application;

  import sailpoint.object.Bundle;

  import sailpoint.object.Filter;

  import sailpoint.object.Identity;

  import sailpoint.object.Profile;

  import sailpoint.tools.GeneralException;

  import sailpoint.tools.RFC4180LineParser;


  private void buildRole(HashMap roleHash) {


    String roleDec = roleHash.get("Role Description").toString();

    String roleOwner = roleHash.get("Role Owner").toString();

    String entsList = roleHash.get("Entilements").toString();

    String roleNameBeforeReplace = roleHash.get("Role Name").toString();

    String roleName = roleNameBeforeReplace.replace("+", "");


    String appName = "AD";

    Bundle role = null;

    Identity ownerId = null;

    Identity workgroupOwner = null;


    Map desc = new HashMap();

    desc.put("en_US", roleDec);


    try {


      role = context.getObjectByName(Bundle.class, roleName);

      if (role == null) {

        role = new Bundle();

      }


      ownerId = context.getObjectByName(Identity.class, roleOwner);

      if (ownerId == null) {



        ownerId = new Identity();

        ownerId.setName(roleOwner);

        ownerId.setDisplayName(roleOwner);

        workgroupOwner = context.getObjectByName(Identity.class, "spadmin");

        ownerId.setOwner(workgroupOwner);

        ownerId.setWorkgroup(true);

        ownerId.setNotificationOption(sailpoint.object.Identity.WorkgroupNotificationOption.MembersOnly);

        context.saveObject(ownerId);

        context.commitTransaction();

        context.decache(ownerId);

      }



      Application appObj = null;

      if (appName != null) {

        appObj = context.getObjectByName(Application.class, appName);

        if (appObj == null) {

          return;

        }

      }


      role.setType("it");

      role.setName(roleName);

      role.setDisplayName(roleName);

      role.setOwner(ownerId);

      role.setDescriptions(desc);


      List inheritList = new ArrayList();

      Bundle bundleObj = context.getObjectByName(Bundle.class, "Mighty IT Roles");

      inheritList.add(inheritList);


      RFC4180LineParser entParser = new RFC4180LineParser("|");

      ArrayList<String> entitlements = entParser.parseLine(entsList);


      if (role.getType().contains("it")) {


        List proList = new ArrayList();

        List filList = new ArrayList();

        Profile profile = new Profile();


        Filter filter = Filter.containsAll("Entilements", entitlements);

        filList.add(filter);

        profile.setConstraints(filList);

        profile.setApplication(appObj);

        proList.add(profile);

        role.setProfiles(filList);

        role.setInheritance(inheritList);


      }

      context.saveObject(role);

      context.commitTransaction();

      context.decache(role);

      return;


    } catch (Exception e) {

      log.error("Exception : " + e.getMessage());

    }

  }

  // Code Execution Starts here

  int lineCounter = 0;

  String dlm = ",";

  String thisLine = "";

  String headerString = "";

  String valueString = "";

  HashMap lineHash = null;

  List headerStrings = new ArrayList();

  String csvFileName = "Path";

  log.debug("Role Creation Started...");

  BufferedReader fileIn = null;

  File bundleFile = null;

  log.debug("Readin Bundle date from : " + csvFileName);


  try {

    bundleFile = new File(csvFileName);


    if ((!bundleFile.exists()) || bundleFile.isDirectory()) {

      log.error("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(tokens.get(i).toString().trim());

        }

      } else {

        lineHash = new HashMap();

        for (int i = 0; i < args.length; i++) {

          headerString = headerStrings.get(i).toString();

          valueString = tokens.get(i).toString();


          if (valueString != null) {

            valueString = valueString.trim();

          } else {

            valueString = "";

          }

          lineHash.put(headerString, valueString);

        }

        buildRole(lineHash);

      }

      lineCounter++;


      if ((lineCounter % 20) == 0) {

        context.decache();

      }

    }

  } catch (FileNotFoundException e) {

    log.error("Exception : " + e.getMessage());

  } catch (IOException e) {

    log.error("Exception : " + e.getMessage());

  } catch (GeneralException e) {

    log.error("Exception : " + e.getMessage());

  }

  return "Sucess";

No comments:

Post a Comment

Fetch Members from Workgroup

  import java.util.ArrayList;   import java.util.Iterator;   import java.util.List;   import sailpoint.api.ObjectUtil;   import sailpoint.ob...