Grant Entitlements To User Using API

Grant Entitlements To User


code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import javax.security.auth.login.AccountNotFoundException;
import javax.security.auth.login.LoginException;
import oracle.iam.identity.exception.NoSuchUserException;
import oracle.iam.identity.exception.UserLookupException;
import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.usermgmt.api.UserManagerConstants;
import oracle.iam.passwordmgmt.exception.UserNotFoundException;
import oracle.iam.platform.OIMClient;
import oracle.iam.platform.authopss.exception.AccessDeniedException;
import oracle.iam.platform.entitymgr.vo.SearchCriteria;
import oracle.iam.provisioning.api.EntitlementService;
import oracle.iam.provisioning.api.ProvisioningConstants;
import oracle.iam.provisioning.api.ProvisioningService;
import oracle.iam.provisioning.exception.EntitlementAlreadyProvisionedException;
import oracle.iam.provisioning.exception.EntitlementNotFoundException;
import oracle.iam.provisioning.exception.GenericEntitlementServiceException;
import oracle.iam.provisioning.exception.GenericProvisioningException;
import oracle.iam.provisioning.exception.ImproperAccountStateException;
import oracle.iam.provisioning.vo.Entitlement;
import oracle.iam.provisioning.vo.EntitlementInstance;


public class GrantEntitlementToUser {
    private static String OIMUserName = "xelsysadm";
    private static String OIMPassword = <Password>;
    private static String OIMURL = "t3://<HostName>:<portNo>";
    private static String OIMInitialContextFactory ="weblogic.jndi.WLInitialContextFactory";
    static UserManager userManager=getOimClient().getService(UserManager.class);
    static ProvisioningService provisioningService=getOimClient().getService(ProvisioningService.class);
    static EntitlementService entitlementService=getOimClient().getService(EntitlementService.class);
   
    public static void grantEntitlement(String userLogin, String entitlementCode) throws NoSuchUserException,
                                                                        UserLookupException,
                                                                        UserNotFoundException,
                                                                        GenericProvisioningException,
                                                                        GenericEntitlementServiceException,
                                                                        AccountNotFoundException,
                                                                        ImproperAccountStateException,
                                                                        EntitlementNotFoundException,
                                                                        EntitlementAlreadyProvisionedException,
                                                                        oracle.iam.provisioning.exception.UserNotFoundException
        {
        // Get user's key
        String userKey = getUserKeyByUserLogin(userLogin);
      
       
        // Get user's account filtered by application instance display name
        boolean populateAcctData = false;
        SearchCriteria appInstCriteria = new SearchCriteria(ProvisioningConstants.AccountSearchAttribute.DISPLAY_NAME.getId(), <Application Instance Display Name>, SearchCriteria.Operator.EQUAL);
        System.out.println("SearchCriteria of appInstCriteria:"+appInstCriteria);
        HashMap acctConfigParams = new HashMap();
        List<oracle.iam.provisioning.vo.Account>  userAccounts =  provisioningService.getAccountsProvisionedToUser(userKey, appInstCriteria, acctConfigParams, populateAcctData);
        System.out.println("List <Accounts>:"+userAccounts);
       
        // Get specific Entitlement Definitions
        SearchCriteria entDefCriteria = new SearchCriteria(ProvisioningConstants.EntitlementSearchAttribute.ENTITLEMENT_DISPLAYNAME.getId(), entitlementCode, SearchCriteria.Operator.EQUAL);
       System.out.println("SearchCriteria of entDefCriteria:"+entDefCriteria);
        HashMap entConfigParams = new HashMap();
        List <Entitlement> entitlements = entitlementService.findEntitlements(entDefCriteria, entConfigParams);
        System.out.println("List <Entitlement>: "+entitlements);
     
        // Ensure an entitlement can be added to a specific resource on a user
      if (userAccounts != null && !userAccounts.isEmpty())
        {
            // Get the first resource account
            oracle.iam.provisioning.vo.Account userAccount = userAccounts.get(0);
            String accountKey = userAccount.getAccountID(); // OIU_KEY
      
           System.out.println("accountkey : "+accountKey);
           Entitlement entitlement = entitlements.get(0);
           System.out.println("Entitlement def : "+entitlement);
           EntitlementInstance grantEntInst = new EntitlementInstance();
           grantEntInst.setEntitlement(entitlement); // **
           grantEntInst.setAccountKey(Long.parseLong(accountKey)); // ** OIU_KEY
           try {
            provisioningService.grantEntitlement(grantEntInst);
        } catch (AccessDeniedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (oracle.iam.provisioning.exception.AccountNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
       
        else
        {
      System.out.println("Entitlement not granted to the user");
        }
    }

  private static String getUserKeyByUserLogin(String userLogin) throws NoSuchUserException, UserLookupException
    {
        boolean userLoginUsed = true;
        HashSet attrsToFetch = new HashSet();
        attrsToFetch.add(UserManagerConstants.AttributeName.USER_KEY.getId());
        attrsToFetch.add(UserManagerConstants.AttributeName.USER_LOGIN.getId());
        oracle.iam.identity.usermgmt.vo.User user = userManager.getDetails(userLogin, attrsToFetch, userLoginUsed);
        return user.getEntityId();
    }
  public static OIMClient getOimClient(){
      Hashtable evn=new Hashtable();
      evn.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, OIMInitialContextFactory);
      evn.put(OIMClient.JAVA_NAMING_PROVIDER_URL, OIMURL);
      System.setProperty("OIM.AppServerType", "wls");
          System.setProperty("APPSERVER_TYPE", "wls");
          System.setProperty("java.security.auth.login.config","C:\\Oracle\\Middleware\\Oracle_IDM1\\server\\config\\authwl.conf");
          System.out.println("......client is invoked....");
          OIMClient client=new OIMClient(evn);
          System.out.println("....Initiating Oim Client.....");
          try {
                      client.login(OIMUserName, OIMPassword.toCharArray());
              } catch (LoginException e) {
                      System.out.println("Error: " + e);
                  client = null;
              }
          System.out.println(".......got Oim client successfully......");
          System.out.println("Client - " + client);
   return client;
     
  }

  public static void readData() throws FileNotFoundException,
                                         NoSuchUserException,
                                         UserLookupException,
                                         GenericProvisioningException,
                                         GenericEntitlementServiceException,
                                         AccountNotFoundException,
                                         ImproperAccountStateException,
                                         EntitlementNotFoundException,
                                         EntitlementAlreadyProvisionedException {
    File file = new File("<Path of Input File>");
    Scanner sc = new Scanner(file);
    String  a[]=new String[2];
    a[0]=sc.next();
    String userLogin=a[0];
    a[1]=sc.next();
    String entitlementCode=a[1];
    System.out.println(userLogin);
    System.out.println(a[1]);


        try {
            grantEntitlement(userLogin,entitlementCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
  public static void main(String[] args) throws FileNotFoundException,
                                                  NoSuchUserException,
                                                  UserLookupException,
                                                  GenericProvisioningException,
                                                  GenericEntitlementServiceException,
                                                  AccountNotFoundException,
                                                  ImproperAccountStateException,
                                                  EntitlementNotFoundException,
                                                  EntitlementAlreadyProvisionedException
                                                  {
    readData();
    }
}

The input of the file should be like below


The first line is user login and the second line is entitlement name
The Scanner will scan the data available in the file and store in the local variables in the readData().



Run the above program and the entitlement will be assigned to the user automatically if the user have the account related to that Entitlement.

please comment below if you have any queries.
Thank you

Comments

Popular posts from this blog

CREATING USER BY USING DYNAMIC VALUES

list of accounts of a specific login id