com.xpn.xwiki.plugin.ldap
Class XWikiLDAPUtils

java.lang.Object
  extended by com.xpn.xwiki.plugin.ldap.XWikiLDAPUtils

public class XWikiLDAPUtils
extends Object

LDAP communication tool.

Since:
1.3 M2
Version:
$Id: 3f1c5b08bb17a3e18fdd72cb6feb915f4171d257 $

Constructor Summary
XWikiLDAPUtils(XWikiLDAPConnection connection)
          Create an instance of XWikiLDAPUtils.
 
Method Summary
protected  void addUserToXWikiGroup(String xwikiUserName, String groupName, XWikiContext context)
          Add user name to provided XWiki group.
protected  void createUserFromLDAP(XWikiDocument userProfile, List<XWikiLDAPSearchAttribute> searchAttributes, String ldapDN, String ldapUid, XWikiContext context)
          Create an XWiki user and set all mapped attributes from LDAP to XWiki attributes.
protected  String findInGroup(String userName, Map<String,String> groupMembers, XWikiContext context)
          Locates the user in the Map: either the user is a value or the key starts with the LDAP syntax.
 String[] getAttributeNameTable(XWikiContext context)
           
 String getBaseDN()
           
 Cache<Map<String,String>> getCache(CacheConfiguration configuration, XWikiContext context)
          Deprecated. use getGroupCache(CacheConfiguration, XWikiContext) instead since 4.1M1
 XWikiLDAPConnection getConnection()
           
 Cache<Map<String,String>> getGroupCache(CacheConfiguration configuration, XWikiContext context)
          Get the cache with the provided name for a particular LDAP server.
static CacheConfiguration getGroupCacheConfiguration(XWikiContext context)
           
 Collection<String> getGroupClasses()
           
 Collection<String> getGroupMemberFields()
           
 boolean getGroupMembers(Map<String,String> memberMap, List<String> subgroups, com.novell.ldap.LDAPEntry ldapEntry, XWikiContext context)
          Get all members of a given group based on the groupDN.
 boolean getGroupMembers(String groupDN, Map<String,String> memberMap, List<String> subgroups, List<XWikiLDAPSearchAttribute> searchAttributeList, XWikiContext context)
          Get all members of a given group based on the groupDN.
 boolean getGroupMembers(String userOrGroup, Map<String,String> memberMap, List<String> subgroups, XWikiContext context)
          Get all members of a given group based on the groupDN.
 Map<String,String> getGroupMembers(String groupDN, XWikiContext context)
          Get group members from cache or update it from LDAP if it is not already cached.
 boolean getGroupMembersFromDN(String userOrGroupDN, Map<String,String> memberMap, List<String> subgroups, XWikiContext context)
          Get all members of a given group based on the groupDN.
 boolean getGroupMembersFromFilter(String filter, Map<String,String> memberMap, List<String> subgroups, XWikiContext context)
          Get all members of a given group based on the groupDN.
 boolean getGroupMembersSearchResult(com.novell.ldap.LDAPSearchResults result, Map<String,String> memberMap, List<String> subgroups, XWikiContext context)
          Get all members of a given group based on the the result of a LDAP search.
 String getUidAttributeName()
           
 XWikiDocument getUserProfileByUid(String validXWikiUserName, String ldapUid, XWikiContext context)
           
 String getUserSearchFormatString()
           
 boolean isMemberOfGroup(String memberDN, String groupDN, XWikiContext context)
          Check if provided DN is in provided LDAP group.
 boolean isMemberOfGroups(String memberDN, Collection<String> groupDNList, XWikiContext context)
          Check if provided DN is in one of the provided LDAP groups.
 String isUidInGroup(String userName, String groupDN, XWikiContext context)
          Check if user is in provided LDAP group.
protected  void removeUserFromXWikiGroup(String xwikiUserName, String groupName, XWikiContext context)
          Remove user name from provided XWiki group.
static void resetGroupCache()
          Force to empty the group cache.
 List<XWikiLDAPSearchAttribute> searchUserAttributesByUid(String uid, String[] attributeNameTable)
           
 String searchUserDNByUid(String uid)
           
 void setBaseDN(String baseDN)
           
 void setGroupClasses(Collection<String> groupClasses)
           
 void setGroupMemberFields(Collection<String> groupMemberFields)
           
 void setUidAttributeName(String uidAttributeName)
           
 void setUserSearchFormatString(String fmt)
           
 void syncGroupsMembership(String xwikiUserName, String userDN, Map<String,Set<String>> groupMappings, XWikiContext context)
          Synchronize user XWiki membership with it's LDAP membership.
 void syncUser(XWikiDocument userProfile, List<XWikiLDAPSearchAttribute> searchAttributeListIn, String ldapDn, String ldapUid, XWikiContext context)
          Update or create XWiki user base on LDAP.
