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();
}
}
}
}
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;
}
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");
}
}
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);
}
}
}
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();
}
Aggregations