1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.admininterface.dialogs;
35
36 import info.magnolia.cms.beans.config.ContentRepository;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.gui.control.Button;
39 import info.magnolia.cms.gui.control.Edit;
40 import info.magnolia.cms.gui.control.Hidden;
41 import info.magnolia.cms.gui.control.Select;
42 import info.magnolia.cms.gui.dialog.DialogBox;
43 import info.magnolia.cms.gui.dialog.DialogButton;
44 import info.magnolia.cms.gui.dialog.DialogFactory;
45 import info.magnolia.cms.gui.misc.CssConstants;
46 import info.magnolia.cms.i18n.Messages;
47 import info.magnolia.cms.i18n.MessagesManager;
48 import info.magnolia.cms.util.ContentUtil;
49 import info.magnolia.module.admininterface.AdminInterfaceModule;
50 import info.magnolia.module.admininterface.config.AclTypeConfiguration;
51 import info.magnolia.module.admininterface.config.PermissionConfiguration;
52 import info.magnolia.module.admininterface.config.RepositoryConfiguration;
53 import info.magnolia.module.admininterface.config.SecurityConfiguration;
54
55 import java.io.IOException;
56 import java.io.PrintWriter;
57 import java.io.Writer;
58 import java.util.Iterator;
59
60 import javax.jcr.RepositoryException;
61 import javax.servlet.http.HttpServletRequest;
62 import javax.servlet.http.HttpServletResponse;
63
64 import org.apache.commons.collections.map.ListOrderedMap;
65 import org.apache.commons.lang.StringUtils;
66
67
68
69
70
71
72 public class ACLSDialogControl extends DialogBox {
73
74 private static final String CSS_ACL_DIV = "aclDynamicTable";
75
76 private SecurityConfiguration securityConf = AdminInterfaceModule.getInstance().getSecurityConfiguration();
77
78 private static String getHtmlRowInner(String dynamicTable, RepositoryConfiguration repoConf) {
79 boolean small = true;
80 Messages msgs = MessagesManager.getMessages();
81
82 Select accessRight = new Select();
83 accessRight.setSaveInfo(false);
84 accessRight.setName("'+prefix+'AccessRight");
85 accessRight.setCssClass("mgnlDialogControlSelect");
86
87 for (Iterator iter = repoConf.getPermissions().iterator(); iter.hasNext();) {
88 PermissionConfiguration permission = (PermissionConfiguration) iter.next();
89 accessRight.setOptions(escapeJs(permission.getI18nLabel()), Long.toString(permission.getValue()));
90 }
91
92 accessRight.setValue("' + object.accessRight + '");
93
94 Select accessType = new Select();
95 accessType.setSaveInfo(false);
96 accessType.setName("'+prefix+'AccessType");
97 accessType.setCssClass("mgnlDialogControlSelect");
98
99 for (Iterator iter = repoConf.getAclTypes().iterator(); iter.hasNext();) {
100 AclTypeConfiguration patternType = (AclTypeConfiguration) iter.next();
101 accessType.setOptions(escapeJs(patternType.getI18nLabel()), String.valueOf(patternType.getType()));
102 }
103
104 accessType.setValue("' + object.accessType + '");
105
106 Edit path = new Edit();
107 path.setSaveInfo(false);
108 path.setName("'+prefix+'Path");
109 path.setValue("'+object.path+'");
110 path.setCssClass(CssConstants.CSSCLASS_EDIT);
111 path.setCssStyles("width", "100%");
112
113 Button choose = null;
114 if(repoConf.isChooseButton()){
115 choose = new Button();
116 choose.setLabel(escapeJs(msgs.get("buttons.choose")));
117 choose.setOnclick("aclChoose(\\''+prefix+'\\',\\'" + repoConf.getName() + "\\');");
118 choose.setSmall(small);
119 }
120
121 Button delete = new Button();
122 delete.setLabel(escapeJs(msgs.get("buttons.delete")));
123 delete.setOnclick(dynamicTable + ".del('+index+');");
124 delete.setSmall(small);
125
126 StringBuffer html = new StringBuffer();
127
128
129 html.append("<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr>");
130 html.append("<td width=\"1\" class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">").append(accessRight.getHtml()).append("</td>");
131 html.append("<td width=\"1\" class=\"mgnlDialogBoxInput\"></td>");
132
133
134 if(!repoConf.getAclTypes().isEmpty()){
135 html.append("<td width=\"1\" class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">").append(accessType.getHtml()).append("</td>");
136 html.append("<td width=\"1\"></td>");
137 }
138 else {
139 html.append("<input type=\"hidden\" id=\"' + prefix + 'AccessType\" name=\"' + prefix + 'AccessType\" value=\"sub\"/>");
140 }
141
142 html.append("<td width=\"100%\"class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">").append(path.getHtml()).append("</td>");
143 html.append("<td width=\"1\"></td>");
144
145 if (choose != null) {
146 html.append("<td width=\"1\" class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">").append(choose.getHtml()).append("</td>");
147 html.append("<td width=\"1\"></td>");
148 }
149
150 html.append("<td width=\"1\" class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">").append(delete.getHtml()).append("</td>");
151 html.append("</tr></table>");
152
153 return html.toString();
154 }
155
156 public void drawHtml(Writer w) throws IOException {
157 PrintWriter out = (PrintWriter) w;
158 this.drawHtmlPre(out);
159 renderACLS(out);
160 this.drawHtmlPost(out);
161
162 }
163
164 protected void renderACLS(PrintWriter out) throws IOException {
165 Messages msgs = MessagesManager.getMessages();
166 Content role = getStorageNode();
167 HttpServletRequest request = this.getRequest();
168 HttpServletResponse response = this.getResponse();
169
170
171 Select repositorySelect = getRepositorySelect();
172
173 out.print(repositorySelect.getHtml());
174 out.print("<p><p/>");
175
176
177 for (Iterator iter = securityConf.getVisibleRepositories().iterator(); iter.hasNext();) {
178 RepositoryConfiguration repositoryConf = (RepositoryConfiguration) iter.next();
179 try {
180 writeRepositoryTable(request, response, msgs, out, role, repositoryConf);
181 }
182 catch (RepositoryException e) {
183 throw new RuntimeException("can't list ", e);
184 }
185 }
186
187
188 out.println("<script type=\"text/javascript\">aclChangeRepository('website');</script>");
189 }
190
191
192
193
194
195
196
197
198 protected void writeRepositoryTable(HttpServletRequest request, HttpServletResponse response, Messages msgs,
199 PrintWriter out, Content role, RepositoryConfiguration repoConf) throws RepositoryException, IOException {
200 String tableName = "acl" + repoConf.getName() + "Table";
201 String dynamicTableName = "acl" + repoConf.getName() + "DynamicTable";
202 String hiddenFieldName = "acl" + repoConf.getName() + "List";
203
204 out.println("<div id=\"acl" + repoConf.getName() + "Div\" class=\"" + CSS_ACL_DIV + "\">");
205 out.println(new Hidden(hiddenFieldName, StringUtils.EMPTY, false).getHtml());
206
207
208 out.println("<table id=\""
209 + tableName
210 + "\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td></td></tr></table>");
211
212
213 out.println("<table width=\"100%\">");
214 DialogButton add = DialogFactory.getDialogButtonInstance(request, response, null, null);
215 add.setBoxType(DialogBox.BOXTYPE_1COL);
216 add.setConfig("buttonLabel", msgs.get("buttons.add"));
217 add.setConfig("onclick", dynamicTableName + ".addNew();");
218 add.drawHtml(out);
219 out.println("</table>");
220
221 out.println("</div>");
222
223 out.println("<script type=\"text/javascript\">");
224
225 out.println("aclRepositories[aclRepositories.length]= '" + repoConf.getName() + "';");
226
227
228 out.println("function acl" + repoConf.getName() + "RenderFunction(cell, prefix, index, object)");
229 out.println("{");
230
231
232 out.println("mgnlDebug('acl" + repoConf.getName() + "RenderFunction: prefix = ' + prefix, 'acl', object)");
233 out.println("cell.innerHTML= '" + getHtmlRowInner(dynamicTableName, repoConf) + "';\n");
234 out.println("document.getElementById(prefix + 'AccessType').value = object.accessType;\n");
235 out.println("document.getElementById(prefix + 'AccessRight').value = object.accessRight;\n");
236
237 out.println("}");
238
239
240 out.println(dynamicTableName + " = new MgnlDynamicTable('"
241 + tableName
242 + "',document.getElementById('mgnlFormMain')."
243 + hiddenFieldName
244 + ", aclGetNewPermissionObject, aclGetPermissionObject, acl"
245 + repoConf.getName()
246 + "RenderFunction, null);");
247
248
249
250 addExistingAclToTable(out, role, dynamicTableName, repoConf);
251
252 out.println("</script>");
253 }
254
255
256
257
258
259 private void addExistingAclToTable(PrintWriter out, Content role, String dynamicTableName,
260 RepositoryConfiguration repoConf) {
261
262 ACLS acls = new ACLS();
263
264 Content aclsNode = ContentUtil.getContent(role, "acl_" + repoConf.getName());
265 if (aclsNode == null || aclsNode.getChildren().size() == 0) {
266 out.println(dynamicTableName + ".addNew();");
267 return;
268 }
269
270 Iterator it = aclsNode.getChildren().iterator();
271 while (it.hasNext()) {
272 Content c = (Content) it.next();
273 String path = c.getNodeData("path").getString();
274 String accessRight = c.getNodeData("permissions").getString();
275 acls.register(path, Integer.valueOf(accessRight).intValue(), repoConf);
276 }
277
278 for (Iterator iter = acls.values().iterator(); iter.hasNext();) {
279 ACL acl = (ACL) iter.next();
280 out.println(dynamicTableName + ".add({accessRight:"
281 + acl.accessRight
282 + ",accessType:'"
283 + acl.type
284 + "',path:'"
285 + acl.path
286 + "'});");
287 }
288 }
289
290 private Select getRepositorySelect() {
291 Select repositorySelect = new Select();
292 repositorySelect.setName("aclRepository");
293 repositorySelect.setCssClass("mgnlDialogControlSelect");
294 repositorySelect.setEvent("onchange", "aclChangeRepository(this.value)");
295 repositorySelect.setSaveInfo(false);
296 repositorySelect.setValue(ContentRepository.WEBSITE);
297
298 for (Iterator iter = securityConf.getVisibleRepositories().iterator(); iter.hasNext();) {
299 RepositoryConfiguration repoConf = (RepositoryConfiguration) iter.next();
300 repositorySelect.setOptions(repoConf.getI18nLabel(), repoConf.getName());
301 }
302 return repositorySelect;
303 }
304
305 private static String escapeJs(String value) {
306 return StringUtils.replace(value, "'", "\\'");
307 }
308
309
310
311
312
313
314 protected class ACL {
315
316 int type = 0;
317
318 String path;
319
320 int accessRight;
321
322 void registerEntry(String path) {
323 if (path.equals("/*")) {
324 type = AclTypeConfiguration.TYPE_ALL;
325 }
326 else if (path.endsWith("/*")) {
327 type = type | AclTypeConfiguration.TYPE_SUBS;
328 }
329 else {
330 type = type | AclTypeConfiguration.TYPE_THIS;
331 }
332 }
333 }
334
335
336
337
338
339
340 protected class ACLS extends ListOrderedMap {
341
342
343
344
345
346
347 void register(String path, int accessRight, RepositoryConfiguration repoConf) {
348 String cleanPath = repoConf.toViewPattern(path);
349
350 String key = cleanPath + ":" + accessRight;
351 if (!this.containsKey(key)) {
352 ACL acl = new ACL();
353 acl.path = cleanPath;
354 acl.accessRight = accessRight;
355 this.put(key, acl);
356 }
357 ((ACL) this.get(key)).registerEntry(path);
358 }
359 }
360
361 }