]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/pages/account/MyDetailsForm.java
upd: enforce a more strict Form call pattern.
[gigi.git] / src / org / cacert / gigi / pages / account / MyDetailsForm.java
index d6b65e01842877154e0fc1c1307881ff96b2152a..f5b6f514fc6c3c9eca77816f5ab00a8b1d1eeefe 100644 (file)
@@ -15,10 +15,10 @@ import org.cacert.gigi.output.ArrayIterable;
 import org.cacert.gigi.output.CountrySelector;
 import org.cacert.gigi.output.DateSelector;
 import org.cacert.gigi.output.GroupIterator;
+import org.cacert.gigi.output.GroupSelector;
 import org.cacert.gigi.output.NameInput;
 import org.cacert.gigi.output.template.Form;
 import org.cacert.gigi.output.template.Template;
-import org.cacert.gigi.pages.Page;
 
 public class MyDetailsForm extends Form {
 
@@ -38,6 +38,8 @@ public class MyDetailsForm extends Form {
 
     private CountrySelector cs;
 
+    private GroupSelector selectedGroup = new GroupSelector("groupToModify", false);
+
     public MyDetailsForm(HttpServletRequest hsr, User target) {
         super(hsr);
         this.target = target;
@@ -53,7 +55,7 @@ public class MyDetailsForm extends Form {
     }
 
     @Override
-    public boolean submit(PrintWriter out, HttpServletRequest req) {
+    public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
         try {
             String rn = req.getParameter("removeName");
             if (rn != null) {
@@ -65,7 +67,7 @@ public class MyDetailsForm extends Form {
                     throw new GigiApiException("Cannot remove the account's preferred name.");
                 }
                 n.remove();
-                return true;
+                return new RedirectResult(MyDetails.PATH);
             }
             String dn = req.getParameter("deprecateName");
             if (dn != null) {
@@ -77,37 +79,44 @@ public class MyDetailsForm extends Form {
                     throw new GigiApiException("Cannot deprecate the account's preferred name.");
                 }
                 n.deprecate();
-                return true;
+                return new RedirectResult(MyDetails.PATH);
             }
             String pn = req.getParameter("preferred");
             if (pn != null) {
                 Name n = Name.getById(Integer.parseInt(pn));
                 target.setPreferredName(n);
-                return true;
+                return new RedirectResult(MyDetails.PATH);
             }
 
             String action = req.getParameter("action");
             if ("addName".equals(action)) {
                 ni.update(req);
                 ni.createName(target);
-                return true;
-            }
-            if ("updateDoB".equals(action)) {
+                return new RedirectResult(MyDetails.PATH);
+            } else if ("updateDoB".equals(action)) {
                 ds.update(req);
                 target.setDoB(ds.getDate());
-            }
-            if ("updateResidenceCountry".equals(action)) {
+                return new RedirectResult(MyDetails.PATH);
+            } else if ("updateResidenceCountry".equals(action)) {
                 cs.update(req);
                 target.setResidenceCountry(cs.getCountry());
+                return new RedirectResult(MyDetails.PATH);
+            } else if ("addGroup".equals(action) || "removeGroup".equals(action)) {
+                selectedGroup.update(req);
+                Group toMod = selectedGroup.getGroup();
+                if ("addGroup".equals(action)) {
+                    target.grantGroup(target, toMod);
+                } else {
+                    target.revokeGroup(target, toMod);
+                }
+                return new RedirectResult(MyDetails.PATH);
+            } else {
+                throw new GigiApiException("Invalid action.");
             }
-        } catch (GigiApiException e) {
-            e.format(out, Page.getLanguage(req));
-            return false;
+
         } catch (NumberFormatException e) {
-            new GigiApiException("Invalid value.").format(out, Page.getLanguage(req));
-            return false;
+            throw new GigiApiException("Invalid value.");
         }
-        return false;
     }
 
     @Override
@@ -136,13 +145,8 @@ public class MyDetailsForm extends Form {
 
         });
         vars.put("name", ni);
-        final Set<Group> gr = target.getGroups();
         names.output(out, l, vars);
 
-        vars.put("support-groups", new GroupIterator(gr.iterator(), true));
-        vars.put("groups", new GroupIterator(gr.iterator(), false));
-        roles.output(out, l, vars);
-
         vars.put("residenceCountry", cs);
         if (target.getReceivedAssurances().length == 0) {
             vars.put("DoB", ds);
@@ -152,6 +156,11 @@ public class MyDetailsForm extends Form {
             assured.output(out, l, vars);
         }
 
+        final Set<Group> gr = target.getGroups();
+        vars.put("support-groups", new GroupIterator(gr.iterator(), true));
+        vars.put("groups", new GroupIterator(gr.iterator(), false));
+        vars.put("groupSelector", selectedGroup);
+        roles.output(out, l, vars);
     }
 
 }