/**
* @see org.apache.jetspeed.security.spi.CredentialHandler#setPassword(java.lang.String,java.lang.String,java.lang.String)
*/
public void setPassword(String userName, String oldPassword, String newPassword) throws SecurityException
{
InternalUserPrincipal internalUser = securityAccess.getInternalUserPrincipal(userName, false);
if (null == internalUser)
{
throw new SecurityException(SecurityException.USER_DOES_NOT_EXIST.create(userName));
}
Collection credentials = internalUser.getCredentials();
if (null == credentials)
{
credentials = new ArrayList();
}
InternalCredential credential = getPasswordCredential(internalUser, userName );
if (null != oldPassword)
{
if ( credential != null &&
credential.getValue() != null &&
credential.isEncoded() &&
pcProvider.getEncoder() != null )
{
oldPassword = pcProvider.getEncoder().encode(userName, oldPassword);
}
}
if (oldPassword != null && (credential == null || credential.getValue() == null || !credential.getValue().equals(oldPassword)))
{
// supplied PasswordCredential not defined for this user
throw new InvalidPasswordException();
}
if ( pcProvider.getValidator() != null )
{
try
{
pcProvider.getValidator().validate(newPassword);
}
catch (InvalidPasswordException ipe)
{
throw new InvalidNewPasswordException();
}
}
boolean encoded = false;
if ( pcProvider.getEncoder() != null )
{
newPassword = pcProvider.getEncoder().encode(userName, newPassword);
encoded = true;
}
boolean create = credential == null;
if ( create )
{
credential = new InternalCredentialImpl(internalUser.getPrincipalId(), newPassword, InternalCredential.PRIVATE,
pcProvider.getPasswordCredentialClass().getName());
credential.setEncoded(encoded);
credentials.add(credential);
}
else if ( oldPassword == null )
{
/* TODO: should only be allowed for admin
// User *has* an PasswordCredential: setting a new Credential without supplying
// its current one is not allowed
throw new SecurityException(SecurityException.PASSWORD_REQUIRED);
*/
}
else if ( oldPassword.equals(newPassword) )
{
throw new PasswordAlreadyUsedException();
}
if ( ipcInterceptor != null )
{
if ( create )
{
ipcInterceptor.beforeCreate(internalUser, credentials, userName, credential, newPassword );
}
else
{
ipcInterceptor.beforeSetPassword(internalUser, credentials, userName, credential, newPassword, oldPassword != null );
}
}
if (!create)
{
credential.setValue(newPassword);
credential.setEncoded(encoded);
}
internalUser.setModifiedDate(new Timestamp(new Date().getTime()));
internalUser.setCredentials(credentials);
// Set the user with the new credentials.
securityAccess.setInternalUserPrincipal(internalUser, false);
}