Example usage for javax.naming.directory SearchResult getName

List of usage examples for javax.naming.directory SearchResult getName

Introduction

In this page you can find the example usage for javax.naming.directory SearchResult getName.

Prototype

public String getName() 

Source Link

Document

Retrieves the name of this binding.

Usage

From source file:org.wso2.carbon.user.core.ldap.ReadWriteLDAPUserStoreManager.java

protected void updateLDAPRoleName(RoleContext context, String newRoleName) throws UserStoreException {

    String roleName = context.getRoleName();
    String groupSearchFilter = ((LDAPRoleContext) context).getSearchFilter();
    String roleNameAttributeName = ((LDAPRoleContext) context).getRoleNameProperty();
    String searchBase = ((LDAPRoleContext) context).getSearchBase();

    DirContext mainContext = this.connectionSource.getContext();
    DirContext groupContext = null;
    NamingEnumeration<SearchResult> groupSearchResults = null;

    try {/*from w  w w.jav  a2  s .c o m*/

        groupSearchFilter = groupSearchFilter.replace("?", escapeSpecialCharactersForFilter(roleName));
        String[] returningAttributes = { roleNameAttributeName };
        groupSearchResults = searchInGroupBase(groupSearchFilter, returningAttributes,
                SearchControls.SUBTREE_SCOPE, mainContext, searchBase);
        SearchResult resultedGroup = null;
        while (groupSearchResults.hasMoreElements()) {
            resultedGroup = groupSearchResults.next();
        }

        if (resultedGroup == null) {
            throw new UserStoreException("Could not find user role " + roleName + " in LDAP server.");
        }

        String groupNameRDN = resultedGroup.getName();
        String newGroupNameRDN = roleNameAttributeName + "=" + newRoleName;

        groupContext = (DirContext) mainContext.lookup(groupSearchBase);
        groupContext.rename(groupNameRDN, newGroupNameRDN);

        String roleNameWithDomain = UserCoreUtil.addDomainToName(roleName, getMyDomainName());
        String newRoleNameWithDomain = UserCoreUtil.addDomainToName(newRoleName, getMyDomainName());
        this.userRealm.getAuthorizationManager().resetPermissionOnUpdateRole(roleNameWithDomain,
                newRoleNameWithDomain);
    } catch (NamingException e) {
        String errorMessage = "Error occurred while modifying the name of role: " + roleName;
        if (log.isDebugEnabled()) {
            log.debug(errorMessage, e);
        }
        throw new UserStoreException(errorMessage, e);
    } finally {
        JNDIUtil.closeNamingEnumeration(groupSearchResults);
        JNDIUtil.closeContext(groupContext);
        JNDIUtil.closeContext(mainContext);
    }
}

From source file:org.wso2.carbon.user.core.ldap.ReadWriteLDAPUserStoreManager.java

protected void deleteLDAPRole(RoleContext context) throws UserStoreException {

    String roleName = context.getRoleName();
    String groupSearchFilter = ((LDAPRoleContext) context).getSearchFilter();
    groupSearchFilter = groupSearchFilter.replace("?", escapeSpecialCharactersForFilter(context.getRoleName()));
    String[] returningAttributes = { ((LDAPRoleContext) context).getRoleNameProperty() };
    String searchBase = ((LDAPRoleContext) context).getSearchBase();

    DirContext mainDirContext = null;
    DirContext groupContext = null;
    NamingEnumeration<SearchResult> groupSearchResults = null;

    try {/*  w w  w  .  j  av a2 s  . c  o  m*/

        mainDirContext = this.connectionSource.getContext();
        groupSearchResults = searchInGroupBase(groupSearchFilter, returningAttributes,
                SearchControls.SUBTREE_SCOPE, mainDirContext, searchBase);
        SearchResult resultedGroup = null;
        while (groupSearchResults.hasMoreElements()) {
            resultedGroup = groupSearchResults.next();
        }

        if (resultedGroup == null) {
            throw new UserStoreException("Could not find specified group/role - " + roleName);
        }

        String groupName = resultedGroup.getName();

        groupContext = (DirContext) mainDirContext.lookup(groupSearchBase);
        String groupNameAttributeValue = (String) resultedGroup.getAttributes()
                .get(realmConfig.getUserStoreProperty(LDAPConstants.GROUP_NAME_ATTRIBUTE)).get();
        if (groupNameAttributeValue.equals(roleName)) {
            groupContext.destroySubcontext(groupName);
        }
    } catch (NamingException e) {
        String errorMessage = "Error occurred while deleting the role: " + roleName;
        if (log.isDebugEnabled()) {
            log.debug(errorMessage, e);
        }
        throw new UserStoreException(errorMessage, e);
    } finally {
        JNDIUtil.closeNamingEnumeration(groupSearchResults);
        JNDIUtil.closeContext(groupContext);
        JNDIUtil.closeContext(mainDirContext);
    }

}