add: add boolean to separate groups for user and support
[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.Iterator;
5 import java.util.Map;
6 import java.util.Set;
7
8 import javax.servlet.http.HttpServletRequest;
9
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.dbObjects.Group;
12 import org.cacert.gigi.dbObjects.Name;
13 import org.cacert.gigi.dbObjects.SupportedUser;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.ArrayIterable;
17 import org.cacert.gigi.output.DateSelector;
18 import org.cacert.gigi.output.GroupSelector;
19 import org.cacert.gigi.output.template.Form;
20 import org.cacert.gigi.output.template.IterableDataset;
21 import org.cacert.gigi.output.template.Outputable;
22 import org.cacert.gigi.output.template.SprintfCommand;
23 import org.cacert.gigi.output.template.Template;
24 import org.cacert.gigi.output.template.TranslateCommand;
25 import org.cacert.gigi.pages.PasswordResetPage;
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 boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
45         if (user.getTicket() == null) {
46             return false;
47         }
48         if ((req.getParameter("detailupdate") != null ? 1 : 0) + (req.getParameter("grant") != null ? 1 : 0) + (req.getParameter("deny") != null ? 1 : 0) + (req.getParameter("resetPass") != null ? 1 : 0) != 1) {
49             throw new GigiApiException("More than one action requested!");
50         }
51         if (req.getParameter("grant") != null || req.getParameter("deny") != null) {
52             String actionType = "granted";
53             value.update(req);
54             Group toMod = value.getGroup();
55             if (req.getParameter("grant") != null) {
56                 user.grant(toMod);
57             } else {
58                 actionType = "revoked";
59                 user.revoke(toMod);
60             }
61             String subject = "Change Group Permissions";
62             Outputable message = SprintfCommand.createSimple("The group permission {0} was {1}.", toMod.getDatabaseName(), actionType);
63             user.sendSupportNotification(subject, message);
64             return true;
65         }
66         if (req.getParameter("resetPass") != null) {
67             String aword = req.getParameter("aword");
68             if (aword == null || aword.equals("")) {
69                 throw new GigiApiException("An A-Word is required to perform a password reset.");
70             }
71             Language l = Language.getInstance(user.getTargetUser().getPreferredLocale());
72             String method = l.getTranslation("A password reset was triggered. Please enter the required text sent to you by support on this page:");
73             String subject = l.getTranslation("Password reset by support.");
74             PasswordResetPage.initPasswordResetProcess(out, user.getTargetUser(), req, aword, l, method, subject);
75             Outputable message = new TranslateCommand("A password reset was triggered and an email was sent to user.");
76             user.sendSupportNotification(subject, message);
77             return true;
78         }
79         dobSelector.update(req);
80         if ( !dobSelector.isValid()) {
81             throw new GigiApiException("Invalid date of birth!");
82         }
83         user.setDob(dobSelector.getDate());
84
85         String subject = "Change Account Data";
86         Outputable message = new TranslateCommand("The account data was changed.");
87         user.sendSupportNotification(subject, message);
88         return true;
89     }
90
91     @Override
92     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
93         User user = this.user.getTargetUser();
94         vars.put("mail", user.getEmail());
95         vars.put("status", l.getTranslation(user.isValidEmail(user.getEmail()) ? "verified" : "not verified"));
96         vars.put("exNames", new ArrayIterable<Name>(user.getNames()) {
97
98             @Override
99             public void apply(Name t, Language l, Map<String, Object> vars) {
100                 vars.put("name", t);
101                 vars.put("points", Integer.toString(t.getAssurancePoints()));
102             }
103
104         });
105         vars.put("assurer", user.canAssure());
106         vars.put("dob", dobSelector);
107         vars.put("assurancepoints", user.getAssurancePoints());
108         vars.put("exppoints", user.getExperiencePoints());
109         final Set<Group> gr = user.getGroups();
110         vars.put("groups", new IterableDataset() {
111
112             Iterator<Group> i = gr.iterator();
113
114             @Override
115             public boolean next(Language l, Map<String, Object> vars) {
116                 if ( !i.hasNext()) {
117                     return false;
118                 }
119                 Group g = i.next();
120                 vars.put("group_name", g.getName());
121                 return true;
122             }
123         });
124         vars.put("groupSelector", value);
125         t.output(out, l, vars);
126     }
127
128 }