Skip to content
Snippets Groups Projects
Commit 68b2bc8a authored by tobias.friedrich's avatar tobias.friedrich
Browse files

MW-1563: Dynamically resolve manager and assistant/secretary attributes

parent 73116b3f
No related branches found
No related tags found
No related merge requests found
......@@ -73,7 +73,6 @@ ad:
instant_messenger2 :
telephone_ip : ipPhone
telephone_isdn : internationaliSDNNumber
manager_name :
marital_status :
cellular_telephone1 : mobile
cellular_telephone2 :
......@@ -130,7 +129,6 @@ ad:
userfield20 :
city_business : l
country_business : co
assistant_name :
telephone_primary :
categories :
title :
......@@ -152,12 +150,18 @@ ad:
# == Misc Mappings =====================================================
# Distribution list members are resolved dynamically using the DNs found in the mapped LDAP property.
# Distribution list members are resolved dynamically using the DNs found in the mapped LDAP attribute. Alternatively, if the
# attribute value does not denote a DN reference, the value is assumed to be the plain email address of the member.
distributionlist : member
# Special mapping where the value is evaluated using a string comparison with, or the existence of the attribute value.
markasdistributionlist: objectClass=group
# The values for the for assistant- and manager name mappings are either used as-is, or get resolved dynamically using the DNs found
# in the mapped LDAP attribute.
assistant_name : secretary
manager_name : manager
# Contact image, binary format is expected.
image1 : jpegPhoto
......@@ -222,7 +226,6 @@ openldap:
instant_messenger2 :
telephone_ip :
telephone_isdn : internationaliSDNNumber
manager_name :
marital_status :
cellular_telephone1 : mobile
cellular_telephone2 :
......@@ -279,7 +282,6 @@ openldap:
userfield20 :
city_business : l
country_business : country
assistant_name :
telephone_primary :
categories :
title : title
......@@ -308,6 +310,11 @@ openldap:
# Special mapping where the value is evaluated using a string comparison with, or the existence of the attribute value.
markasdistributionlist: objectClass=posixGroup
# The values for the for assistant- and manager name mappings are either used as-is, or get resolved dynamically using the DNs found
# in the mapped LDAP attribute.
assistant_name : secretary
manager_name : manager
# Contact image, binary format is expected.
image1 : jpegPhoto
......
......@@ -36,6 +36,7 @@ import static com.openexchange.contact.provider.ldap.Utils.applyFolderExclusions
import static com.openexchange.contact.provider.ldap.Utils.applyPagedResultsControl;
import static com.openexchange.contact.provider.ldap.Utils.applyRangeAndLimit;
import static com.openexchange.contact.provider.ldap.Utils.collectAttributes;
import static com.openexchange.contact.provider.ldap.Utils.generateDisplayName;
import static com.openexchange.contact.provider.ldap.Utils.getAutocompleteFilter;
import static com.openexchange.contact.provider.ldap.Utils.getContactFieldTerm;
import static com.openexchange.contact.provider.ldap.Utils.getObjectIdsPerFolderId;
......@@ -43,6 +44,7 @@ import static com.openexchange.contact.provider.ldap.Utils.getOrFilter;
import static com.openexchange.contact.provider.ldap.Utils.getSizeLimit;
import static com.openexchange.contact.provider.ldap.Utils.getSortOptions;
import static com.openexchange.contact.provider.ldap.Utils.resolveUserAndContextId;
import static com.openexchange.contact.provider.ldap.config.ConfigUtils.DISPLAYNAME_FIELDS;
import static com.openexchange.contact.provider.ldap.config.ConfigUtils.DISTLISTMEMBER_FIELDS;
import static com.openexchange.contact.provider.ldap.config.ConfigUtils.PROPERTY_AUTOCOMPLETE_FIELDS;
import static com.openexchange.contact.provider.ldap.config.ConfigUtils.PROPERTY_MINIMUM_SEARCH_CHARACTERS;
......@@ -104,6 +106,7 @@ import com.openexchange.groupware.container.DistributionListEntryObject;
import com.openexchange.groupware.container.FolderObject;
import com.openexchange.groupware.search.ContactsSearchObject;
import com.openexchange.java.Enums;
import com.openexchange.java.Strings;
import com.openexchange.java.util.UUIDs;
import com.openexchange.ldap.common.LDAPConnectionProvider;
import com.openexchange.ldap.common.LDAPService;
......@@ -555,11 +558,50 @@ public class LdapContactsAccess extends ReadOnlyFolderContactsAccess implements
* resolve distribution list members as needed
*/
if (contact.getMarkAsDistribtuionlist()) {
contact.setDistributionList(resolveMembers(searchResult.getConnectionProvider(), contact.getDistributionList()));
if (null == fields || com.openexchange.tools.arrays.Arrays.contains(fields, ContactField.DISTRIBUTIONLIST)) {
contact.setDistributionList(resolveMembers(searchResult.getConnectionProvider(), contact.getDistributionList()));
} else {
contact.removeDistributionLists();
}
}
/*
* resolve manager / assistant as needed
*/
if (Strings.isNotEmpty(contact.getManagerName())) {
contact.setManagerName(resolveToDisplayName(searchResult.getConnectionProvider(), contact.getManagerName()));
}
if (Strings.isNotEmpty(contact.getAssistantName())) {
contact.setAssistantName(resolveToDisplayName(searchResult.getConnectionProvider(), contact.getAssistantName()));
}
return contact;
}
private String resolveToDisplayName(LDAPConnectionProvider connectionProvider, String dn) {
if (null == dn || false == DN.isValidDN(dn)) {
return dn;
}
Set<String> collectedAttributes = new HashSet<String>(DISPLAYNAME_FIELDS.length);
config.getMapper().collectAttributes(DISPLAYNAME_FIELDS, collectedAttributes);
String[] attributes = collectedAttributes.toArray(new String[collectedAttributes.size()]);
LDAPConnection connection = null;
try {
connection = connectionProvider.getConnection(session);
/*
* get referenced entry and convert to representative contact
*/
SearchResultEntry resultEntry = connection.getEntry(dn, attributes);
if (null == resultEntry) {
throw OXException.notFound(dn);
}
return generateDisplayName(getContact(new LdapSearchResult(connectionProvider, resultEntry), dn, DISPLAYNAME_FIELDS, false));
} catch (LDAPException | OXException e) {
warnings.add(LdapContactsExceptionCodes.CANT_RESOLVE_ENTRY.create(e, dn));
} finally {
connectionProvider.back(connection);
}
return dn;
}
private DistributionListEntryObject[] resolveMembers(LDAPConnectionProvider connectionProvider, DistributionListEntryObject[] memberDNs) throws OXException {
if (null == memberDNs || 0 == memberDNs.length) {
return memberDNs;
......@@ -603,7 +645,7 @@ public class LdapContactsAccess extends ReadOnlyFolderContactsAccess implements
resolvedMembers.add(asMember(memberDN.getDisplayname()));
}
} catch (OXException | LDAPException e) {
warnings.add(LdapContactsExceptionCodes.CANT_RESOLVE_MEMBER.create(e, memberDN.getDisplayname()));
warnings.add(LdapContactsExceptionCodes.CANT_RESOLVE_ENTRY.create(e, memberDN.getDisplayname()));
}
}
} finally {
......
......@@ -59,9 +59,9 @@ public enum LdapContactsExceptionCodes implements DisplayableOXExceptionCode {
UNEXPECTED_ERROR("An unexpected error occurred: %1$s", Category.CATEGORY_ERROR, 4),
/**
* <li>Entry %1$s can't be resolved as distribution list member</li>
* <li>Entry %1$s can't be resolved</li>
*/
CANT_RESOLVE_MEMBER("Entry %1$s can't be resolved as distribution list member", Category.CATEGORY_TRUNCATED, 5),
CANT_RESOLVE_ENTRY("Entry %1$s can't be resolved", Category.CATEGORY_TRUNCATED, 5),
/**
* <li>The search term '%1$s' has been ignored in the search.</li>
......
......@@ -54,6 +54,7 @@ import com.openexchange.contact.provider.ldap.mapping.LdapMapping;
import com.openexchange.context.ContextService;
import com.openexchange.exception.OXException;
import com.openexchange.groupware.contact.ContactExceptionCodes;
import com.openexchange.groupware.contact.ContactUtil;
import com.openexchange.groupware.contact.helpers.ContactField;
import com.openexchange.groupware.container.Contact;
import com.openexchange.groupware.contexts.Context;
......@@ -135,6 +136,30 @@ public class Utils {
return contact;
}
/**
* Generates a display name for the supplied contact, consisting of it's constructed display name and email address.
*
* @param contact The contact to generate the display name for
* @return The display name
*/
static String generateDisplayName(Contact contact) {
String email = contact.getEmail1();
if (Strings.isEmpty(email)) {
email = contact.getEmail2();
if (Strings.isEmpty(email))
email = contact.getEmail3();
}
ContactUtil.generateDisplayName(contact);
String displayName = contact.getDisplayName();
if (Strings.isEmpty(email)) {
return displayName;
}
if (Strings.isEmpty(displayName)) {
return email;
}
return String.format("%1$s <%2$s>", displayName, email);
}
static void collectAttributes(Filter filter, Collection<String> attributes) {
String attributeName = filter.getAttributeName();
if (null != attributeName) {
......
......@@ -69,6 +69,12 @@ public class ConfigUtils {
public static final ContactField[] DISTLISTMEMBER_FIELDS = {
ContactField.EMAIL1, ContactField.EMAIL2, ContactField.EMAIL3, ContactField.FOLDER_ID, ContactField.OBJECT_ID, ContactField.DISPLAY_NAME, ContactField.SUR_NAME, ContactField.GIVEN_NAME
};
/** The contact fields to use the mapped attributes from when resolving referenced manager/assistant names */
public static final ContactField[] DISPLAYNAME_FIELDS = {
ContactField.EMAIL1, ContactField.EMAIL2, ContactField.EMAIL3, ContactField.DISPLAY_NAME, ContactField.SUR_NAME, ContactField.GIVEN_NAME, ContactField.COMPANY
};
static Map<String, Object> asMap(Object yaml) throws OXException {
if (null == yaml) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment