Example usage for javax.naming.ldap LdapContext unbind

List of usage examples for javax.naming.ldap LdapContext unbind

Introduction

In this page you can find the example usage for javax.naming.ldap LdapContext unbind.

Prototype

public void unbind(Name name) throws NamingException;

Source Link

Document

Unbinds the named object.

Usage

From source file:com.liferay.portal.security.ldap.internal.exportimport.LDAPUserExporterImpl.java

@Override
public void exportUser(long userId, long userGroupId, UserOperation userOperation) throws Exception {

    User user = _userLocalService.getUser(userId);

    long companyId = user.getCompanyId();

    StopWatch stopWatch = new StopWatch();

    if (_log.isDebugEnabled()) {
        stopWatch.start();/*from   w w w  .  ja  v  a2  s.c o  m*/

        _log.debug(StringBundler.concat("Exporting user ", String.valueOf(user), " in user group ",
                String.valueOf(userGroupId)));
    }

    if (!_ldapSettings.isExportEnabled(companyId) || !_ldapSettings.isExportGroupEnabled(companyId)) {

        return;
    }

    long ldapServerId = _portalLDAP.getLdapServerId(companyId, user.getScreenName(), user.getEmailAddress());

    LdapContext ldapContext = _portalLDAP.getContext(ldapServerId, companyId);

    if (ldapContext == null) {
        return;
    }

    UserGroup userGroup = _userGroupLocalService.getUserGroup(userGroupId);

    Properties groupMappings = _ldapSettings.getGroupMappings(ldapServerId, companyId);
    Properties userMappings = _ldapSettings.getUserMappings(ldapServerId, companyId);

    Binding binding = _portalLDAP.getGroup(ldapServerId, companyId, userGroup.getName());

    if (binding == null) {
        if (userOperation == UserOperation.ADD) {
            addGroup(ldapServerId, ldapContext, userGroup, user, groupMappings, userMappings);
        } else {
            if (_log.isWarnEnabled()) {
                _log.warn("Unable to get or add LDAP bindings for user group " + userGroup.getName());
            }
        }

        return;
    }

    try {
        Name name = new CompositeName();

        name.add(binding.getNameInNamespace());

        Modifications modifications = _portalToLDAPConverter.getLDAPGroupModifications(ldapServerId, userGroup,
                user, groupMappings, userMappings, userOperation);

        ModificationItem[] modificationItems = modifications.getItems();

        ldapContext.modifyAttributes(name, modificationItems);
    } catch (SchemaViolationException sve) {
        if (_log.isInfoEnabled()) {
            _log.info("Unable to update LDAP bindings for user group " + userGroup.getName(), sve);
        }

        String fullGroupDN = binding.getNameInNamespace();

        Attributes attributes = _portalLDAP.getGroupAttributes(ldapServerId, companyId, ldapContext,
                fullGroupDN, true);

        Attribute groupMembers = attributes.get(groupMappings.getProperty(GroupConverterKeys.USER));

        if ((groupMembers != null) && (groupMembers.size() == 1)) {
            ldapContext.unbind(fullGroupDN);
        }
    } finally {
        if (ldapContext != null) {
            ldapContext.close();
        }

        if (_log.isDebugEnabled()) {
            _log.debug(StringBundler.concat("Finished exporting user ", String.valueOf(user), " in user group ",
                    String.valueOf(userGroupId), " in ", String.valueOf(stopWatch.getTime()), "ms"));
        }
    }
}