ac7ffd00bb68e69b22478c7fc378ed9215d5d482
[gigi.git] / src / org / cacert / gigi / pages / admin / support / SupportUserDetailsForm.java
1 package org.cacert.gigi.pages.admin.support;
2
3 import java.io.PrintWriter;
4 import java.util.Map;
5 import java.util.Set;
6
7 import javax.servlet.http.HttpServletRequest;
8
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.dbObjects.Group;
11 import org.cacert.gigi.dbObjects.Name;
12 import org.cacert.gigi.dbObjects.SupportedUser;
13 import org.cacert.gigi.dbObjects.User;
14 import org.cacert.gigi.localisation.Language;
15 import org.cacert.gigi.output.ArrayIterable;
16 import org.cacert.gigi.output.DateSelector;
17 import org.cacert.gigi.output.GroupIterator;
18 import org.cacert.gigi.output.GroupSelector;
19 import org.cacert.gigi.output.template.Form;
20 import org.cacert.gigi.output.template.Template;
21
22 public class SupportUserDetailsForm extends Form {
23
24     private static final Template t = new Template(FindUserByDomainForm.class.getResource("SupportUserDetailsForm.templ"));
25
26     private SupportedUser user;
27
28     private DateSelector dobSelector;
29
30     private GroupSelector value = new GroupSelector("groupToModify", true);
31
32     public SupportUserDetailsForm(HttpServletRequest hsr, SupportedUser user) {
33         super(hsr);
34         this.user = user;
35         dobSelector = new DateSelector("dobd", "dobm", "doby", user.getTargetUser().getDoB());
36     }
37
38     @Override
39     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
40         if (user.getTicket() == null) {
41             return false;
42         }
43         if ((req.getParameter("detailupdate") != null ? 1 : 0) + (req.getParameter("addGroup") != null ? 1 : 0) + (req.getParameter("removeGroup") != null ? 1 : 0) + (req.getParameter("resetPass") != null ? 1 : 0) != 1) {
44             throw new GigiApiException("More than one action requested!");
45         }
46         if (req.getParameter("addGroup") != null || req.getParameter("removeGroup") != null) {
47             value.update(req);
48             Group toMod = value.getGroup();
49             if (req.getParameter("addGroup") != null) {
50                 user.grant(toMod);
51             } else {
52                 user.revoke(toMod);
53             }
54             return true;
55         }
56         if (req.getParameter("resetPass") != null) {
57             String aword = req.getParameter("aword");
58             if (aword == null || aword.equals("")) {
59                 throw new GigiApiException("An A-Word is required to perform a password reset.");
60             }
61             user.triggerPasswordReset(aword, out, req);
62             return true;
63         }
64         dobSelector.update(req);
65         if ( !dobSelector.isValid()) {
66             throw new GigiApiException("Invalid date of birth!");
67         }
68         user.setDob(dobSelector.getDate());
69         return true;
70     }
71
72     @Override
73     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
74         User user = this.user.getTargetUser();
75         vars.put("mail", user.getEmail());
76         vars.put("status", l.getTranslation(user.isValidEmail(user.getEmail()) ? "verified" : "not verified"));
77         vars.put("exNames", new ArrayIterable<Name>(user.getNames()) {
78
79             @Override
80             public void apply(Name t, Language l, Map<String, Object> vars) {
81                 vars.put("name", t);
82                 vars.put("points", Integer.toString(t.getAssurancePoints()));
83             }
84
85         });
86         vars.put("assurer", user.canAssure());
87         vars.put("dob", dobSelector);
88         vars.put("assurancepoints", user.getAssurancePoints());
89         vars.put("exppoints", user.getExperiencePoints());
90         final Set<Group> gr = user.getGroups();
91         vars.put("support-groups", new GroupIterator(gr.iterator(), true));
92         vars.put("groups", new GroupIterator(gr.iterator(), false));
93         vars.put("groupSelector", value);
94         t.output(out, l, vars);
95     }
96
97 }