mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Heinous merge from HEAD. Seems to basically work. Be on guard however.
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@4137 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -30,17 +30,22 @@ import javax.naming.directory.InitialDirContext;
|
||||
|
||||
import org.alfresco.repo.security.authentication.AuthenticationException;
|
||||
import org.alfresco.util.ApplicationContextHelper;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
public class LDAPInitialDirContextFactoryImpl implements LDAPInitialDirContextFactory
|
||||
public class LDAPInitialDirContextFactoryImpl implements LDAPInitialDirContextFactory, InitializingBean
|
||||
{
|
||||
private static final Log logger = LogFactory.getLog(LDAPInitialDirContextFactoryImpl.class);
|
||||
|
||||
private Map<String, String> initialDirContextEnvironment = Collections.<String, String> emptyMap();
|
||||
|
||||
static
|
||||
{
|
||||
System.setProperty("javax.security.auth.useSubjectCredentialsOnly", "false");
|
||||
}
|
||||
|
||||
|
||||
public LDAPInitialDirContextFactoryImpl()
|
||||
{
|
||||
super();
|
||||
@@ -87,11 +92,22 @@ public class LDAPInitialDirContextFactoryImpl implements LDAPInitialDirContextFa
|
||||
{
|
||||
throw new AuthenticationException("Null user name provided.");
|
||||
}
|
||||
|
||||
if (principal.length() == 0)
|
||||
{
|
||||
throw new AuthenticationException("Empty user name provided.");
|
||||
}
|
||||
|
||||
if (credentials == null)
|
||||
{
|
||||
throw new AuthenticationException("No credentials provided.");
|
||||
}
|
||||
|
||||
if (credentials.length() == 0)
|
||||
{
|
||||
throw new AuthenticationException("Empty credentials provided.");
|
||||
}
|
||||
|
||||
Hashtable<String, String> env = new Hashtable<String, String>(initialDirContextEnvironment.size());
|
||||
env.putAll(initialDirContextEnvironment);
|
||||
env.put(Context.SECURITY_PRINCIPAL, principal);
|
||||
@@ -187,4 +203,108 @@ public class LDAPInitialDirContextFactoryImpl implements LDAPInitialDirContextFa
|
||||
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception
|
||||
{
|
||||
// Check Anonymous bind
|
||||
|
||||
Hashtable<String, String> env = new Hashtable<String, String>(initialDirContextEnvironment.size());
|
||||
env.putAll(initialDirContextEnvironment);
|
||||
env.remove(Context.SECURITY_PRINCIPAL);
|
||||
env.remove(Context.SECURITY_CREDENTIALS);
|
||||
try
|
||||
{
|
||||
new InitialDirContext(env);
|
||||
|
||||
logger.warn("LDAP server supports anonymous bind " + env.get(Context.PROVIDER_URL));
|
||||
}
|
||||
catch (javax.naming.AuthenticationException ax)
|
||||
{
|
||||
|
||||
}
|
||||
catch (NamingException nx)
|
||||
{
|
||||
throw new AuthenticationException("Unable to connect to LDAP Server; check LDAP configuration", nx);
|
||||
}
|
||||
|
||||
// Simple DN and password
|
||||
|
||||
env = new Hashtable<String, String>(initialDirContextEnvironment.size());
|
||||
env.putAll(initialDirContextEnvironment);
|
||||
env.put(Context.SECURITY_PRINCIPAL, "daftAsABrush");
|
||||
env.put(Context.SECURITY_CREDENTIALS, "daftAsABrush");
|
||||
try
|
||||
{
|
||||
|
||||
new InitialDirContext(env);
|
||||
|
||||
throw new AuthenticationException(
|
||||
"The ldap server at "
|
||||
+ env.get(Context.PROVIDER_URL)
|
||||
+ " falls back to use anonymous bind if invalid security credentials are presented. This is not supported.");
|
||||
}
|
||||
catch (javax.naming.AuthenticationException ax)
|
||||
{
|
||||
logger.info("LDAP server does not fall back to anonymous bind for a string uid and password at " + env.get(Context.PROVIDER_URL));
|
||||
}
|
||||
catch (NamingException nx)
|
||||
{
|
||||
logger.info("LDAP server does not support simple string user ids and invalid credentials at "+ env.get(Context.PROVIDER_URL));
|
||||
}
|
||||
|
||||
// DN and password
|
||||
|
||||
env = new Hashtable<String, String>(initialDirContextEnvironment.size());
|
||||
env.putAll(initialDirContextEnvironment);
|
||||
env.put(Context.SECURITY_PRINCIPAL, "cn=daftAsABrush,dc=woof");
|
||||
env.put(Context.SECURITY_CREDENTIALS, "daftAsABrush");
|
||||
try
|
||||
{
|
||||
|
||||
new InitialDirContext(env);
|
||||
|
||||
throw new AuthenticationException(
|
||||
"The ldap server at "
|
||||
+ env.get(Context.PROVIDER_URL)
|
||||
+ " falls back to use anonymous bind if invalid security credentials are presented. This is not supported.");
|
||||
}
|
||||
catch (javax.naming.AuthenticationException ax)
|
||||
{
|
||||
logger.info("LDAP server does not fall back to anonymous bind for a simple dn and password at " + env.get(Context.PROVIDER_URL));
|
||||
}
|
||||
catch (NamingException nx)
|
||||
{
|
||||
logger.info("LDAP server does not support simple DN and invalid password at "+ env.get(Context.PROVIDER_URL));
|
||||
}
|
||||
|
||||
// Check more if we have a real principal we expect to work
|
||||
|
||||
env = new Hashtable<String, String>(initialDirContextEnvironment.size());
|
||||
env.putAll(initialDirContextEnvironment);
|
||||
if(env.get(Context.SECURITY_PRINCIPAL) != null)
|
||||
{
|
||||
// Correct principal invalid password
|
||||
|
||||
env = new Hashtable<String, String>(initialDirContextEnvironment.size());
|
||||
env.putAll(initialDirContextEnvironment);
|
||||
env.put(Context.SECURITY_CREDENTIALS, "sdasdasdasdasd123123123");
|
||||
try
|
||||
{
|
||||
|
||||
new InitialDirContext(env);
|
||||
|
||||
throw new AuthenticationException(
|
||||
"The ldap server at "
|
||||
+ env.get(Context.PROVIDER_URL)
|
||||
+ " falls back to use anonymous bind for a known principal if invalid security credentials are presented. This is not supported.");
|
||||
}
|
||||
catch (javax.naming.AuthenticationException ax)
|
||||
{
|
||||
logger.info("LDAP server does not fall back to anonymous bind for known principal and invalid credentials at " + env.get(Context.PROVIDER_URL));
|
||||
}
|
||||
catch (NamingException nx)
|
||||
{
|
||||
// already donw
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,334 +1,342 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2006 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.security.authentication.ldap;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.naming.NamingEnumeration;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.InitialDirContext;
|
||||
import javax.naming.directory.SearchControls;
|
||||
import javax.naming.directory.SearchResult;
|
||||
import javax.transaction.UserTransaction;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.importer.ExportSource;
|
||||
import org.alfresco.repo.importer.ExportSourceImporterException;
|
||||
import org.alfresco.service.cmr.security.PersonService;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.service.transaction.TransactionService;
|
||||
import org.alfresco.util.ApplicationContextHelper;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
|
||||
public class LDAPPersonExportSource implements ExportSource
|
||||
{
|
||||
private static Log s_logger = LogFactory.getLog(LDAPPersonExportSource.class);
|
||||
|
||||
private String personQuery = "(objectclass=inetOrgPerson)";
|
||||
|
||||
private String searchBase;
|
||||
|
||||
private String userIdAttributeName;
|
||||
|
||||
private LDAPInitialDirContextFactory ldapInitialContextFactory;
|
||||
|
||||
private PersonService personService;
|
||||
|
||||
private Map<String, String> attributeMapping;
|
||||
|
||||
private NamespaceService namespaceService;
|
||||
|
||||
private String defaultHomeFolder;
|
||||
|
||||
public LDAPPersonExportSource()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public void setPersonQuery(String personQuery)
|
||||
{
|
||||
this.personQuery = personQuery;
|
||||
}
|
||||
|
||||
public void setSearchBase(String searchBase)
|
||||
{
|
||||
this.searchBase = searchBase;
|
||||
}
|
||||
|
||||
public void setUserIdAttributeName(String userIdAttributeName)
|
||||
{
|
||||
this.userIdAttributeName = userIdAttributeName;
|
||||
}
|
||||
|
||||
public void setLDAPInitialDirContextFactory(LDAPInitialDirContextFactory ldapInitialDirContextFactory)
|
||||
{
|
||||
this.ldapInitialContextFactory = ldapInitialDirContextFactory;
|
||||
}
|
||||
|
||||
public void setPersonService(PersonService personService)
|
||||
{
|
||||
this.personService = personService;
|
||||
}
|
||||
|
||||
public void setDefaultHomeFolder(String defaultHomeFolder)
|
||||
{
|
||||
this.defaultHomeFolder = defaultHomeFolder;
|
||||
}
|
||||
|
||||
public void setNamespaceService(NamespaceService namespaceService)
|
||||
{
|
||||
this.namespaceService = namespaceService;
|
||||
}
|
||||
|
||||
public void setAttributeMapping(Map<String, String> attributeMapping)
|
||||
{
|
||||
this.attributeMapping = attributeMapping;
|
||||
}
|
||||
|
||||
public void generateExport(XMLWriter writer)
|
||||
{
|
||||
QName nodeUUID = QName.createQName("sys:node-uuid", namespaceService);
|
||||
|
||||
Collection<String> prefixes = namespaceService.getPrefixes();
|
||||
QName childQName = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, "childName", namespaceService);
|
||||
|
||||
try
|
||||
{
|
||||
AttributesImpl attrs = new AttributesImpl();
|
||||
attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, childQName.getLocalName(), childQName
|
||||
.toPrefixString(), null, ContentModel.TYPE_PERSON.toPrefixString(namespaceService));
|
||||
|
||||
writer.startDocument();
|
||||
|
||||
for (String prefix : prefixes)
|
||||
{
|
||||
if (!prefix.equals("xml"))
|
||||
{
|
||||
String uri = namespaceService.getNamespaceURI(prefix);
|
||||
writer.startPrefixMapping(prefix, uri);
|
||||
}
|
||||
}
|
||||
|
||||
writer.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, "view",
|
||||
NamespaceService.REPOSITORY_VIEW_PREFIX + ":" + "view", new AttributesImpl());
|
||||
|
||||
InitialDirContext ctx = null;
|
||||
try
|
||||
{
|
||||
ctx = ldapInitialContextFactory.getDefaultIntialDirContext();
|
||||
|
||||
// Authentication has been successful.
|
||||
// Set the current user, they are now authenticated.
|
||||
|
||||
SearchControls userSearchCtls = new SearchControls();
|
||||
userSearchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
||||
|
||||
userSearchCtls.setCountLimit(Integer.MAX_VALUE);
|
||||
|
||||
NamingEnumeration searchResults = ctx.search(searchBase, personQuery, userSearchCtls);
|
||||
while (searchResults.hasMoreElements())
|
||||
{
|
||||
SearchResult result = (SearchResult) searchResults.next();
|
||||
Attributes attributes = result.getAttributes();
|
||||
Attribute uidAttribute = attributes.get(userIdAttributeName);
|
||||
if (uidAttribute == null)
|
||||
{
|
||||
throw new ExportSourceImporterException(
|
||||
"User returned by user search does not have mandatory user id attribute " + attributes);
|
||||
}
|
||||
String uid = (String) uidAttribute.get(0);
|
||||
|
||||
if (s_logger.isDebugEnabled())
|
||||
{
|
||||
s_logger.debug("Adding user for " + uid);
|
||||
}
|
||||
|
||||
|
||||
writer.startElement(ContentModel.TYPE_PERSON.getNamespaceURI(), ContentModel.TYPE_PERSON
|
||||
.getLocalName(), ContentModel.TYPE_PERSON.toPrefixString(namespaceService), attrs);
|
||||
|
||||
// permissions
|
||||
|
||||
// owner
|
||||
|
||||
writer.startElement(ContentModel.ASPECT_OWNABLE.getNamespaceURI(), ContentModel.ASPECT_OWNABLE
|
||||
.getLocalName(), ContentModel.ASPECT_OWNABLE.toPrefixString(namespaceService),
|
||||
new AttributesImpl());
|
||||
|
||||
writer.endElement(ContentModel.ASPECT_OWNABLE.getNamespaceURI(), ContentModel.ASPECT_OWNABLE
|
||||
.getLocalName(), ContentModel.ASPECT_OWNABLE.toPrefixString(namespaceService));
|
||||
|
||||
writer.startElement(ContentModel.PROP_OWNER.getNamespaceURI(), ContentModel.PROP_OWNER
|
||||
.getLocalName(), ContentModel.PROP_OWNER.toPrefixString(namespaceService),
|
||||
new AttributesImpl());
|
||||
|
||||
writer.characters(uid.toCharArray(), 0, uid.length());
|
||||
|
||||
writer.endElement(ContentModel.PROP_OWNER.getNamespaceURI(),
|
||||
ContentModel.PROP_OWNER.getLocalName(), ContentModel.PROP_OWNER
|
||||
.toPrefixString(namespaceService));
|
||||
|
||||
for (String key : attributeMapping.keySet())
|
||||
{
|
||||
QName keyQName = QName.createQName(key, namespaceService);
|
||||
|
||||
writer.startElement(keyQName.getNamespaceURI(), keyQName.getLocalName(), keyQName
|
||||
.toPrefixString(namespaceService), new AttributesImpl());
|
||||
|
||||
// cater for null
|
||||
String attributeName = attributeMapping.get(key);
|
||||
if (attributeName != null)
|
||||
{
|
||||
Attribute attribute = attributes.get(attributeName);
|
||||
if (attribute != null)
|
||||
{
|
||||
String value = (String) attribute.get(0);
|
||||
if (value != null)
|
||||
{
|
||||
writer.characters(value.toCharArray(), 0, value.length());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writer.endElement(keyQName.getNamespaceURI(), keyQName.getLocalName(), keyQName
|
||||
.toPrefixString(namespaceService));
|
||||
}
|
||||
|
||||
// Default home folder
|
||||
|
||||
if (!(attributeMapping.keySet().contains(ContentModel.PROP_HOMEFOLDER.toString()) || attributeMapping
|
||||
.keySet().contains(ContentModel.PROP_HOMEFOLDER.toPrefixString(namespaceService))))
|
||||
{
|
||||
// Only if we are creating the person for the first time
|
||||
if (!personService.personExists(uid))
|
||||
{
|
||||
writer.startElement(ContentModel.PROP_HOMEFOLDER.getNamespaceURI(),
|
||||
ContentModel.PROP_HOMEFOLDER.getLocalName(), ContentModel.PROP_HOMEFOLDER
|
||||
.toPrefixString(namespaceService), new AttributesImpl());
|
||||
|
||||
if (defaultHomeFolder != null)
|
||||
{
|
||||
writer.characters(defaultHomeFolder.toCharArray(), 0, defaultHomeFolder.length());
|
||||
}
|
||||
|
||||
writer.endElement(ContentModel.PROP_HOMEFOLDER.getNamespaceURI(),
|
||||
ContentModel.PROP_HOMEFOLDER.getLocalName(), ContentModel.PROP_HOMEFOLDER
|
||||
.toPrefixString(namespaceService));
|
||||
}
|
||||
}
|
||||
|
||||
if (personService.personExists(uid))
|
||||
{
|
||||
String uguid = personService.getPerson(uid).getId();
|
||||
|
||||
writer.startElement(nodeUUID.getNamespaceURI(), nodeUUID.getLocalName(), nodeUUID
|
||||
.toPrefixString(namespaceService), new AttributesImpl());
|
||||
|
||||
writer.characters(uguid.toCharArray(), 0, uguid.length());
|
||||
|
||||
writer.endElement(nodeUUID.getNamespaceURI(), nodeUUID.getLocalName(), nodeUUID
|
||||
.toPrefixString(namespaceService));
|
||||
}
|
||||
writer.endElement(ContentModel.TYPE_PERSON.getNamespaceURI(), ContentModel.TYPE_PERSON
|
||||
.getLocalName(), ContentModel.TYPE_PERSON.toPrefixString(namespaceService));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch (NamingException e)
|
||||
{
|
||||
throw new ExportSourceImporterException("Failed to import people.", e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (ctx != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
ctx.close();
|
||||
}
|
||||
catch (NamingException e)
|
||||
{
|
||||
throw new ExportSourceImporterException("Failed to import people.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (String prefix : prefixes)
|
||||
{
|
||||
if (!prefix.equals("xml"))
|
||||
{
|
||||
writer.endPrefixMapping(prefix);
|
||||
}
|
||||
}
|
||||
|
||||
writer.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, "view", NamespaceService.REPOSITORY_VIEW_PREFIX
|
||||
+ ":" + "view");
|
||||
|
||||
writer.endDocument();
|
||||
}
|
||||
catch (SAXException e)
|
||||
{
|
||||
throw new ExportSourceImporterException("Failed to create file for import.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
|
||||
ExportSource source = (ExportSource) ctx.getBean("ldapPeopleExportSource");
|
||||
TransactionService txs = (TransactionService) ctx.getBean("transactionComponent");
|
||||
UserTransaction tx = txs.getUserTransaction();
|
||||
tx.begin();
|
||||
|
||||
File file = new File(args[0]);
|
||||
Writer writer = new BufferedWriter(new FileWriter(file));
|
||||
XMLWriter xmlWriter = createXMLExporter(writer);
|
||||
source.generateExport(xmlWriter);
|
||||
xmlWriter.close();
|
||||
|
||||
tx.commit();
|
||||
}
|
||||
|
||||
private static XMLWriter createXMLExporter(Writer writer)
|
||||
{
|
||||
// Define output format
|
||||
OutputFormat format = OutputFormat.createPrettyPrint();
|
||||
format.setNewLineAfterDeclaration(false);
|
||||
format.setIndentSize(3);
|
||||
format.setEncoding("UTF-8");
|
||||
|
||||
// Construct an XML Exporter
|
||||
|
||||
XMLWriter xmlWriter = new XMLWriter(writer, format);
|
||||
return xmlWriter;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright (C) 2005-2006 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.security.authentication.ldap;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.naming.NamingEnumeration;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.InitialDirContext;
|
||||
import javax.naming.directory.SearchControls;
|
||||
import javax.naming.directory.SearchResult;
|
||||
import javax.transaction.UserTransaction;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.importer.ExportSource;
|
||||
import org.alfresco.repo.importer.ExportSourceImporterException;
|
||||
import org.alfresco.service.cmr.security.PersonService;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.service.transaction.TransactionService;
|
||||
import org.alfresco.util.ApplicationContextHelper;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
|
||||
public class LDAPPersonExportSource implements ExportSource
|
||||
{
|
||||
private static Log s_logger = LogFactory.getLog(LDAPPersonExportSource.class);
|
||||
|
||||
private String personQuery = "(objectclass=inetOrgPerson)";
|
||||
|
||||
private String searchBase;
|
||||
|
||||
private String userIdAttributeName;
|
||||
|
||||
private LDAPInitialDirContextFactory ldapInitialContextFactory;
|
||||
|
||||
private PersonService personService;
|
||||
|
||||
private Map<String, String> attributeMapping;
|
||||
|
||||
private NamespaceService namespaceService;
|
||||
|
||||
private Map<String, String> attributeDefaults;
|
||||
|
||||
private boolean errorOnMissingUID;
|
||||
|
||||
public LDAPPersonExportSource()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public void setPersonQuery(String personQuery)
|
||||
{
|
||||
this.personQuery = personQuery;
|
||||
}
|
||||
|
||||
public void setSearchBase(String searchBase)
|
||||
{
|
||||
this.searchBase = searchBase;
|
||||
}
|
||||
|
||||
public void setUserIdAttributeName(String userIdAttributeName)
|
||||
{
|
||||
this.userIdAttributeName = userIdAttributeName;
|
||||
}
|
||||
|
||||
public void setLDAPInitialDirContextFactory(LDAPInitialDirContextFactory ldapInitialDirContextFactory)
|
||||
{
|
||||
this.ldapInitialContextFactory = ldapInitialDirContextFactory;
|
||||
}
|
||||
|
||||
public void setPersonService(PersonService personService)
|
||||
{
|
||||
this.personService = personService;
|
||||
}
|
||||
|
||||
public void setAttributeDefaults(Map<String, String> attributeDefaults)
|
||||
{
|
||||
this.attributeDefaults = attributeDefaults;
|
||||
}
|
||||
|
||||
public void setNamespaceService(NamespaceService namespaceService)
|
||||
{
|
||||
this.namespaceService = namespaceService;
|
||||
}
|
||||
|
||||
public void setAttributeMapping(Map<String, String> attributeMapping)
|
||||
{
|
||||
this.attributeMapping = attributeMapping;
|
||||
}
|
||||
|
||||
public void setErrorOnMissingUID(boolean errorOnMissingUID)
|
||||
{
|
||||
this.errorOnMissingUID = errorOnMissingUID;
|
||||
}
|
||||
|
||||
public void generateExport(XMLWriter writer)
|
||||
{
|
||||
QName nodeUUID = QName.createQName("sys:node-uuid", namespaceService);
|
||||
|
||||
Collection<String> prefixes = namespaceService.getPrefixes();
|
||||
QName childQName = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, "childName", namespaceService);
|
||||
|
||||
try
|
||||
{
|
||||
AttributesImpl attrs = new AttributesImpl();
|
||||
attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, childQName.getLocalName(), childQName
|
||||
.toPrefixString(), null, ContentModel.TYPE_PERSON.toPrefixString(namespaceService));
|
||||
|
||||
writer.startDocument();
|
||||
|
||||
for (String prefix : prefixes)
|
||||
{
|
||||
if (!prefix.equals("xml"))
|
||||
{
|
||||
String uri = namespaceService.getNamespaceURI(prefix);
|
||||
writer.startPrefixMapping(prefix, uri);
|
||||
}
|
||||
}
|
||||
|
||||
writer.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, "view",
|
||||
NamespaceService.REPOSITORY_VIEW_PREFIX + ":" + "view", new AttributesImpl());
|
||||
|
||||
InitialDirContext ctx = null;
|
||||
try
|
||||
{
|
||||
ctx = ldapInitialContextFactory.getDefaultIntialDirContext();
|
||||
|
||||
// Authentication has been successful.
|
||||
// Set the current user, they are now authenticated.
|
||||
|
||||
SearchControls userSearchCtls = new SearchControls();
|
||||
userSearchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
||||
|
||||
userSearchCtls.setCountLimit(Integer.MAX_VALUE);
|
||||
|
||||
NamingEnumeration searchResults = ctx.search(searchBase, personQuery, userSearchCtls);
|
||||
while (searchResults.hasMoreElements())
|
||||
{
|
||||
SearchResult result = (SearchResult) searchResults.next();
|
||||
Attributes attributes = result.getAttributes();
|
||||
Attribute uidAttribute = attributes.get(userIdAttributeName);
|
||||
if (uidAttribute == null)
|
||||
{
|
||||
if(errorOnMissingUID)
|
||||
{
|
||||
throw new ExportSourceImporterException(
|
||||
"User returned by user search does not have mandatory user id attribute " + attributes);
|
||||
}
|
||||
else
|
||||
{
|
||||
s_logger.warn("User returned by user search does not have mandatory user id attribute " + attributes);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
String uid = (String) uidAttribute.get(0);
|
||||
|
||||
if (s_logger.isDebugEnabled())
|
||||
{
|
||||
s_logger.debug("Adding user for " + uid);
|
||||
}
|
||||
|
||||
|
||||
writer.startElement(ContentModel.TYPE_PERSON.getNamespaceURI(), ContentModel.TYPE_PERSON
|
||||
.getLocalName(), ContentModel.TYPE_PERSON.toPrefixString(namespaceService), attrs);
|
||||
|
||||
// permissions
|
||||
|
||||
// owner
|
||||
|
||||
writer.startElement(ContentModel.ASPECT_OWNABLE.getNamespaceURI(), ContentModel.ASPECT_OWNABLE
|
||||
.getLocalName(), ContentModel.ASPECT_OWNABLE.toPrefixString(namespaceService),
|
||||
new AttributesImpl());
|
||||
|
||||
writer.endElement(ContentModel.ASPECT_OWNABLE.getNamespaceURI(), ContentModel.ASPECT_OWNABLE
|
||||
.getLocalName(), ContentModel.ASPECT_OWNABLE.toPrefixString(namespaceService));
|
||||
|
||||
writer.startElement(ContentModel.PROP_OWNER.getNamespaceURI(), ContentModel.PROP_OWNER
|
||||
.getLocalName(), ContentModel.PROP_OWNER.toPrefixString(namespaceService),
|
||||
new AttributesImpl());
|
||||
|
||||
writer.characters(uid.toCharArray(), 0, uid.length());
|
||||
|
||||
writer.endElement(ContentModel.PROP_OWNER.getNamespaceURI(),
|
||||
ContentModel.PROP_OWNER.getLocalName(), ContentModel.PROP_OWNER
|
||||
.toPrefixString(namespaceService));
|
||||
|
||||
for (String key : attributeMapping.keySet())
|
||||
{
|
||||
QName keyQName = QName.createQName(key, namespaceService);
|
||||
|
||||
writer.startElement(keyQName.getNamespaceURI(), keyQName.getLocalName(), keyQName
|
||||
.toPrefixString(namespaceService), new AttributesImpl());
|
||||
|
||||
// cater for null
|
||||
String attributeName = attributeMapping.get(key);
|
||||
if (attributeName != null)
|
||||
{
|
||||
Attribute attribute = attributes.get(attributeName);
|
||||
if (attribute != null)
|
||||
{
|
||||
String value = (String) attribute.get(0);
|
||||
if (value != null)
|
||||
{
|
||||
writer.characters(value.toCharArray(), 0, value.length());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
String defaultValue = attributeDefaults.get(key);
|
||||
if(defaultValue != null)
|
||||
{
|
||||
writer.characters(defaultValue.toCharArray(), 0, defaultValue.length());
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
String defaultValue = attributeDefaults.get(key);
|
||||
if(defaultValue != null)
|
||||
{
|
||||
writer.characters(defaultValue.toCharArray(), 0, defaultValue.length());
|
||||
}
|
||||
}
|
||||
|
||||
writer.endElement(keyQName.getNamespaceURI(), keyQName.getLocalName(), keyQName
|
||||
.toPrefixString(namespaceService));
|
||||
}
|
||||
|
||||
if (personService.personExists(uid))
|
||||
{
|
||||
String uguid = personService.getPerson(uid).getId();
|
||||
|
||||
writer.startElement(nodeUUID.getNamespaceURI(), nodeUUID.getLocalName(), nodeUUID
|
||||
.toPrefixString(namespaceService), new AttributesImpl());
|
||||
|
||||
writer.characters(uguid.toCharArray(), 0, uguid.length());
|
||||
|
||||
writer.endElement(nodeUUID.getNamespaceURI(), nodeUUID.getLocalName(), nodeUUID
|
||||
.toPrefixString(namespaceService));
|
||||
}
|
||||
writer.endElement(ContentModel.TYPE_PERSON.getNamespaceURI(), ContentModel.TYPE_PERSON
|
||||
.getLocalName(), ContentModel.TYPE_PERSON.toPrefixString(namespaceService));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch (NamingException e)
|
||||
{
|
||||
throw new ExportSourceImporterException("Failed to import people.", e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (ctx != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
ctx.close();
|
||||
}
|
||||
catch (NamingException e)
|
||||
{
|
||||
throw new ExportSourceImporterException("Failed to import people.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (String prefix : prefixes)
|
||||
{
|
||||
if (!prefix.equals("xml"))
|
||||
{
|
||||
writer.endPrefixMapping(prefix);
|
||||
}
|
||||
}
|
||||
|
||||
writer.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, "view", NamespaceService.REPOSITORY_VIEW_PREFIX
|
||||
+ ":" + "view");
|
||||
|
||||
writer.endDocument();
|
||||
}
|
||||
catch (SAXException e)
|
||||
{
|
||||
throw new ExportSourceImporterException("Failed to create file for import.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
|
||||
ExportSource source = (ExportSource) ctx.getBean("ldapPeopleExportSource");
|
||||
TransactionService txs = (TransactionService) ctx.getBean("transactionComponent");
|
||||
UserTransaction tx = txs.getUserTransaction();
|
||||
tx.begin();
|
||||
|
||||
File file = new File(args[0]);
|
||||
Writer writer = new BufferedWriter(new FileWriter(file));
|
||||
XMLWriter xmlWriter = createXMLExporter(writer);
|
||||
source.generateExport(xmlWriter);
|
||||
xmlWriter.close();
|
||||
|
||||
tx.commit();
|
||||
}
|
||||
|
||||
private static XMLWriter createXMLExporter(Writer writer)
|
||||
{
|
||||
// Define output format
|
||||
OutputFormat format = OutputFormat.createPrettyPrint();
|
||||
format.setNewLineAfterDeclaration(false);
|
||||
format.setIndentSize(3);
|
||||
format.setEncoding("UTF-8");
|
||||
|
||||
// Construct an XML Exporter
|
||||
|
||||
XMLWriter xmlWriter = new XMLWriter(writer, format);
|
||||
return xmlWriter;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user