protected  void updateUserFromLDAP(XWikiDocument userProfile, List<XWikiLDAPSearchAttribute> searchAttributes, String ldapDN, String ldapUid, XWikiContext context)
          Sets attributes on the user object based on attribute values provided by the LDAP.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XWikiLDAPUtils

public XWikiLDAPUtils(XWikiLDAPConnection connection)
Create an instance of XWikiLDAPUtils.

Parameters:
connection - the XWiki LDAP connection tool.
Method Detail

setUidAttributeName

public void setUidAttributeName(String uidAttributeName)
Parameters:
uidAttributeName - the LDAP attribute containing the identifier for a user.

getUidAttributeName

public String getUidAttributeName()
Returns:
the LDAP attribute containing the identifier for a user.

setBaseDN

public void setBaseDN(String baseDN)
Parameters:
baseDN - the LDAP base DN from where to executes LDAP queries.

getBaseDN

public String getBaseDN()
Returns:
the LDAP base DN from where to executes LDAP queries.

setUserSearchFormatString

public void setUserSearchFormatString(String fmt)
Parameters:
fmt - the user search format string.

getUserSearchFormatString

public String getUserSearchFormatString()
Returns:
the user search format string.

setGroupClasses

public void setGroupClasses(Collection<String> groupClasses)
Parameters:
groupClasses - the different LDAP implementations groups classes names.

getGroupClasses

public Collection<String> getGroupClasses()
Returns:
the different LDAP implementations groups classes names.

setGroupMemberFields

public void setGroupMemberFields(Collection<String> groupMemberFields)
Parameters:
groupMemberFields - the different LDAP implementations groups member property name.

getGroupMemberFields

public Collection<String> getGroupMemberFields()
Returns:
the different LDAP implementations groups member property name.

getCache

@Deprecated
public Cache<Map<String,String>> getCache(CacheConfiguration configuration,
                                                     XWikiContext context)
                                   throws CacheException
Deprecated. use getGroupCache(CacheConfiguration, XWikiContext) instead since 4.1M1

Get the cache with the provided name for a particular LDAP server.

Parameters:
configuration - the configuration to use to create the cache and to find it if it's already created.
context - the XWiki context.
Returns:
the cache.
Throws:
CacheException - error when creating the cache.

getGroupCache

public Cache<Map<String,String>> getGroupCache(CacheConfiguration configuration,
                                               XWikiContext context)
                                        throws CacheException
Get the cache with the provided name for a particular LDAP server.

Parameters:
configuration - the configuration to use to create the cache and to find it if it's already created.
context - the XWiki context.
Returns:
the cache.
Throws:
CacheException - error when creating the cache.
Since:
4.1M1

resetGroupCache

public static void resetGroupCache()
Force to empty the group cache.

Since:
4.1M1

getConnection

public XWikiLDAPConnection getConnection()
Returns:
get XWikiLDAPConnection.

getGroupMembers

public boolean getGroupMembers(String groupDN,
                               Map<String,String> memberMap,
                               List<String> subgroups,
                               List<XWikiLDAPSearchAttribute> searchAttributeList,
                               XWikiContext context)
Get all members of a given group based on the groupDN. If the group contains subgroups get these members as well. Retrieve an identifier for each member.

Parameters:
groupDN - the group to retrieve the members of and scan for subgroups.
memberMap - the result: maps DN to member id.
subgroups - all the subgroups identified.
searchAttributeList - the groups members found in LDAP search.
context - the XWiki context.
Returns:
whether the groupDN is actually a group.

getGroupMembers

public boolean getGroupMembers(Map<String,String> memberMap,
                               List<String> subgroups,
                               com.novell.ldap.LDAPEntry ldapEntry,
                               XWikiContext context)
                        throws com.novell.ldap.LDAPException
Get all members of a given group based on the groupDN. If the group contains subgroups get these members as well. Retrieve an identifier for each member.

