X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fpages%2Faccount%2FMyDetailsForm.java;h=813902289e623f925e5b89c2d9dad7ef10123169;hp=d35ba45e84bea8ec27872807196abf4de2be6632;hb=3d1e7ec232f250e1ecb8180675d609b34c62b8c2;hpb=25f59f370978c0e9789821a15d89a83d5d702622 diff --git a/src/org/cacert/gigi/pages/account/MyDetailsForm.java b/src/org/cacert/gigi/pages/account/MyDetailsForm.java index d35ba45e..81390228 100644 --- a/src/org/cacert/gigi/pages/account/MyDetailsForm.java +++ b/src/org/cacert/gigi/pages/account/MyDetailsForm.java @@ -2,18 +2,24 @@ package org.cacert.gigi.pages.account; import java.io.PrintWriter; import java.util.Map; +import java.util.Set; import javax.servlet.http.HttpServletRequest; import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.dbObjects.Group; import org.cacert.gigi.dbObjects.Name; import org.cacert.gigi.dbObjects.User; import org.cacert.gigi.localisation.Language; +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; -import org.cacert.gigi.util.HTMLEncoder; public class MyDetailsForm extends Form { @@ -21,37 +27,94 @@ public class MyDetailsForm extends Form { private static final Template templ = new Template(MyDetailsForm.class.getResource("MyDetailsForm.templ")); + private static final Template names = new Template(MyDetailsForm.class.getResource("NamesForm.templ")); + + private static final Template roles = new Template(MyDetailsForm.class.getResource("MyDetailsRoles.templ")); + private User target; private DateSelector ds; + private NameInput ni; + + private CountrySelector cs; + + private GroupSelector selectedGroup = new GroupSelector("groupToModify", false); + public MyDetailsForm(HttpServletRequest hsr, User target) { super(hsr); this.target = target; + ni = new NameInput(); + this.ds = new DateSelector("day", "month", "year", target.getDoB()); + + if (target.getResidenceCountry() == null) { + this.cs = new CountrySelector("residenceCountry", true); + } else { + this.cs = new CountrySelector("residenceCountry", true, target.getResidenceCountry()); + } } @Override public boolean submit(PrintWriter out, HttpServletRequest req) { try { - synchronized (target) { - if (target.getAssurancePoints() == 0) { - String newFname = req.getParameter("fname").trim(); - String newLname = req.getParameter("lname").trim(); - String newMname = req.getParameter("mname").trim(); - String newSuffix = req.getParameter("suffix").trim(); - if (newLname.isEmpty()) { - throw new GigiApiException("Last name cannot be empty."); - } + String rn = req.getParameter("removeName"); + if (rn != null) { + Name n = Name.getById(Integer.parseInt(rn)); + if (n.getOwner() != target) { + throw new GigiApiException("Cannot remove a name that does not belong to this account."); + } + if (n.equals(target.getPreferredName())) { + throw new GigiApiException("Cannot remove the account's preferred name."); + } + n.remove(); + return true; + } + String dn = req.getParameter("deprecateName"); + if (dn != null) { + Name n = Name.getById(Integer.parseInt(dn)); + if (n.getOwner() != target) { + throw new GigiApiException("Cannot deprecate a name that does not belong to this account."); + } + if (n.equals(target.getPreferredName())) { + throw new GigiApiException("Cannot deprecate the account's preferred name."); + } + n.deprecate(); + return true; + } + String pn = req.getParameter("preferred"); + if (pn != null) { + Name n = Name.getById(Integer.parseInt(pn)); + target.setPreferredName(n); + return true; + } + + String action = req.getParameter("action"); + if ("addName".equals(action)) { + ni.update(req); + ni.createName(target); + return true; + } + if ("updateDoB".equals(action)) { + ds.update(req); + target.setDoB(ds.getDate()); + } + if ("updateResidenceCountry".equals(action)) { + cs.update(req); + target.setResidenceCountry(cs.getCountry()); + } - target.setName(new Name(newFname, newLname, newMname, newSuffix)); - ds.update(req); - target.setDoB(ds.getDate()); - target.updateUserData(); + if ("addGroup".equals(action) || "removeGroup".equals(action)) { + selectedGroup.update(req); + Group toMod = selectedGroup.getGroup(); + if ("addGroup".equals(action)) { + target.grantGroup(target, toMod); } else { - throw new GigiApiException("No change after assurance allowed."); + target.revokeGroup(target, toMod); } + return true; } + } catch (GigiApiException e) { e.format(out, Page.getLanguage(req)); return false; @@ -64,19 +127,46 @@ public class MyDetailsForm extends Form { @Override protected void outputContent(PrintWriter out, Language l, Map vars) { - Name name = target.getName(); - vars.put("fname", HTMLEncoder.encodeHTML(name.getFname())); - vars.put("mname", name.getMname() == null ? "" : HTMLEncoder.encodeHTML(name.getMname())); - vars.put("lname", HTMLEncoder.encodeHTML(name.getLname())); - vars.put("suffix", name.getSuffix() == null ? "" : HTMLEncoder.encodeHTML(name.getSuffix())); - vars.put("details", ""); - if (target.getAssurancePoints() == 0) { + vars.put("exNames", new ArrayIterable(target.getNames()) { + + Name preferred = target.getPreferredName(); + + @Override + public void apply(Name t, Language l, Map vars) { + if (t.equals(preferred)) { + vars.put("preferred", " disabled"); + vars.put("deprecated", " disabled"); + } else { + if (t.isDeprecated()) { + vars.put("deprecated", " disabled"); + } else { + vars.put("deprecated", ""); + } + vars.put("preferred", ""); + } + vars.put("name", t); + vars.put("id", t.getId()); + vars.put("npoints", Integer.toString(t.getAssurancePoints())); + } + + }); + vars.put("name", ni); + names.output(out, l, vars); + + vars.put("residenceCountry", cs); + if (target.getReceivedAssurances().length == 0) { vars.put("DoB", ds); templ.output(out, l, vars); } else { vars.put("DoB", target.getDoB()); assured.output(out, l, vars); } + + final Set 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); } }