Package org.sakaiproject.unboundid
Class SimpleLdapAttributeMapper
- java.lang.Object
-
- org.sakaiproject.unboundid.SimpleLdapAttributeMapper
-
- All Implemented Interfaces:
LdapAttributeMapper
- Direct Known Subclasses:
EmailAddressDerivingLdapAttributeMapper,MultipleEmailLdapAttributeMapper
public class SimpleLdapAttributeMapper extends Object implements LdapAttributeMapper
Implements LDAP attribute mappings and filter generations using an attribute map keyed by constants inAttributeMappingConstants. The strategy for calculating Sakai user type can be injected as aUserTypeMapper. This strategy defaults toEmptyStringUserTypeMapper, which will match <= 2.3.0 OOTB behavior.- Author:
- Dan McCallum, Unicon Inc
-
-
Constructor Summary
Constructors Constructor Description SimpleLdapAttributeMapper()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringescapeSearchFilterTerm(String unescapedTerm)Scrubs the given search filter term (i.e. a value to be matched, or not, in a search predicate) for reserved characters.StringgetAttributeMapping(String key)Map the given logical attribute name to a physical attribute name.Map<String,String>getAttributeMappings()Returns a direct reference to the currently cached mappings.StringgetFindUserByAidFilter(String aid)Output a filter string for searching the directory with the specified user aid as a key.StringgetFindUserByCrossAttributeSearchFilter(String unescapedCriteria)Builds a filter to perform a wildcard search for criteria in uid, email, first name or last nameStringgetFindUserByEidFilter(String eid)Builds a filter of the form <login-attr>=<eid>StringgetFindUserByEmailFilter(String emailAddr)Builds a filter of the form <email-attr>=<emailAddr>StringgetManyUsersInOneSearch(Set<String> criteria)Builds a filter to a uid search against many users at once For reference, the LDAP search filter is of the form: "(|(uid=sample.user)(uid=john.doe)(uid=jane.smith))"protected Map<String,Collection<String>>getReverseAttributeMap()Collection<String>getReverseAttributeMappings(String physicalAttrName)Access the configured logical names associated with the given physical attribute name.String[]getSearchResultAttributes()Implemented to return the current values of {link {@link #getAttributeMappings().values()} as a String array.StringgetUserBindDn(LdapUserData userData)Determine the DN to which to bind when executing an authentication attempt for the given user.protected StringgetUserDataDn(LdapUserData userData)UserTypeMappergetUserTypeMapper()Access the strategy for calculating the Sakai user type given aLDAPEntryMap<String,MessageFormat>getValueMappings()voidinit()Completes configuration of this instance.protected voidmapLdapAttributeOntoUserData(com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPAttribute attribute, LdapUserData userData, String logicalAttrName)A delegate ofmapLdapAttributeOntoUserData(LDAPAttribute, LdapUserData, Collection)that allows for discrete handling of each logical attribute name associated with the givenLDAPAttributeprotected voidmapLdapAttributeOntoUserData(com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPAttribute attribute, LdapUserData userData, Collection<String> logicalAttrNames)Map the givenLDAPAttributeonto the givenLdapUserData.voidmapLdapEntryOntoUserData(com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPEntry ldapEntry, LdapUserData userData)PerformsLDAPEntry-to- attribute mappings.protected StringmapLdapEntryToSakaiUserType(com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPEntry ldapEntry)Passes the givenLDAPEntryand a reference to thisSimpleLdapAttributeMappertoUserTypeMapper.mapLdapEntryToSakaiUserType(LDAPEntry, LdapAttributeMapper).voidmapUserDataOntoUserEdit(LdapUserData userData, org.sakaiproject.user.api.UserEdit userEdit)StraightforwardLdapUserDatatoUserEditfield-to-field mapping, including properties.protected Map<String,Collection<String>>reverseAttributeMap(Map<String,String> toReverse)Creates a reverse lookup map of a given attribute map's values.voidsetAttributeMappings(Map<String,String> attributeMappings)Caches the given Map reference and takes a snapshot of the values therein for future use bygetSearchResultAttributes().protected voidsetUserDataDn(com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPEntry entry, LdapUserData targetUserData)voidsetUserTypeMapper(UserTypeMapper userTypeMapper)Assign the strategy for calculating the Sakai user type given aLDAPEntryvoidsetValueMappings(Map<String,MessageFormat> valueMappings)protected StringusePreferredFirstName(LdapUserData userData)Determines if a user has a preferredFirstName set and if so, returns it for use.
-
-
-
Method Detail
-
init
public void init()
Completes configuration of this instance.Initializes internal mappings to a copy of
AttributeMappingConstants.DEFAULT_ATTR_MAPPINGSif the current map is empty. Initializes user type mapping strategy to aEmptyStringUserTypeMapperif no strategy has been specified.This defaulting enables UDP config forward-compatibility.
- Specified by:
initin interfaceLdapAttributeMapper
-
getFindUserByEmailFilter
public String getFindUserByEmailFilter(String emailAddr)
Builds a filter of the form <email-attr>=<emailAddr>- Specified by:
getFindUserByEmailFilterin interfaceLdapAttributeMapper- Parameters:
emailAddr- an email address to search on- Returns:
- an LDAP search filter
-
getFindUserByEidFilter
public String getFindUserByEidFilter(String eid)
Builds a filter of the form <login-attr>=<eid>- Specified by:
getFindUserByEidFilterin interfaceLdapAttributeMapper- Parameters:
eid- a user eid to search on- Returns:
- an LDAP search filter
-
getFindUserByAidFilter
public String getFindUserByAidFilter(String aid)
Description copied from interface:LdapAttributeMapperOutput a filter string for searching the directory with the specified user aid as a key.- Specified by:
getFindUserByAidFilterin interfaceLdapAttributeMapper- Parameters:
aid- a user authentication id.- Returns:
- an LDAP search filter
-
mapLdapEntryOntoUserData
public void mapLdapEntryOntoUserData(com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPEntry ldapEntry, LdapUserData userData)PerformsLDAPEntry-to- attribute mappings. Assigns the givenLDAPEntry's DN to theLdapUserDataas a property keyed byAttributeMappingConstants.USER_DN_PROPERTY. Then iterates overLDAPEntry.getAttributeSet(), handing each attribute tomapLdapAttributeOntoUserData(LDAPAttribute, LdapUserData, Collection). Then enforces the preferred first name field, if it exists. Finally, assigns a "type" to theLdapUserDataas defined bymapLdapEntryToSakaiUserType(LDAPEntry).- Specified by:
mapLdapEntryOntoUserDatain interfaceLdapAttributeMapper- Parameters:
ldapEntry- the user's directory entryuserData- targetLdapUserData- See Also:
UserTypeMapper
-
getUserBindDn
public String getUserBindDn(LdapUserData userData)
Description copied from interface:LdapAttributeMapperDetermine the DN to which to bind when executing an authentication attempt for the given user. An invocation implies that the DN can be derived from attributes already mapped onto the givenLdapUserDataby thisLdapAttributeMapper. For example, the mapper could have cached the DN in the user's property map, or the bind DN could be reliably calculated from a combination of the user'seidand some other configured RDN string.- Specified by:
getUserBindDnin interfaceLdapAttributeMapper- Parameters:
userData- a mapped collection of user attributes from which to derive a bindable DN. Should not benull- Returns:
- a bindable DN derived from
userDataornull if the DN is not known.
-
getUserDataDn
protected String getUserDataDn(LdapUserData userData)
-
setUserDataDn
protected void setUserDataDn(com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPEntry entry, LdapUserData targetUserData)
-
mapLdapAttributeOntoUserData
protected void mapLdapAttributeOntoUserData(com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPAttribute attribute, LdapUserData userData, Collection<String> logicalAttrNames)Map the givenLDAPAttributeonto the givenLdapUserData. Client can specify the logical attribute name(s) which have been configured for the givenLDAPAttribute. This implementation has specific handling for the following logical attribute names:AttributeMappingConstants.LOGIN_ATTR_MAPPING_KEY-LdapUserData.setEid(String)AttributeMappingConstants.FIRST_NAME_ATTR_MAPPING_KEY-LdapUserData.setFirstName(String)AttributeMappingConstants.LAST_NAME_ATTR_MAPPING_KEY-LdapUserData.setLastName(String)AttributeMappingConstants.EMAIL_ATTR_MAPPING_KEY-LdapUserData.setEmail(String)
logicalAttrNameswill be mapped ontouserDataas a property using the logical attribute name as a key.- Parameters:
attribute- theLDAPAttributeto mapuserData- the targetLdapUserDatainstancelogicalAttrNames- logical name(s) of theattribute. May be null or empty, indicating no configured logical name(s).
-
mapLdapAttributeOntoUserData
protected void mapLdapAttributeOntoUserData(com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPAttribute attribute, LdapUserData userData, String logicalAttrName)A delegate ofmapLdapAttributeOntoUserData(LDAPAttribute, LdapUserData, Collection)that allows for discrete handling of each logical attribute name associated with the givenLDAPAttribute- Parameters:
attribute-userData-logicalAttrName-
-
mapLdapEntryToSakaiUserType
protected String mapLdapEntryToSakaiUserType(com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPEntry ldapEntry)
Passes the givenLDAPEntryand a reference to thisSimpleLdapAttributeMappertoUserTypeMapper.mapLdapEntryToSakaiUserType(LDAPEntry, LdapAttributeMapper). By default, this will just return an empty String.- Parameters:
ldapEntry- theLDAPEntryto map- Returns:
- a String representing a Sakai user type.
nulls and empty Strings are possible.
-
mapUserDataOntoUserEdit
public void mapUserDataOntoUserEdit(LdapUserData userData, org.sakaiproject.user.api.UserEdit userEdit)
StraightforwardLdapUserDatatoUserEditfield-to-field mapping, including properties.- Specified by:
mapUserDataOntoUserEditin interfaceLdapAttributeMapper- Parameters:
userData- a non-null user cache entryuserEdit- a non-null user domain object
-
escapeSearchFilterTerm
public String escapeSearchFilterTerm(String unescapedTerm)
Description copied from interface:LdapAttributeMapperScrubs the given search filter term (i.e. a value to be matched, or not, in a search predicate) for reserved characters. I.e. protects against query injection.- Specified by:
escapeSearchFilterTermin interfaceLdapAttributeMapper- Parameters:
unescapedTerm- The string value to be scrubbed- Returns:
nullif the received String is null, otherwise a copy of the received String with reserved characters escaped.
-
getAttributeMapping
public String getAttributeMapping(String key)
Map the given logical attribute name to a physical attribute name.- Specified by:
getAttributeMappingin interfaceLdapAttributeMapper- Parameters:
key- the logical attribute name- Returns:
- the corresponding physical attribute name, or null if no mapping exists.
-
getReverseAttributeMappings
public Collection<String> getReverseAttributeMappings(String physicalAttrName)
Access the configured logical names associated with the given physical attribute name. May returnnull.- Parameters:
physicalAttrName- a physical LDAP attribute name to reverse map to zero or more logical attribute names- Returns:
- a collection of logical attribute names; may be
nullor empty.
-
getReverseAttributeMap
protected Map<String,Collection<String>> getReverseAttributeMap()
-
getSearchResultAttributes
public String[] getSearchResultAttributes()
Implemented to return the current values of {link {@link #getAttributeMappings().values()} as a String array.- Specified by:
getSearchResultAttributesin interfaceLdapAttributeMapper- Returns:
- an array of directory attribute names
-
getAttributeMappings
public Map<String,String> getAttributeMappings()
Returns a direct reference to the currently cached mappings. Note that if this map is modified, the next call togetSearchResultAttributes()may return stale values.- Specified by:
getAttributeMappingsin interfaceLdapAttributeMapper- Returns:
- the current attribute map.
-
setAttributeMappings
public void setAttributeMappings(Map<String,String> attributeMappings)
Caches the given Map reference and takes a snapshot of the values therein for future use bygetSearchResultAttributes().- Specified by:
setAttributeMappingsin interfaceLdapAttributeMapper- Parameters:
attributeMappings- the attribute map.- See Also:
getAttributeMappings()
-
reverseAttributeMap
protected Map<String,Collection<String>> reverseAttributeMap(Map<String,String> toReverse)
Creates a reverse lookup map of a given attribute map's values. That is, creates a map of physical to logical LDAP attribute names. Since a multiple logical names may point to a single physical name, values in this map are actuallyCollection's.Protected access control mainly to enable testing
- Parameters:
toReverse-- Returns:
-
getUserTypeMapper
public UserTypeMapper getUserTypeMapper()
Access the strategy for calculating the Sakai user type given aLDAPEntry
-
setUserTypeMapper
public void setUserTypeMapper(UserTypeMapper userTypeMapper)
Assign the strategy for calculating the Sakai user type given aLDAPEntry
-
usePreferredFirstName
protected String usePreferredFirstName(LdapUserData userData)
Determines if a user has a preferredFirstName set and if so, returns it for use. Otherwise, returns their firstName as normal.- Parameters:
userData- theLdapUserDatafor the user- Returns:
- a String of the user's first name.
-
getFindUserByCrossAttributeSearchFilter
public String getFindUserByCrossAttributeSearchFilter(String unescapedCriteria)
Description copied from interface:LdapAttributeMapperBuilds a filter to perform a wildcard search for criteria in uid, email, first name or last nameIn order to minimise hitting the limits of searches, this only performs a wildcard match on anything after the supplied criteria.
For example, a search for 'john' will match 'john' and 'johnson' but not 'gudjohnsen'.For reference, the LDAP search filter is of the form: "(|(uid=criteria*)(mail=criteria*)(givenName=criteria*)(sn=criteria*))"
- Specified by:
getFindUserByCrossAttributeSearchFilterin interfaceLdapAttributeMapper- Returns:
- the formatted search filter
-
getManyUsersInOneSearch
public String getManyUsersInOneSearch(Set<String> criteria)
Description copied from interface:LdapAttributeMapperBuilds a filter to a uid search against many users at once For reference, the LDAP search filter is of the form: "(|(uid=sample.user)(uid=john.doe)(uid=jane.smith))"- Specified by:
getManyUsersInOneSearchin interfaceLdapAttributeMapper- Returns:
- the formatted search filter
-
getValueMappings
public Map<String,MessageFormat> getValueMappings()
- Returns:
- A Map of message formats used for extracting values from LDAP data.
-
setValueMappings
public void setValueMappings(Map<String,MessageFormat> valueMappings)
- Parameters:
valueMappings- A Map of message formats used for extracting values from LDAP data.
-
-