Parameters:
memberMap - the result: maps DN to member id.
subgroups - all the subgroups identified.
ldapEntry - the ldap entry returned by a search members found in LDAP search.
context - the XWiki context.
Returns:
whether the groupDN is actually a group.
Throws:
com.novell.ldap.LDAPException - error when parsing the provided LDAP entry
Since:
3.3M1

getGroupMembers

public boolean getGroupMembers(String userOrGroup,
                               Map<String,String> memberMap,
                               List<String> subgroups,
                               XWikiContext context)
Get all members of a given group based on the groupDN. If the group contains subgroups get these members as well. Retrieve an identifier for each member.

Parameters:
userOrGroup - the group to retrieve the members of and scan for subgroups. Can be
  • a group DN
  • a user DN
  • a group id
  • a user id
memberMap - the result: maps DN to member id.
subgroups - all the subgroups identified.
context - the XWiki context.
Returns:
whether the identifier is actually a group.

getGroupMembersFromDN

public boolean getGroupMembersFromDN(String userOrGroupDN,
                                     Map<String,String> memberMap,
                                     List<String> subgroups,
                                     XWikiContext context)
Get all members of a given group based on the groupDN. If the group contains subgroups get these members as well. Retrieve an identifier for each member.

Parameters:
userOrGroupDN - the group DN to retrieve the members from or the user DN to add in the members map.
memberMap - the result: maps DN to member id.
subgroups - all the subgroups identified.
context - the XWiki context.
Returns:
whether the provided DN is actually a group or not.

getGroupMembersFromFilter

public boolean getGroupMembersFromFilter(String filter,
                                         Map<String,String> memberMap,
                                         List<String> subgroups,
                                         XWikiContext context)
Get all members of a given group based on the groupDN. If the group contains subgroups get these members as well. Retrieve an identifier for each member.

Parameters:
filter - the LDAP filter to search with.
memberMap - the result: maps DN to member id.
subgroups - all the subgroups identified.
context - the XWiki context.
Returns:
whether the provided DN is actually a group or not.

getGroupMembersSearchResult

public boolean getGroupMembersSearchResult(com.novell.ldap.LDAPSearchResults result,
                                           Map<String,String> memberMap,
                                           List<String> subgroups,
                                           XWikiContext context)
Get all members of a given group based on the the result of a LDAP search. If the group contains subgroups get these members as well. Retrieve an identifier for each member.

Parameters:
result - the result of a LDAP search.
memberMap - the result: maps DN to member id.
subgroups - all the subgroups identified.
context - the XWiki context.
Returns:
whether the provided DN is actually a group or not.

getGroupMembers

public Map<String,String> getGroupMembers(String groupDN,
                                          XWikiContext context)
                                   throws XWikiException
Get group members from cache or update it from LDAP if it is not already cached.

Parameters:
groupDN - the name of the group.
context - the XWiki context.
Returns:
the members of the group.
Throws:
XWikiException - error when getting the group cache.

isMemberOfGroup

public boolean isMemberOfGroup(String memberDN,
                               String groupDN,
                               XWikiContext context)
                        throws XWikiException
Check if provided DN is in provided LDAP group.

Parameters:
memberDN - the DN to find in the provided group.
groupDN - the DN of the group where to search.
context - the XWiki context.
Returns:
true if provided members in the provided group.
Throws:
XWikiException - error when searching for group members.

isMemberOfGroups

public boolean isMemberOfGroups(String memberDN,
                                Collection<String> groupDNList,
                                XWikiContext context)
                         throws XWikiException
Check if provided DN is in one of the provided LDAP groups.

Parameters:
memberDN - the DN to find in the provided groups.
groupDNList - the list of DN of the groups where to search.
context - the XWiki context.
Returns:
true if provided members in one of the provided groups.
Throws:
XWikiException - error when searching for group members.

getGroupCacheConfiguration

public static CacheConfiguration getGroupCacheConfiguration(XWikiContext context)
Parameters:
context - the XWiki context used to get cache configuration.
Returns:
the configuration for the LDAP groups cache.

findInGroup

protected String findInGroup(String userName,
                             Map<String,String> groupMembers,
                             XWikiContext context)
Locates the user in the Map: either the user is a value or the key starts with the LDAP syntax.

Parameters:
userName - the name of the user.
groupMembers - the members of LDAP group.
context - the XWiki context.
Returns:
the full user name.

isUidInGroup

public String isUidInGroup(String userName,
                           String groupDN,
                           XWikiContext context)
                    throws XWikiException
Check if user is in provided LDAP group.

