]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/pages/account/ChangeForm.java
Implement and test change password form.
[gigi.git] / src / org / cacert / gigi / pages / account / ChangeForm.java
index f200c32398075482965ab19f50df5b78a2fb6755..5458b63d9f5ef9d94a9db148cccee44634194410 100644 (file)
@@ -1,24 +1,28 @@
 package org.cacert.gigi.pages.account;
 
-import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.Language;
+import org.cacert.gigi.User;
 import org.cacert.gigi.output.Form;
-import org.cacert.gigi.output.Template;
+import org.cacert.gigi.output.template.Template;
+import org.cacert.gigi.pages.Page;
 
 public class ChangeForm extends Form {
-       public ChangeForm(HttpServletRequest hsr) {
+       User target;
+
+       public ChangeForm(HttpServletRequest hsr, User target) {
                super(hsr);
+               this.target = target;
        }
 
        private static Template t;
        static {
-               t = new Template(
-                       new InputStreamReader(ChangePasswordPage.class.getResourceAsStream("ChangePasswordForm.templ")));
+               t = new Template(ChangePasswordPage.class.getResource("ChangePasswordForm.templ"));
        }
 
        @Override
@@ -28,7 +32,28 @@ public class ChangeForm extends Form {
 
        @Override
        public boolean submit(PrintWriter out, HttpServletRequest req) {
-               return false;
+               String oldpassword = req.getParameter("oldpassword");
+               String p1 = req.getParameter("pword1");
+               String p2 = req.getParameter("pword2");
+               GigiApiException error = new GigiApiException();
+               if (oldpassword == null || p1 == null || p2 == null) {
+                       new GigiApiException("All fields are required.").format(out, Page.getLanguage(req));
+                       return false;
+               }
+               if (!p1.equals(p2)) {
+                       new GigiApiException("New passwords do not match.").format(out, Page.getLanguage(req));
+                       return false;
+               }
+               try {
+                       target.changePassword(oldpassword, p1);
+               } catch (GigiApiException e) {
+                       error.mergeInto(e);
+               }
+               if (!error.isEmpty()) {
+                       error.format(out, Page.getLanguage(req));
+                       return false;
+               }
+               return true;
        }
 
 }