public User createUser(String userName, String password) throws IdentityException, IllegalArgumentException
{
if (userName == null)
{
throw new IdentityException("User name cannot be null");
}
/*if (realEmail == null)
{
throw new IdentityException("User email cannot be null");
}*/
if (password == null)
{
throw new IdentityException("User password cannot be null");
}
log.debug("Creating user: " + userName);
LdapContext ldapContext = getConnectionContext().createInitialContext();
try
{
//
LdapContext ctx = (LdapContext)ldapContext.lookup(getContainerDN());
//We store new entry using set of attributes. This should give more flexibility then
//extending user object from ContextDir - configure what objectClass place there
Attributes attrs = new BasicAttributes(true);
//create attribute using provided configuration
Map attributesToAdd = getAttributesToAdd();
//attributes
for (Iterator it1 = attributesToAdd.keySet().iterator(); it1.hasNext();)
{
String attributeName = (String)it1.next();
if (getUidAttributeID().equals(attributeName))
{
continue;
}
log.debug("adding attribute: " + attributeName);
Attribute attr = new BasicAttribute(attributeName);
Set attributeValues = (Set)attributesToAdd.get(attributeName);
//values
for (Iterator it2 = attributeValues.iterator(); it2.hasNext();)
{
String attrValue = (String)it2.next();
log.debug("adding attribute value: " + attrValue);
attr.add(attrValue);
}
attrs.put(attr);
}
if (!isSetPasswordAfterUserCreate())
{
attrs.put(getPasswordAttributeId(), password);
}
String validUserName = LDAPTools.encodeRfc2253Name(userName);
String dn = getUidAttributeID().concat("=").concat(validUserName);
log.debug("creating ldap entry for: " + dn + "; " + attrs);
ctx.createSubcontext(dn, attrs);
}
catch (Exception e)
{
throw new IdentityException("Failed to create user", e);
}
finally
{
try
{
ldapContext.close();
}
catch (NamingException e)
{
throw new IdentityException("Failed to close LDAP connection", e);
}
}
LDAPUserImpl u = (LDAPUserImpl)findUserByUserName(userName);