OIM API To Check Has Access for Admin Role


  1. package com.oimacademy.NeedEvaluate;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. import com.oimacademy.connection.Platform;
  5. import oracle.iam.identity.exception.AccessDeniedException;
  6. import oracle.iam.identity.rolemgmt.api.RoleManager;
  7. import oracle.iam.identity.rolemgmt.api.RoleManagerConstants;
  8. import oracle.iam.identity.rolemgmt.api.RoleManagerConstants.RoleAttributeName;
  9. import oracle.iam.identity.rolemgmt.vo.Role;
  10. import oracle.iam.platform.context.ContextManager;
  11. public class HasAccessByAdminRole {
  12.  public static void main(String[] args) {
  13.   hasAccessByAdminRole();
  14.  }
  15.  public static void hasAccessByAdminRole() throws AccessDeniedException {
  16.         boolean isAdminRoleGranted = false;
  17.         String loggedinUsrName = (String)ContextManager.getOIMUser();
  18.         String loggedinUsrKey = (String) ContextManager.getUserPreference(ContextManager.USERDETAILS.KEY.getKey());
  19.     try {
  20.       System.out.println(" loggedinUsrKey  "+loggedinUsrKey);
  21.       System.out.println(" loggedinUsrName  "+loggedinUsrName);
  22.                 RoleManager roleManager = Platform.getService(RoleManager.class);
  23.                 Set<String> returnAttrs = new HashSet<String>();
  24.             returnAttrs.add(RoleAttributeName.KEY.getId());
  25.             // find the System Admin Role Key
  26.                 Role role = roleManager.getDetails(RoleAttributeName.NAME.getId(), RoleManagerConstants.SYS_ADMIN_ROLE_NAME, returnAttrs);
  27.                 if(role!=null){
  28.                         String roleKey = role.getEntityId();
  29.                         System.out.println(" roleKey  "+roleKey);
  30.                         // check if System Admin role is granted to logged in user in directAndIndirect Roles
  31.                         isAdminRoleGranted = roleManager.isRoleGranted(roleKey, loggedinUsrKey, true);
  32.                         System.out.println(" isAdminRoleGranted  "+isAdminRoleGranted);
  33.                 }
  34.         } catch (Exception e) {
  35.                 System.out.println("Error while checking user's membership roles");
  36.         }
  37.         //If logged in user is NOT a member of System Administrators group, throw Exception
  38.         if(!isAdminRoleGranted) {
  39.                 throw new AccessDeniedException();
  40.         }
  41. }
  42. }


No comments:

Post a Comment