public void updatePassword(LDAPUserImpl ldapu, String password) throws IdentityException
{
if ((password == null || password.length() == 0) && !isAllowEmptyPasswords())
{
throw new IdentityException("Cannot update password with empty value - please set proper option to allow this");
}
String attributeName = getPasswordAttributeId();
LdapContext ldapContext = getConnectionContext().createInitialContext();
String passwordString = password;
if (getEnclosePasswordWith() != null)
{
String enc = getEnclosePasswordWith();
passwordString = enc + passwordString + enc;
}
byte[] encodedPassword = null;
if (getPasswordEncoding() != null && passwordString != null)
{
try
{
encodedPassword = passwordString.getBytes(getPasswordEncoding());
}
catch (UnsupportedEncodingException e)
{
throw new IdentityException("Error while encoding password with configured setting: " + getPasswordEncoding(),
e);
}
}
try
{
//TODO: maybe perform a schema check if this attribute is allowed for such entry
Attributes attrs = new BasicAttributes(true);
Attribute attr = new BasicAttribute(attributeName);
if (encodedPassword != null)
{
attr.add(encodedPassword);
}
else
{
attr.add(passwordString);
}
attrs.put(attr);
if(getUpdatePasswordAttributeValues() != null && getUpdatePasswordAttributeValues().size() > 0)
{
Map<String, Set<String>> attributesToAdd = getUpdatePasswordAttributeValues();
for (Map.Entry<String, Set<String>> entry : attributesToAdd.entrySet())
{
Attribute additionalAttr = new BasicAttribute(entry.getKey());
for (String val : entry.getValue())
{
additionalAttr.add(val);
}
attrs.put(additionalAttr);
}
}
ldapContext.modifyAttributes(ldapu.getDn(), DirContext.REPLACE_ATTRIBUTE,attrs);
}
catch (NamingException e)
{
throw new IdentityException("Cannot set user password value.", e);
}
finally
{
try
{
ldapContext.close();
}
catch (NamingException e)
{
throw new IdentityException("Failed to close LDAP connection", e);
}
}
}