Search in sources :

Example 6 with Attribute

use of com.unboundid.ldap.sdk.Attribute in project gitblit by gitblit.

the class LdapAuthProvider method sync.

public synchronized void sync() {
    final boolean enabled = settings.getBoolean(Keys.realm.ldap.synchronize, false);
    if (enabled) {
        logger.info("Synchronizing with LDAP @ " + settings.getRequiredString(Keys.realm.ldap.server));
        final boolean deleteRemovedLdapUsers = settings.getBoolean(Keys.realm.ldap.removeDeletedUsers, true);
        LdapConnection ldapConnection = new LdapConnection(settings);
        if (ldapConnection.connect()) {
            if (ldapConnection.bind() == null) {
                ldapConnection.close();
                logger.error("Cannot synchronize with LDAP.");
                return;
            }
            try {
                String uidAttribute = settings.getString(Keys.realm.ldap.uid, "uid");
                String accountBase = ldapConnection.getAccountBase();
                String accountPattern = ldapConnection.getAccountPattern();
                accountPattern = StringUtils.replace(accountPattern, "${username}", "*");
                SearchResult result = doSearch(ldapConnection, accountBase, accountPattern);
                if (result != null && result.getEntryCount() > 0) {
                    final Map<String, UserModel> ldapUsers = new HashMap<String, UserModel>();
                    for (SearchResultEntry loggingInUser : result.getSearchEntries()) {
                        Attribute uid = loggingInUser.getAttribute(uidAttribute);
                        if (uid == null) {
                            logger.error("Can not synchronize with LDAP, missing \"{}\" attribute", uidAttribute);
                            continue;
                        }
                        final String username = uid.getValue();
                        logger.debug("LDAP synchronizing: " + username);
                        UserModel user = userManager.getUserModel(username);
                        if (user == null) {
                            user = new UserModel(username);
                        }
                        if (!supportsTeamMembershipChanges()) {
                            getTeamsFromLdap(ldapConnection, username, loggingInUser, user);
                        }
                        // Get User Attributes
                        setUserAttributes(user, loggingInUser);
                        // store in map
                        ldapUsers.put(username.toLowerCase(), user);
                    }
                    if (deleteRemovedLdapUsers) {
                        logger.debug("detecting removed LDAP users...");
                        for (UserModel userModel : userManager.getAllUsers()) {
                            if (AccountType.LDAP == userModel.accountType) {
                                if (!ldapUsers.containsKey(userModel.username)) {
                                    logger.info("deleting removed LDAP user " + userModel.username + " from user service");
                                    userManager.deleteUser(userModel.username);
                                }
                            }
                        }
                    }
                    userManager.updateUserModels(ldapUsers.values());
                    if (!supportsTeamMembershipChanges()) {
                        final Map<String, TeamModel> userTeams = new HashMap<String, TeamModel>();
                        for (UserModel user : ldapUsers.values()) {
                            for (TeamModel userTeam : user.teams) {
                                // Is this an administrative team?
                                setAdminAttribute(userTeam);
                                userTeams.put(userTeam.name, userTeam);
                            }
                        }
                        userManager.updateTeamModels(userTeams.values());
                    }
                }
                if (!supportsTeamMembershipChanges()) {
                    getEmptyTeamsFromLdap(ldapConnection);
                }
            } finally {
                ldapConnection.close();
            }
        }
    }
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) HashMap(java.util.HashMap) Attribute(com.unboundid.ldap.sdk.Attribute) SearchResult(com.unboundid.ldap.sdk.SearchResult) LdapConnection(com.gitblit.ldap.LdapConnection) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 7 with Attribute

use of com.unboundid.ldap.sdk.Attribute in project zm-mailbox by Zimbra.

the class UBIDAttributes method getAttrs.

@Override
public Map<String, Object> getAttrs(Set<String> extraBinaryAttrs) throws LdapException {
    Map<String, Object> map = new HashMap<String, Object>();
    AttributeManager attrMgr = AttributeManager.getInst();
    for (Attribute attr : entry.getAttributes()) {
        String transferAttrName = attr.getName();
        String attrName = LdapUtil.binaryTransferAttrNameToAttrName(transferAttrName);
        boolean containsBinaryData = (attrMgr != null && attrMgr.containsBinaryData(attrName)) || (extraBinaryAttrs != null && extraBinaryAttrs.contains(attrName));
        if (attr.size() == 1) {
            map.put(attrName, getAttrStringInternal(attr, containsBinaryData));
        } else {
            String[] result = getMultiAttrStringInternal(attr, containsBinaryData);
            map.put(attrName, result);
        }
    }
    return map;
}
Also used : AttributeManager(com.zimbra.cs.account.AttributeManager) HashMap(java.util.HashMap) Attribute(com.unboundid.ldap.sdk.Attribute)

Example 8 with Attribute

use of com.unboundid.ldap.sdk.Attribute in project zm-mailbox by Zimbra.

the class UBIDUserCertificateAttributeTest method getMultiAttrStringShouldReturnCertificateForAttributeNameWithoutBinary.

