Custom Admin Role With Grant Role Capabilities


  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.logging.FileHandler;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import oracle.iam.platform.authopss.api.PolicyConstants;
  10. import oracle.iam.platform.authopss.api.PolicyConstants.Actions;
  11. import oracle.iam.platform.authopss.api.PolicyConstants.Resources;
  12. import oracle.iam.platform.authopss.vo.AdminRole;
  13. import oracle.iam.platform.authopss.vo.AdminRoleVO;
  14. import oracle.iam.platform.authopss.vo.Capability;
  15. import oracle.iam.platformservice.api.AdminRoleService;
  16. import com.oimacademy.connection.Platform;
  17. public class CustomAdminRoleWihGrantRoleCapabilities {
  18.  private static Logger logger;
  19.   private static final String DEFAULT_LOG_FILE = "customAdminRoleWihGrantRoleCapabilities.log";  
  20.   private static final String usageString = "\nUsage of CustomAdminRoleWihGrantRoleCapabilities.java \n" +
  21.     " where arguments are :\n" +
  22.     "  ADMIN_ROLE_NAME\t\t [Required] Custom Admin Role Name. \n" + 
  23.     "  RESOURCES\t\t\t [Required] Ex: Resources such as ROLE, USER, ORGANIZATION  etc \n" +
  24.     "  RESOURCES ACTION\t\t [Required] Ex: Actions For Respective Resources such as ADD_MEMBERS, ADD_ADMIN_USER  etc \n" +
  25.     " [Option] You can pass multiple RESOURCES ACTION in ',' seperated values. Ex: ADD_MEMBERS,CREATE,MODIFY \n" +
  26.     "  For More Info on Resource and Action   \n" +
  27.     "  1. https://docs.oracle.com/cd/E52734_01/oim/OMJAV/oracle/iam/platform/authopss/api/PolicyConstants.Resources.html \n" +
  28.     "  2.  https://docs.oracle.com/cd/E52734_01/oim/OMJAV/oracle/iam/platform/authopss/api/PolicyConstants.Actions.html   \n";  
  29.  private static AdminRoleService adminRoleService; 
  30.  private static List<Capability> getCapabilitiess(PolicyConstants.Resources resourceType, PolicyConstants.Actions actions) 
  31.  throws Exception {
  32.   adminRoleService = Platform.getService(AdminRoleService.class);
  33.         return adminRoleService.getCapabilitiess(resourceType, actions);
  34.     }
  35.  private static void createAdminRoleWithAddMemberCapabilities(String adminRoleName,PolicyConstants.Resources resource,
  36.  List<PolicyConstants.Actions> actions) throws Exception{
  37.    logger.log(Level.INFO,"Starting createAdminRoleWithAddMemberCapabilities ");
  38.   AdminRole adminrole= new AdminRole(adminRoleName, " Display Name "+adminRoleName, "Admin Role for "
  39.    +resource.getId()+" with capabilities ", true);
  40.      List<Capability> capabilities = new ArrayList<Capability>();
  41.      for(PolicyConstants.Actions action : actions){
  42.       logger.log(Level.INFO,"Resource :"+resource+", Actions : "+action);
  43.       capabilities.addAll(getCapabilitiess(resource, action));
  44.      }   
  45.      logger.log(Level.INFO," Setting capabilities");
  46.      adminrole.setCapabilities(capabilities);
  47.      AdminRoleVO adminRolevo = new AdminRoleVO( adminrole);
  48.      logger.log(Level.INFO," Invoking createAdminRole...!");
  49.      adminRolevo = adminRoleService.createAdminRole(adminRolevo);
  50.      logger.log(Level.INFO,"Ending  createAdminRoleWithAddMemberCapabilities with Admin Role Name :"
  51.      +adminRolevo.getAdminRole().getRoleName());
  52.     } 
  53.  public static void main(String[] args) throws Exception {
  54.   /*
  55.          * Configuring logger level           
  56.          */
  57.     setLogger();
  58.    logger.log(Level.INFO,"Starting Main Method..! ");       
  59.   try{
  60.     boolean configFileOptionPresent = readArguments(args);
  61.           if (!configFileOptionPresent) {              
  62.               logger.log(Level.SEVERE,"ERROR: Arguments Cannot be Null Or Invalid Command Line.. \n "+usageString);
  63.                throw new IllegalArgumentException(" ERROR: Arguments Cannot be Null Or Invalid Command Line.. \n "+usageString);
  64.           }          
  65.           String adminRoleName =args[0];
  66.           String resourceName =args[1].toUpperCase();
  67.           String actionNames=args[2];          
  68.           validateParams(adminRoleName,resourceName,actionNames);          
  69.           List actions =Arrays.asList(actionNames.trim().split(","));
  70.           logger.log(Level.INFO,"Admin Role Name : "+adminRoleName+", resource Name : "+resourceName+", Actions : "+actions);
  71.           PolicyConstants.Resources resource= getResources(resourceName);
  72.           List<PolicyConstants.Actions> actionTypeList = getActions(resource,actions);        
  73.    createAdminRoleWithAddMemberCapabilities(adminRoleName,resource,actionTypeList); 
  74.     logger.log(Level.INFO,"Ending  Main Method..! ");
  75.   }catch(Exception e){
  76.    logger.log(Level.SEVERE," Exception Occured with Message "+e);
  77.    System.out.println(" For Detail Exception Please look at File : "+new File("").getAbsolutePath()+
  78.    "/customAdminRoleWihGrantRoleCapabilities.log");
  79.    //throw e;
  80.   }
  81.  } 
  82.   private static void validateParams(String adminRoleName,String resourceName,String actionNames) throws Exception {
  83.    logger.log(Level.INFO,"Starting validateParams  Method..! ");
  84.    if(adminRoleName==null || adminRoleName.isEmpty()){
  85.           logger.log(Level.SEVERE, "ERROR: Invalid ADMIN_ROLE_NAME. Please Pass Valid ADMIN_ROLE_NAME. \n "+usageString);
  86.            throw new Exception(" ERROR: Invalid ADMIN_ROLE_NAME. Please Pass Valid ADMIN_ROLE_NAME. \n "+usageString);
  87.          }         
  88.          if(resourceName==null || resourceName.isEmpty()){
  89.           logger.log(Level.SEVERE, "ERROR: Invalid Resources. Please Pass Valid RESOURCES. \n "+usageString);
  90.            throw new Exception(" ERROR: Invalid Resources. Please Pass Valid RESOURCES. \n "+usageString);
  91.          }        
  92.          if(actionNames==null | actionNames.isEmpty()){
  93.           logger.log(Level.SEVERE, "ERROR: Invalid RESOURCE ACTION. Please Pass Valid Actions for Entity. \n "+usageString);
  94.           throw new Exception(" ERROR: Invalid RESOURCE ACTION. Please Pass Valid Actions for Entity. \n "+usageString);
  95.          }
  96.          logger.log(Level.INFO,"Ending validateParams  Method..! ");
  97.   }
  98.  private static PolicyConstants.Resources getResources(String resource){
  99.   logger.log(Level.INFO," Entering  getResources Method... !");
  100.   Resources resourceObj=null;
  101.   for ( Resources resourceEnum : PolicyConstants.Resources.values()){
  102.          if(resourceEnum.toString().equals(resource)){
  103.         resourceObj=resourceEnum;
  104.         logger.log(Level.INFO," Found Resources "+resourceObj );         
  105.           break;
  106.          }
  107.        }
  108.   logger.log(Level.INFO," Ending  getResources Method with Resources"+resourceObj);
  109.   return resourceObj;
  110.  } 
  111.  private static List<PolicyConstants.Actions> getActions(PolicyConstants.Resources resourceObj,List actions){
  112.   logger.log(Level.INFO," Entering  getActions Method... !");
  113.   List<PolicyConstants.Actions> actionTypeList = new ArrayList<PolicyConstants.Actions>();
  114.    for(Actions action : resourceObj.getActions()){
  115.     if(actions.contains(action.toString())){
  116.      logger.log(Level.INFO," Actions "+action.toString()+" Found For Resources "+resourceObj );
  117.           actionTypeList.add(action); 
  118.            }
  119.     }  
  120.   logger.log(Level.INFO," Ending  getActions Method with Actions "+actionTypeList);
  121.   return actionTypeList;
  122.  } 
  123.  private static boolean readArguments(String[] args) throws Exception {
  124.         
  125.         boolean configFileOptionPresent = true;
  126.   if(args.length!=3){
  127.    configFileOptionPresent=false;
  128.    throw new Exception(" ERROR: Inavlid Command Line Arguments. \n "+usageString);
  129.    }            
  130.         return configFileOptionPresent;
  131.     }
  132.  /**
  133.      * Sets up Logger.
  134.      */
  135.     public static void setLogger() throws IOException {       
  136.             String logFile = DEFAULT_LOG_FILE;     
  137.         logger = Logger.getLogger("oracle.iam.custom.adminRole");
  138.         logger.setLevel(Level.ALL);
  139.         try {
  140.             FileHandler loggerFileHandler = new FileHandler(logFile, true);
  141.             if (!logFile.toLowerCase().endsWith("xml")) {
  142.                 loggerFileHandler.setFormatter(new java.util.logging.SimpleFormatter());
  143.             }
  144.             logger.addHandler(loggerFileHandler);
  145.             logger.setUseParentHandlers(false);
  146.         } catch (IOException io) {
  147.             logger.log(Level.SEVERE, "Exception In Adding Log Handler");
  148.              throw io;
  149.         }
  150.     }   
  151. }

No comments:

Post a Comment