]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/orga/ViewOrgPage.java
Adding new fields to organisation account
[gigi.git] / src / org / cacert / gigi / pages / orga / ViewOrgPage.java
1 package org.cacert.gigi.pages.orga;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.dbObjects.Organisation;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.template.Form;
17 import org.cacert.gigi.output.template.IterableDataset;
18 import org.cacert.gigi.output.template.Template;
19 import org.cacert.gigi.pages.LoginPage;
20 import org.cacert.gigi.pages.Page;
21 import org.cacert.gigi.pages.account.domain.DomainManagementForm;
22 import org.cacert.gigi.util.AuthorizationContext;
23
24 public class ViewOrgPage extends Page {
25
26     private final Template orgas = new Template(ViewOrgPage.class.getResource("ViewOrgs.templ"));
27
28     private final Template mainTempl = new Template(ViewOrgPage.class.getResource("EditOrg.templ"));
29
30     public static final String DEFAULT_PATH = "/orga";
31
32     public ViewOrgPage() {
33         super("View Organisation");
34     }
35
36     @Override
37     public boolean isPermitted(AuthorizationContext ac) {
38         return ac != null && (ac.isInGroup(CreateOrgPage.ORG_ASSURER) || ac.getActor().getOrganisations(true).size() != 0);
39     }
40
41     @Override
42     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
43         try {
44             User u = LoginPage.getUser(req);
45             if (req.getParameter("do_affiliate") != null || req.getParameter("del") != null) {
46                 AffiliationForm form = Form.getForm(req, AffiliationForm.class);
47                 if (form.submit(resp.getWriter(), req)) {
48                     resp.sendRedirect(DEFAULT_PATH + "/" + form.getOrganisation().getId());
49                 }
50                 return;
51             } else {
52                 if ( !u.isInGroup(CreateOrgPage.ORG_ASSURER)) {
53                     resp.sendError(403, "Access denied");
54                     return;
55                 }
56
57                 if (req.getParameter("addDomain") != null) {
58                     OrgDomainAddForm form = Form.getForm(req, OrgDomainAddForm.class);
59                     if (form.submit(resp.getWriter(), req)) {
60                         resp.sendRedirect(DEFAULT_PATH + "/" + form.getOrganisation().getId());
61                     }
62                 } else if (req.getParameter("delete") != null) {
63                     DomainManagementForm form = Form.getForm(req, DomainManagementForm.class);
64                     if (form.submit(resp.getWriter(), req)) {
65                         resp.sendRedirect(DEFAULT_PATH + "/" + form.getTarget().getId());
66                     }
67                 } else {
68                     CreateOrgForm form = Form.getForm(req, CreateOrgForm.class);
69                     if (form.submit(resp.getWriter(), req)) {
70                         resp.sendRedirect(DEFAULT_PATH + "/" + form.getResult().getId());
71                     }
72                 }
73             }
74
75         } catch (GigiApiException e) {
76             e.format(resp.getWriter(), getLanguage(req));
77         }
78     }
79
80     @Override
81     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
82         User u = LoginPage.getUser(req);
83         String idS = req.getPathInfo();
84         Language lang = getLanguage(req);
85         PrintWriter out = resp.getWriter();
86         if (idS.length() < DEFAULT_PATH.length() + 2) {
87             final Organisation[] orgas = Organisation.getOrganisations(0, 30);
88             HashMap<String, Object> map = new HashMap<>();
89             final List<Organisation> myOrgs = u.getOrganisations(true);
90             final boolean orgAss = u.isInGroup(CreateOrgPage.ORG_ASSURER);
91             if (orgAss) {
92                 map.put("orgas", makeOrgDataset(orgas));
93             } else {
94                 map.put("orgas", makeOrgDataset(myOrgs.toArray(new Organisation[myOrgs.size()])));
95             }
96             this.orgas.output(out, lang, map);
97             return;
98         }
99         idS = idS.substring(DEFAULT_PATH.length() + 1);
100         int id = Integer.parseInt(idS);
101         Organisation o;
102         try {
103             o = Organisation.getById(id);
104         } catch (IllegalArgumentException e) {
105             resp.sendError(404);
106             return;
107         }
108         final List<Organisation> myOrgs = u.getOrganisations();
109         final boolean orgAss = u.isInGroup(CreateOrgPage.ORG_ASSURER);
110         if ( !orgAss && !myOrgs.contains(o)) {
111             resp.sendError(404);
112             return;
113         }
114         HashMap<String, Object> vars = new HashMap<>();
115         if (orgAss) {
116             vars.put("editForm", new CreateOrgForm(req, o));
117             vars.put("affForm", new AffiliationForm(req, o));
118             vars.put("mgmDom", new DomainManagementForm(req, o, true));
119             vars.put("addDom", new OrgDomainAddForm(req, o));
120         } else {
121             vars.put("affForm", new AffiliationForm(req, o));
122             vars.put("orgName", o.getName());
123         }
124         mainTempl.output(out, lang, vars);
125     }
126
127     private IterableDataset makeOrgDataset(final Organisation[] orgas) {
128         return new IterableDataset() {
129
130             int count = 0;
131
132             @Override
133             public boolean next(Language l, Map<String, Object> vars) {
134                 if (count >= orgas.length) {
135                     return false;
136                 }
137                 Organisation org = orgas[count++];
138                 vars.put("id", Integer.toString(org.getId()));
139                 vars.put("name", org.getName());
140                 vars.put("country", org.getState());
141                 return true;
142             }
143         };
144     }
145 }