@Test
public void getMultiAttrStringShouldReturnCertificateForAttributeNameWithoutBinary() {
    Attribute attr = new Attribute(ContactConstants.A_userCertificate, certBase64);
    Entry entry = PowerMockito.mock(Entry.class);
    UBIDAttributes attributes = new UBIDAttributes(entry);
    // entry does not contain "userCertificate;binary" attribute
    Mockito.when(entry.getAttribute(lookupAttr)).thenReturn(null);
    // entry contains "userCertificate" attribute
    Mockito.when(entry.getAttribute(ContactConstants.A_userCertificate)).thenReturn(attr);
    try {
        assertEquals(attributes.getMultiAttrString(lookupAttr, false)[0], certBase64);
    } catch (com.zimbra.cs.ldap.LdapException e) {
        fail("Exception thrown");
    }
}
Also used : Entry(com.unboundid.ldap.sdk.Entry) Attribute(com.unboundid.ldap.sdk.Attribute) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with Attribute

use of com.unboundid.ldap.sdk.Attribute in project cas by apereo.

the class LdapTestUtils method createLdapEntries.

/**
 * Creates the given LDAP entries.
 *
 * @param connection Open LDAP connection used to connect to directory.
 * @param entries    Collection of LDAP entries.
 * @param connInit   the connection initializer
 */
public static void createLdapEntries(final LDAPConnection connection, final Collection<LdapEntry> entries, final BindConnectionInitializer connInit) {
    for (val entry : entries) {
        val attrs = new ArrayList<Attribute>(entry.getAttributeNames().length);
        attrs.addAll(entry.getAttributes().stream().map(a -> new Attribute(a.getName(), a.getStringValues())).collect(Collectors.toList()));
        val ad = new AddRequest(entry.getDn(), attrs);
        LOGGER.debug("Creating entry [{}] with attributes [{}]", entry, attrs);
        try {
            connection.add(ad);
        } catch (final LDAPException e) {
            LOGGER.debug(e.getMessage(), e);
            if (e.getResultCode().equals(ResultCode.ENTRY_ALREADY_EXISTS)) {
                modifyLdapEntries(connection, entries, connInit);
            } else {
                LoggingUtils.error(LOGGER, e);
            }
        } catch (final Exception e) {
            LoggingUtils.error(LOGGER, e);
        }
    }
}
Also used : lombok.val(lombok.val) AddRequest(com.unboundid.ldap.sdk.AddRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) Attribute(com.unboundid.ldap.sdk.Attribute) LdapAttribute(org.ldaptive.LdapAttribute) ArrayList(java.util.ArrayList) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException)

Example 10 with Attribute

use of com.unboundid.ldap.sdk.Attribute in project graylog2-server by Graylog2.

the class UnboundLDAPConnector method createLDAPEntry.

public LDAPEntry createLDAPEntry(Entry entry, String uniqueIdAttribute) {
    requireNonNull(entry, "entry cannot be null");
    checkArgument(!isBlank(uniqueIdAttribute), "uniqueIdAttribute cannot be blank");
    final LDAPEntry.Builder ldapEntryBuilder = LDAPEntry.builder();
    // Always set the proper DN for the entry
    ldapEntryBuilder.dn(entry.getDN());
    // Always require and set the unique ID attribute
    final byte[] uniqueId = requireNonNull(entry.getAttributeValueBytes(uniqueIdAttribute), uniqueIdAttribute + " attribute cannot be null");
    ldapEntryBuilder.base64UniqueId(Base64.encode(uniqueId));
    if (entry.getObjectClassValues() != null) {
        ldapEntryBuilder.objectClasses(Arrays.asList(entry.getObjectClassValues()));
    }
    for (final Attribute attribute : entry.getAttributes()) {
        // in LDAPEntry#objectClasses
        if (OBJECT_CLASS_ATTRIBUTE.equalsIgnoreCase(attribute.getBaseName())) {
            continue;
        }
        if (attribute.needsBase64Encoding()) {
            for (final byte[] value : attribute.getValueByteArrays()) {
                if (isValidUTF8(value)) {
                    ldapEntryBuilder.addAttribute(attribute.getBaseName(), toUTF8String(value));
                } else {
                    ldapEntryBuilder.addAttribute(attribute.getBaseName(), Base64.encode(value));
                }
            }
        } else {
            for (final String value : attribute.getValues()) {
                ldapEntryBuilder.addAttribute(attribute.getBaseName(), value);
            }
        }
    }
    return ldapEntryBuilder.build();
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) StaticUtils.toUTF8String(com.unboundid.util.StaticUtils.toUTF8String)

Aggregations

Attribute (com.unboundid.ldap.sdk.Attribute)18 ArrayList (java.util.ArrayList)6 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)4 Entry (com.unboundid.ldap.sdk.Entry)3 SearchResult (com.unboundid.ldap.sdk.SearchResult)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 AttributeData (org.gluu.persist.model.AttributeData)3 Test (org.junit.Test)3 TeamModel (com.gitblit.models.TeamModel)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)2 AddRequest (com.unboundid.ldap.sdk.AddRequest)2 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)2 LDIFAddChangeRecord (com.unboundid.ldif.LDIFAddChangeRecord)2 AttributeManager (com.zimbra.cs.account.AttributeManager)2 MappingException (org.gluu.persist.exception.mapping.MappingException)2 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)2 SearchException (org.gluu.persist.exception.operation.SearchException)2 LdapAttribute (org.ldaptive.LdapAttribute)2