2 Replies Latest reply: Jan 2, 2013 7:37 PM by idamGod RSS

    Grant a role to a user using API in OIM 11gR1

    904088
      Friends,

      Could you please provide me some code snippet to provision a role to a user using API in OIM-R1?

      Thanks,
      Vetri
        • 1. Re: Grant a role to a user using API in OIM 11gR1
          Rajiv Dewan
          Set a = new HashSet();
               a.add("21");
          RoleManager roleManager = client.getService(RoleManager.class);
          roleManager .grantRole("101", a);


          101: ROLE KEY
          21: USER KEY
          • 2. Re: Grant a role to a user using API in OIM 11gR1
            idamGod
            Try this code.

            public void GrantRole(String userLogin,String roleName) {
            try
            {
            String userKey=getUserKey(userLogin);
            String roleKey=getRoleKey(roleName);

            RoleManager roleMgr = oimClient.getService(RoleManager.class);
            Set<String> userKeys = new HashSet<String>();
            userKeys.add(userKey);
            RoleManagerResult result = roleMgr.grantRole(roleKey, userKeys);
            System.out.println("Status:"+result.getStatus());
            }
            catch(Exception e) {
            e.printStackTrace();
            }
            }

            public String getRoleKey(String roleName)
            {
            String roleKey = null;
            try
            {
            RoleManager roleMgr = oimClient.getService(RoleManager.class);
            List<Role> roles = null;
            SearchCriteria criteria = null;

            criteria = new SearchCriteria(RoleManagerConstants.ROLE_NAME, roleName, SearchCriteria.Operator.EQUAL);

            roles = roleMgr.search(criteria, null, null);
            roleKey = (String)roles.get(0).getAttribute(RoleManagerConstants.ROLE_KEY);
            }
            catch(Exception e) {
            e.printStackTrace();
            }
            return roleKey;
            }

            public String getUserKey(String userName) throws Exception
            {
            UserManager usrService = oimClient.getService(UserManager.class);
            User user = usrService.getDetails("User Login", userName, null);
            String userKey=user.getAttribute("usr_key").toString();
            return userKey;
            }