Saturday 31 March 2018

OIM API - Code Snippet to get Role from OIM for the given Role Name.

 public Role getRole(String roleName){
        final String logp = CN + " :: getRole - ";
        LOGGER.debug(logp + "START");

        if (null == roleName || roleName.trim().length() == 0) {
                return null;
        }
        roleName = roleName.trim();

        Role role = null;
        try {
            //get role manager service
            RoleManager roleManager = Platform.getService(RoleManager.class);
           
            SearchCriteria criteria = new SearchCriteria(RoleAttributeName.NAME.getId(), roleName, SearchCriteria.Operator.EQUAL);

            List<Role> roleList = roleManager.search(criteria, null, null);

            if(null == roleList || roleList.size() == 0){
                LOGGER.error(logp + "Role not found in OIM for role name - " + roleName);
            }else if(roleList.size() > 1){
                LOGGER.error(logp + "More than 1 role found in OIM for role name - " + roleName);
            } else {
                role = roleList.get(0);
                LOGGER.info(logp + "Successfully obtained role for role name " + roleName);
                LOGGER.info(logp + "Role Key : " + role.getEntityId());
                LOGGER.info(logp + "Role Display Name : " + role.getDisplayName());
            }
        }catch(Exception e){
                LOGGER.error(logp + "Exception while fetching role  " + roleName + " - " + e, e);
        }

        LOGGER.debug(logp + "END");
        return role;
 }


Happy Learning!!!

No comments:

Post a Comment