*/
public class UpdateAccount extends Action
{
public void doPerform( RunData rundata ) throws Exception
{
JetspeedRunData data = (JetspeedRunData)rundata;
// check to make sure the user has logged in before accessing this screen
if ( ! data.getUser().hasLoggedIn() )
{
data.setScreenTemplate( JetspeedResources.getString( "services.JspService.screen.error.NotLoggedIn", "Error" ) );
return;
}
String cancelBtn = data.getParameters().getString( "CancelBtn" , "" );
String username = data.getParameters().getString( "username" , "" );
String oldPassword = JetspeedSecurity.convertPassword(data.getParameters().getString( "old_password" , "" ));
String password = JetspeedSecurity.convertPassword(data.getParameters().getString( "password" , "" ));
String password2 = JetspeedSecurity.convertPassword(data.getParameters().getString( "password_confirm" , "" ));
String firstname = data.getParameters().getString( "firstname", "" );
String lastname = data.getParameters().getString( "lastname" , "" );
String email = data.getParameters().getString( "email" , "" );
boolean userRequestsRememberMe = data.getParameters().getBoolean( "rememberme" , false );
// Save user input in case there is an error and
// we have to go back to the EditAccount screen
Hashtable screenData = new Hashtable();
screenData.put( "username", username );
screenData.put( "firstname", firstname );
screenData.put( "lastname", lastname );
screenData.put( "email", email );
data.getRequest().setAttribute( "ScreenDataEditAccount", screenData );
// CANCEL BUTTON
//
// check to see if the Cancel button was pressed.
// if so, return to the screen we were previously on
// defined by nextscreen in the EditAccount screen
if ( ! cancelBtn.equalsIgnoreCase( "" ) )
{
return;
}
// PASSWORD
//
// if the fields are empty, then don't do anything to the passwords
boolean changepass = false;
if ( password.trim().length() > 0 && password2.trim().length() > 0 )
{
changepass = true;
}
if ( changepass == true && ! password.equals( password2 ) )
{
data.setMessage(CustomLocalization.getString("UPDATEACCOUNT_PWNOTMATCH", rundata));
backToEditAccount( data, screenData );
return;
}
if ( changepass == true && password.equals( oldPassword ) )
{
// old password = new passwod, so do not change.
changepass = false;
}
// FIRSTNAME
//
// make sure the firstname exists
if ( firstname.length() == 0 )
{
data.setMessage(CustomLocalization.getString("UPDATEACCOUNT_NOFIRSTNAME", rundata));
backToEditAccount( data, screenData );
return;
}
// LASTNAME
//
// make sure the lastname exists
if ( lastname.length() == 0 )
{
data.setMessage(CustomLocalization.getString("UPDATEACCOUNT_NOLASTNAME", rundata));
backToEditAccount( data, screenData );
return;
}
// AUTOMATIC LOGIN
//
// if automatic login is enabled, then handle the remember me checkbox
if ( JetspeedResources.getBoolean("automatic.logon.enable", false) )
{
if ( ! userRequestsRememberMe )
{
if ( data.getRequest().getCookies() != null &&
data.getCookies().getString("username") != null &&
data.getCookies().getString("logincookie") != null )
{
// remove cookies by re-adding them with zero MaxAge, which deletes them
Cookie userName = new Cookie("username","");
Cookie loginCookie = new Cookie("logincookie","");
String comment = JetspeedResources.getString("automatic.logon.cookie.comment","");
String domain = JetspeedResources.getString("automatic.logon.cookie.domain");
String path = JetspeedResources.getString("automatic.logon.cookie.path","/");
if (domain == null)
{
String server = data.getServerName();
domain = "." + server;
}
userName.setMaxAge(0);
userName.setComment(comment);
userName.setDomain(domain);
userName.setPath(path);
loginCookie.setMaxAge(0);
loginCookie.setComment(comment);
loginCookie.setDomain(domain);
loginCookie.setPath(path);
data.getResponse().addCookie(userName);
data.getResponse().addCookie(loginCookie);
data.getCookies().remove("username");
data.getCookies().remove("logincookie");
}
}
else
{
if ( data.getRequest().getCookies() == null ||
!data.getCookies().getString("username","").equals(data.getUser().getUserName()) ||
!data.getCookies().getString("logincookie","").equals(data.getUser().getPerm("logincookie")) )
{
String loginCookieValue = (String)data.getUser().getPerm("logincookie");
if (loginCookieValue == null || loginCookieValue.length() == 0)
{
loginCookieValue = ""+Math.random();
data.getUser().setPerm("logincookie",loginCookieValue);
JetspeedSecurity.saveUser( data.getJetspeedUser() );
}
Cookie userName = new Cookie("username",data.getUser().getUserName());
Cookie loginCookie = new Cookie("logincookie",loginCookieValue);
int maxage = JetspeedResources.getInt("automatic.logon.cookie.maxage",-1);
String comment = JetspeedResources.getString("automatic.logon.cookie.comment","");
String domain = JetspeedResources.getString("automatic.logon.cookie.domain");
String path = JetspeedResources.getString("automatic.logon.cookie.path","/");
if (domain == null)
{
String server = data.getServerName();
domain = "." + server;
}
userName.setMaxAge(maxage);
userName.setComment(comment);
userName.setDomain(domain);
userName.setPath(path);
loginCookie.setMaxAge(maxage);
loginCookie.setComment(comment);
loginCookie.setDomain(domain);
loginCookie.setPath(path);
data.getResponse().addCookie(userName);
data.getResponse().addCookie(loginCookie);
data.getCookies().add("username",data.getUser().getUserName());
data.getCookies().add("logincookie",loginCookieValue);
}
}
}
// EMAIL
//
// make sure the email exists
if ( email.length() == 0 )
{
data.setMessage(CustomLocalization.getString("UPDATEACCOUNT_NOEMAIL", rundata));
backToEditAccount( data, screenData );
return;
}
boolean enableMail = JetspeedResources.getBoolean("newuser.confirm.enable", false);
String currentEmail = (String) data.getUser().getEmail();
if ( enableMail && ( currentEmail == null || ! currentEmail.equalsIgnoreCase(email) ) )
{
//Send confirmation email if different than current
data.getUser().setEmail( email );
data.getUser().setConfirmed( GenerateUniqueId.getIdentifier() );
JetspeedSecurity.saveUser( data.getJetspeedUser() );
ActionLoader.getInstance().exec(data, "SendConfirmationEmail");
// add in the username to the parameters because ConfirmRegistration needs it
data.getParameters().add("username", data.getUser().getUserName() );
data.setMessage(CustomLocalization.getString("UPDATEACCOUNT_NEWEMAILCONFIRM", rundata));
data.setScreenTemplate("ConfirmRegistration");
}
else
{
JetspeedSecurity.saveUser( data.getJetspeedUser() );
}
// update currently logged in information that might have changed
data.getUser().setFirstName( firstname );
data.getUser().setLastName( lastname );
data.getUser().setEmail( email );
if ( changepass )
{
try
{
JetspeedSecurity.changePassword(data.getJetspeedUser(),oldPassword, password);
} catch (JetspeedSecurityException e)
{
data.setMessage(e.getMessage());
backToEditAccount( data, screenData );
return;
}
}
//allow sub-classes to update additional information
updateUser(data);
JetspeedSecurity.saveUser( data.getJetspeedUser() );
data.setMessage (CustomLocalization.getString("UPDATEACCOUNT_DONE", rundata));
}