Parameters:
userName - the user name.
groupDN - the LDAP group DN.
context - the XWiki context.
Returns:
LDAP user's DN if the user is in the LDAP group, null otherwise.
Throws:
XWikiException - error when getting the group cache.

searchUserAttributesByUid

public List<XWikiLDAPSearchAttribute> searchUserAttributesByUid(String uid,
                                                                String[] attributeNameTable)
Parameters:
uid - the unique identifier of the user in the LDAP server.
attributeNameTable - the names of the LDAP user attributes to query.
Returns:
the found LDAP attributes.
Since:
1.6M2

searchUserDNByUid

public String searchUserDNByUid(String uid)
Parameters:
uid - the unique identifier of the user in the LDAP server.
Returns:
the user DN, return null if no user was found.
Since:
1.6M2

syncUser

public void syncUser(XWikiDocument userProfile,
                     List<XWikiLDAPSearchAttribute> searchAttributeListIn,
                     String ldapDn,
                     String ldapUid,
                     XWikiContext context)
              throws XWikiException
Update or create XWiki user base on LDAP.

Parameters:
userProfile - the name of the user.
ldapDn - the LDAP user DN.
ldapUid - the UID field.
searchAttributeListIn - the attributes of the LDAP user.
context - the XWiki context.
Throws:
XWikiException - error when updating or creating XWiki user.

syncGroupsMembership

public void syncGroupsMembership(String xwikiUserName,
                                 String userDN,
                                 Map<String,Set<String>> groupMappings,
                                 XWikiContext context)
                          throws XWikiException
Synchronize user XWiki membership with it's LDAP membership.

Parameters:
xwikiUserName - the name of the user.
userDN - the LDAP DN of the user.
groupMappings - the mapping between XWiki groups names and LDAP groups names.
context - the XWiki context.
Throws:
XWikiException - error when synchronizing user membership.

getAttributeNameTable

public String[] getAttributeNameTable(XWikiContext context)
Parameters:
context - the XWiki context.
Returns:
the LDAP user attributes names.

createUserFromLDAP

protected void createUserFromLDAP(XWikiDocument userProfile,
                                  List<XWikiLDAPSearchAttribute> searchAttributes,
                                  String ldapDN,
                                  String ldapUid,
                                  XWikiContext context)
                           throws XWikiException
Create an XWiki user and set all mapped attributes from LDAP to XWiki attributes.

Parameters:
userProfile - the XWiki user profile.
searchAttributes - the attributes.
ldapDN - the LDAP DN of the user.
ldapUid - the LDAP unique id of the user.
context - the XWiki context.
Throws:
XWikiException - error when creating XWiki user.

updateUserFromLDAP

protected void updateUserFromLDAP(XWikiDocument userProfile,
                                  List<XWikiLDAPSearchAttribute> searchAttributes,
                                  String ldapDN,
                                  String ldapUid,
                                  XWikiContext context)
                           throws XWikiException
Sets attributes on the user object based on attribute values provided by the LDAP.

Parameters:
userProfile - the XWiki user profile document.
searchAttributes - the attributes of the LDAP user to update.
ldapDN - the DN of the LDAP user to update
ldapUid - value of the unique identifier for the user to update.
context - the XWiki context.
Throws:
XWikiException - error when updating XWiki user.

addUserToXWikiGroup

protected void addUserToXWikiGroup(String xwikiUserName,
                                   String groupName,
                                   XWikiContext context)
Add user name to provided XWiki group.

Parameters:
xwikiUserName - the full name of the user.
groupName - the name of the group.
context - the XWiki context.

removeUserFromXWikiGroup

protected void removeUserFromXWikiGroup(String xwikiUserName,
                                        String groupName,
                                        XWikiContext context)
Remove user name from provided XWiki group.

Parameters:
xwikiUserName - the full name of the user.
groupName - the name of the group.
context - the XWiki context.

getUserProfileByUid

public XWikiDocument getUserProfileByUid(String validXWikiUserName,
                                         String ldapUid,
                                         XWikiContext context)
                                  throws XWikiException
Parameters:
validXWikiUserName - the valid XWiki name of the user to get the profile for. Used for fast lookup relying on the document cache before doing a database search.
ldapUid - the UID to get the profile for
context - the XWiki context
Returns:
the XWiki document of the user with the passed UID
Throws:
XWikiException - when a problem occurs while retrieving the user profile


Copyright © 2004-2013 XWiki. All Rights Reserved.