71909cf24b48d0793262125897a96dc038c45db1
[gigi.git] / src / club / wpia / gigi / pages / admin / support / SupportUserDetailsForm.java
1 package club.wpia.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 club.wpia.gigi.Gigi;
10 import club.wpia.gigi.GigiApiException;
11 import club.wpia.gigi.dbObjects.Group;
12 import club.wpia.gigi.dbObjects.Name;
13 import club.wpia.gigi.dbObjects.SupportedUser;
14 import club.wpia.gigi.dbObjects.User;
15 import club.wpia.gigi.localisation.Language;
16 import club.wpia.gigi.output.ArrayIterable;
17 import club.wpia.gigi.output.DateSelector;
18 import club.wpia.gigi.output.GroupList;
19 import club.wpia.gigi.output.GroupSelector;
20 import club.wpia.gigi.output.template.Form;
21 import club.wpia.gigi.output.template.Template;
22 import club.wpia.gigi.output.template.TranslateCommand;
23 import club.wpia.gigi.pages.LoginPage;
24 import club.wpia.gigi.pages.account.MyDetails;
25 import club.wpia.gigi.util.AuthorizationContext;
26
27 public class SupportUserDetailsForm extends Form {
28
29     private static final Template t = new Template(FindUserByDomainForm.class.getResource("SupportUserDetailsForm.templ"));
30
31     private SupportedUser user;
32
33     private DateSelector dobSelector;
34
35     private GroupSelector value = new GroupSelector("groupToModify", true);
36
37     public SupportUserDetailsForm(HttpServletRequest hsr, SupportedUser user) {
38         super(hsr);
39         this.user = user;
40         dobSelector = new DateSelector("dobd", "dobm", "doby", user.getTargetUser().getDoB());
41     }
42
43     @Override
44     public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
45         if (user.getTicket() == null) {
46             throw new GigiApiException("No ticket number set.");
47         }
48
49         int numActions = 0;
50         numActions += req.getParameter("detailupdate") != null ? 1 : 0;
51         numActions += req.getParameter("addGroup") != null ? 1 : 0;
52         numActions += req.getParameter("removeGroup") != null ? 1 : 0;
53         numActions += req.getParameter("resetPass") != null ? 1 : 0;
54
55         if (numActions != 1) {
56             throw new GigiApiException("More than one action requested!");
57         }
58
59         if (user.getTargetUser() == LoginPage.getUser(req)) {
60             if (req.getParameter("removeGroup") != null) {
61                 value.update(req);
62                 Group toMod = value.getGroup();
63                 if (toMod == Group.SUPPORTER) {
64                     user.revoke(toMod);
65                     AuthorizationContext ac = LoginPage.getAuthorizationContext(req);
66                     req.getSession().setAttribute(Gigi.AUTH_CONTEXT, new AuthorizationContext(ac.getActor(), ac.getActor(), ac.isStronglyAuthenticated()));
67                     return new RedirectResult(MyDetails.PATH);
68                 }
69             }
70             throw new GigiApiException("Supporter may not modify himself.");
71         }
72
73         if (req.getParameter("addGroup") != null || req.getParameter("removeGroup") != null) {
74             value.update(req);
75             Group toMod = value.getGroup();
76             if (req.getParameter("addGroup") != null) {
77                 user.grant(toMod);
78             } else {
79                 user.revoke(toMod);
80             }
81             return new RedirectResult(req.getPathInfo());
82         }
83         if (req.getParameter("resetPass") != null) {
84             String aword = req.getParameter("aword");
85             if (aword == null || aword.equals("")) {
86                 throw new GigiApiException("An A-Word is required to perform a password reset.");
87             }
88             user.triggerPasswordReset(aword, req);
89             return new SuccessMessageResult(new TranslateCommand("Password reset successful."));
90         }
91         dobSelector.update(req);
92         if ( !dobSelector.isValid()) {
93             throw new GigiApiException("Invalid date of birth!");
94         }
95         user.setDob(dobSelector.getDate());
96         return new RedirectResult(req.getPathInfo());
97     }
98
99     @Override
100     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
101         User user = this.user.getTargetUser();
102         vars.put("mail", user.getEmail());
103         vars.put("status", l.getTranslation(user.isValidEmail(user.getEmail()) ? "verified" : "not verified"));
104         vars.put("exNames", new ArrayIterable<Name>(user.getNames()) {
105
106             @Override
107             public void apply(Name t, Language l, Map<String, Object> vars) {
108                 vars.put("name", t);
109                 vars.put("preferred", t.getOwner().getPreferredName() == t);
110                 vars.put("points", Integer.toString(t.getVerificationPoints()));
111             }
112
113         });
114         vars.put("agent", user.canVerify());
115         vars.put("dob", dobSelector);
116         vars.put("verificationPoints", user.getVerificationPoints());
117         vars.put("exppoints", user.getExperiencePoints());
118         final Set<Group> gr = user.getGroups();
119         vars.put("support-groups", new GroupList(gr, true));
120         vars.put("groups", new GroupList(gr, false));
121         vars.put("groupSelector", value);
122         t.output(out, l, vars);
123     }
124
125 }