Total Pageviews

Tuesday, April 5, 2016

grant entitlements through API



public static void grantEntitlementToUser(String userLogin, String entitlementCode) throws NoSuchUserException, UserLookupException, UserNotFoundException, GenericProvisioningException, GenericEntitlementServiceException, AccountNotFoundException, ImproperAccountStateException, EntitlementNotFoundException, EntitlementAlreadyProvisionedException
        {
            // 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(), "DisplayName", SearchCriteria.Operator.EQUAL);
            HashMap acctConfigParams = new HashMap();
            List userAccounts = provisioningservice.getAccountsProvisionedToUser(userKey, appInstCriteria, acctConfigParams, populateAcctData);
           
           
            // Get specific Entitlement Definitions
            SearchCriteria entDefCriteria = new SearchCriteria(ProvisioningConstants.EntitlementSearchAttribute.ENTITLEMENT_DISPLAYNAME.getId(), entitlementCode, SearchCriteria.Operator.EQUAL);
            HashMap entConfigParams = new HashMap();
            List entitlements = entSvc.findEntitlements(entDefCriteria, entConfigParams);
        
         
            // Ensure an entitlement can be added to a specific resource on a user
        
            if (userAccounts != null && !userAccounts.isEmpty())
            {
                // Get the first resource account
                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
               provisioningservice.grantEntitlement(grantEntInst);
            }
           
            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());
            User user = userManager.getDetails(userLogin, attrsToFetch, userLoginUsed);
            return user.getEntityId();
        }
     

2 comments: