public User importLDAPUserByScreenName(long companyId, String screenName) throws Exception { long ldapServerId = PortalLDAPUtil.getLdapServerId(companyId, screenName, StringPool.BLANK); SearchResult result = (SearchResult) PortalLDAPUtil.getUser(ldapServerId, companyId, screenName, StringPool.BLANK); if (result == null) { if (_log.isWarnEnabled()) { _log.warn("No user was found in LDAP with screenName " + screenName); } return null; } LdapContext ldapContext = PortalLDAPUtil.getContext(ldapServerId, companyId); String fullUserDN = PortalLDAPUtil.getNameInNamespace(ldapServerId, companyId, result); Attributes attributes = PortalLDAPUtil.getUserAttributes(ldapServerId, companyId, ldapContext, fullUserDN); User user = importLDAPUser(ldapServerId, companyId, ldapContext, attributes, StringPool.BLANK); ldapContext.close(); return user; }
public void importFromLDAP(long ldapServerId, long companyId) throws Exception { if (!LDAPSettingsUtil.isImportEnabled(companyId)) { return; } LdapContext ldapContext = PortalLDAPUtil.getContext(ldapServerId, companyId); if (ldapContext == null) { return; } try { Properties userMappings = LDAPSettingsUtil.getUserMappings(ldapServerId, companyId); Properties userExpandoMappings = LDAPSettingsUtil.getUserExpandoMappings(ldapServerId, companyId); Properties contactMappings = LDAPSettingsUtil.getContactMappings(ldapServerId, companyId); Properties contactExpandoMappings = LDAPSettingsUtil.getContactExpandoMappings(ldapServerId, companyId); Properties groupMappings = LDAPSettingsUtil.getGroupMappings(ldapServerId, companyId); String importMethod = PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_IMPORT_METHOD); if (importMethod.equals(_IMPORT_BY_GROUP)) { importFromLDAPByGroup( ldapServerId, companyId, ldapContext, userMappings, userExpandoMappings, contactMappings, contactExpandoMappings, groupMappings); } else if (importMethod.equals(_IMPORT_BY_USER)) { importFromLDAPByUser( ldapServerId, companyId, ldapContext, userMappings, userExpandoMappings, contactMappings, contactExpandoMappings, groupMappings); } } catch (Exception e) { _log.error("Error importing LDAP users and groups", e); } finally { if (ldapContext != null) { ldapContext.close(); } } }
public User importLDAPUser( long ldapServerId, long companyId, String emailAddress, String screenName) throws Exception { LdapContext ldapContext = null; NamingEnumeration<SearchResult> enu = null; try { String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId); String baseDN = PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_BASE_DN + postfix); ldapContext = PortalLDAPUtil.getContext(ldapServerId, companyId); if (ldapContext == null) { throw new SystemException("Failed to bind to the LDAP server"); } String filter = PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_AUTH_SEARCH_FILTER + postfix); if (_log.isDebugEnabled()) { _log.debug("Search filter before transformation " + filter); } filter = StringUtil.replace( filter, new String[] {"@company_id@", "@email_address@", "@screen_name@"}, new String[] {String.valueOf(companyId), emailAddress, screenName}); if (_log.isDebugEnabled()) { _log.debug("Search filter after transformation " + filter); } Properties userMappings = LDAPSettingsUtil.getUserMappings(ldapServerId, companyId); String userMappingsScreenName = GetterUtil.getString(userMappings.getProperty("screenName")).toLowerCase(); SearchControls searchControls = new SearchControls( SearchControls.SUBTREE_SCOPE, 1, 0, new String[] {userMappingsScreenName}, false, false); enu = ldapContext.search(baseDN, filter, searchControls); if (enu.hasMoreElements()) { if (_log.isDebugEnabled()) { _log.debug("Search filter returned at least one result"); } Binding binding = enu.nextElement(); Attributes attributes = PortalLDAPUtil.getUserAttributes( ldapServerId, companyId, ldapContext, PortalLDAPUtil.getNameInNamespace(ldapServerId, companyId, binding)); return importLDAPUser(ldapServerId, companyId, ldapContext, attributes, StringPool.BLANK); } else { return null; } } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn("Problem accessing LDAP server " + e.getMessage()); } if (_log.isDebugEnabled()) { _log.debug(e, e); } throw new SystemException("Problem accessing LDAP server " + e.getMessage()); } finally { if (enu != null) { enu.close(); } if (ldapContext != null) { ldapContext.close(); } } }