package graphene.web.pages;

import graphene.model.idl.G_User;
import graphene.model.idl.G_UserDataAccess;
import graphene.model.idl.G_VisualType;
import graphene.web.annotations.PluginPage;
import graphene.web.pages.pub.Login;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;
import org.tynamo.security.services.SecurityService;

/**
 * Allows the user to modify password and other settings
 * 
 * @author djue
 */
@PluginPage(visualType = G_VisualType.SETTINGS, menuName = "Change Password", icon = "fa fa-lg fa-fw fa-list-alt")
public class Settings {
	@Inject
	private G_UserDataAccess userDataAccess;

	@Inject
	private Messages messages;

	@InjectPage
	private Login loginPage;

	@Property
	private String password;

	@Property
	private String verifyPassword;

	@Component
	private Form settingsForm;

	@SessionState(create = false)
	private G_User user;

	@Inject
	private Logger logger;

	@Inject
	private SecurityService securityService;

	public Object onSuccess() {
		if (!verifyPassword.equals(password)) {
			settingsForm.recordError(messages.get("error.verifypassword"));
			return null;
		} else {
			settingsForm.clearErrors();
		}
		boolean success = false;
		try {
			success = userDataAccess.setUserPassword(user.getId(), password);

		} catch (final Exception e) {
			// if the DAO didn't update successfully, tell them so.
			logger.error("Unable to update password for user "
					+ user.getUsername() + " Error: " + e.getMessage());
		}
		if (success) {
			loginPage
					.setFlashMessage(messages.get("settings.password-changed"));
		} else {
			loginPage.setFlashMessage(messages
					.get("settings.password-not-changed"));
		}

		// authenticator.logout();
		user = null;
		securityService.getSubject().logout();
		// Send the user to the login page.
		return loginPage;
	}
}

@@ -1181,24 +1181,52 @@
 _User user;%0A
+%09private boolean userExists;
 %0A%09@Inject%0A%09p
@@ -1329,16 +1329,37 @@
 ess() %7B%0A
+%09%09if (userExists) %7B%0A%09
 %09%09if (!v
@@ -1392,16 +1392,17 @@
 ord)) %7B%0A
+%09
 %09%09%09setti
@@ -1463,16 +1463,17 @@
 %22));%0A%09%09%09
+%09
 return n
@@ -1475,24 +1475,25 @@
 urn null;%0A%09%09
+%09
 %7D else %7B%0A%09%09%09
@@ -1489,16 +1489,17 @@
  else %7B%0A
+%09
 %09%09%09setti
@@ -1521,22 +1521,24 @@
 rors();%0A
+%09
 %09%09%7D%0A%09%09
+%09
 boolean 
@@ -1560,14 +1560,16 @@
 ;%0A%09%09
+%09
 try %7B%0A
+%09
 %09%09%09s
@@ -1636,16 +1636,17 @@
 rd);%0A%0A%09%09
+%09
 %7D catch 
@@ -1667,16 +1667,17 @@
 on e) %7B%0A
+%09
 %09%09%09// if
@@ -1730,16 +1730,17 @@
  so.%0A%09%09%09
+%09
 logger.e
@@ -1781,22 +1781,17 @@
 r user %22
-%0A%09%09%09%09%09
+ 
 + user.g
@@ -1837,18 +1837,20 @@
 e());%0A%09%09
+%09
 %7D%0A
+%09
 %09%09if (su
@@ -1857,24 +1857,25 @@
 ccess) %7B%0A%09%09%09
+%09
 loginPage%0A%09%09
@@ -1871,22 +1871,16 @@
 oginPage
-%0A%09%09%09%09%09
 .setFlas
@@ -1934,16 +1934,17 @@
 d%22));%0A%09%09
+%09
 %7D else %7B
@@ -1944,16 +1944,17 @@
  else %7B%0A
+%09
 %09%09%09login
@@ -1986,14 +1986,8 @@
 ages
-%0A%09%09%09%09%09
 .get
@@ -2028,11 +2028,13 @@
 ;%0A%09%09
+%09
 %7D%0A%0A
+%09
 %09%09//
@@ -2060,16 +2060,17 @@
 ut();%0A%09%09
+%09
 user = n
@@ -2074,16 +2074,17 @@
 = null;%0A
+%09
 %09%09securi
@@ -2116,16 +2116,187 @@
 gout();%0A
+%09%09%7D else %7B%0A%09%09%09logger.error(%22A user tried to change a password without being logged in.%22);%0A%09%09%09loginPage.setFlashMessage(messages.get(%22settings.password-not-changed%22));%0A%09%09%7D%0A
 %09%09// Sen

Check for user exists for password reset page