1 package info.magnolia.module.admininterface.dialogpages; 2 3 import info.magnolia.cms.beans.config.ContentRepository; 4 import info.magnolia.cms.core.Content; 5 import info.magnolia.cms.gui.control.Button; 6 import info.magnolia.cms.gui.control.Edit; 7 import info.magnolia.cms.gui.control.Hidden; 8 import info.magnolia.cms.gui.control.Select; 9 import info.magnolia.cms.gui.dialog.DialogBox; 10 import info.magnolia.cms.gui.dialog.DialogButton; 11 import info.magnolia.cms.gui.dialog.DialogFactory; 12 import info.magnolia.cms.gui.dialog.DialogSuper; 13 import info.magnolia.cms.gui.misc.CssConstants; 14 import info.magnolia.cms.i18n.Messages; 15 import info.magnolia.cms.i18n.MessagesManager; 16 import info.magnolia.cms.security.Permission; 17 import info.magnolia.module.admininterface.DialogPageMVCHandler; 18 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.util.Iterator ; 22 23 import javax.jcr.RepositoryException; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.commons.lang.StringUtils; 28 29 30 34 public class UserRolesEditIncludeAclDialogPage extends DialogPageMVCHandler { 35 36 public UserRolesEditIncludeAclDialogPage(String name, HttpServletRequest request, HttpServletResponse response) { 37 super(name, request, response); 38 } 39 40 43 private static final long serialVersionUID = 222L; 44 45 private static final long PERMISSION_ALL = Permission.ALL; 47 48 private static final long PERMISSION_READ = Permission.READ; 49 50 private static final long PERMISSION_NO = 0; 51 52 private static final String CSS_ACL_DIV = "aclDynamicTable"; 54 private static String getHtmlRowInner(HttpServletRequest request, String dynamicTable, String repository) { 55 boolean small = true; 56 Messages msgs = MessagesManager.getMessages(request); 57 58 Select accessRight = new Select(); 59 accessRight.setSaveInfo(false); 60 accessRight.setName("'+prefix+'AccessRight"); accessRight.setCssClass("mgnlDialogControlSelect"); accessRight.setOptions(msgs.get("roles.permission.readWrite"), Long.toString(PERMISSION_ALL)); accessRight.setOptions(msgs.get("roles.permission.readOnly"), Long.toString(PERMISSION_READ)); accessRight.setOptions(msgs.get("roles.permission.deny"), Long.toString(PERMISSION_NO)); accessRight.setValue("' + object.accessRight + '"); 67 Select accessType = new Select(); 68 accessType.setSaveInfo(false); 69 accessType.setName("'+prefix+'AccessType"); accessType.setCssClass("mgnlDialogControlSelect"); if (repository.equals(ContentRepository.WEBSITE)) { 72 accessType.setOptions(msgs.get("roles.edit.thisAndSubPages"), "self"); accessType.setOptions(msgs.get("roles.edit.subPages"), "sub"); } 75 else { 76 accessType.setOptions(msgs.get("roles.edit.thisAndSubNodes"), "self"); accessType.setOptions(msgs.get("roles.edit.subNodes"), "sub"); } 79 accessType.setValue("' + object.accessType + '"); 81 Edit path = new Edit(); 82 path.setSaveInfo(false); 83 path.setName("'+prefix+'Path"); path.setValue("'+object.path+'"); path.setCssClass(CssConstants.CSSCLASS_EDIT); 86 path.setCssStyles("width", "100%"); 88 Button choose = new Button(); 89 choose.setLabel(msgs.get("buttons.choose")); choose.setOnclick("aclChoose(\\''+prefix+'\\',\\'" + repository + "\\');"); choose.setSmall(small); 92 93 Button delete = new Button(); 94 delete.setLabel(msgs.get("buttons.delete")); delete.setOnclick(dynamicTable + ".del('+index+');"); delete.setSmall(small); 97 98 StringBuffer html = new StringBuffer (); 99 html.append("<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr>"); html.append("<td width=\"1\" class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">" + accessRight.getHtml() + "</td>"); html.append("<td width=\"1\" class=\"mgnlDialogBoxInput\"></td>"); 107 if (!repository.equals(ContentRepository.USERS) && !repository.equals(ContentRepository.USER_ROLES)) { 108 html.append("<td width=\"1\" class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">" + accessType.getHtml() + "</td>"); html.append("<td width=\"1\"></td>"); } 113 else { 114 html 115 .append("<input type=\"hidden\" id=\"' + prefix + 'AccessType\" name=\"' + prefix + 'AccessType\" value=\"sub\"/>"); } 117 118 html.append("<td width=\"100%\"class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">" + path.getHtml() + "</td>"); html.append("<td width=\"1\"></td>"); html.append("<td width=\"1\" class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">" + choose.getHtml() + "</td>"); html.append("<td width=\"1\"></td>"); html.append("<td width=\"1\" class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">" + delete.getHtml() + "</td>"); html.append("</tr></table>"); 131 return html.toString(); 132 } 133 134 138 protected void draw(HttpServletRequest request, HttpServletResponse response) throws Exception { 139 PrintWriter out = response.getWriter(); 140 Messages msgs = MessagesManager.getMessages(request); 141 142 DialogSuper dialogControl = (DialogSuper) request.getAttribute("dialogObject"); Content role = dialogControl.getWebsiteNode(); 144 145 Select repositorySelect = getReposiotySelect(request); 147 148 out.print(repositorySelect.getHtml()); 149 out.print("<p><p/>"); for (int i = 0; i < ContentRepository.getAllRepositoryNames().length; i++) { 151 writeRepositoryTable(request, response, msgs, out, role, ContentRepository.getAllRepositoryNames()[i]); 152 } 153 out.println("<script>aclChangeRepository('website');</script>"); 156 } 157 158 165 private void writeRepositoryTable(HttpServletRequest request, HttpServletResponse response, Messages msgs, 166 PrintWriter out, Content role, String repository) throws RepositoryException, IOException { 167 String tableName = "acl" + repository + "Table"; String dynamicTableName = "acl" + repository + "DynamicTable"; String hiddenFieldName = "acl" + repository + "List"; 171 out.println("<div id=\"acl" + repository + "Div\" class=\"" + CSS_ACL_DIV + "\">"); out.println(new Hidden(hiddenFieldName, StringUtils.EMPTY, false).getHtml()); 173 174 out.println("<table id=\"" + tableName 177 + "\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td></td></tr></table>"); 179 181 out.println("<table width=\"100%\">"); DialogButton add = DialogFactory.getDialogButtonInstance(request, response, null, null); 183 add.setBoxType(DialogBox.BOXTYPE_1COL); 184 add.setConfig("buttonLabel", msgs.get("buttons.add")); add.setConfig("onclick", dynamicTableName + ".addNew();"); add.drawHtml(out); 187 out.println("</table>"); 189 out.println("</div>"); 191 out.println("<script type=\"text/javascript\">"); out.println("aclRepositories[aclRepositories.length]= '" + repository + "';"); 195 out.println("function acl" + repository + "RenderFunction(cell, prefix, index, object)"); out.println("{"); 199 out.println("mgnlDebug('acl" + repository + "RenderFunction: prefix = ' + prefix, 'acl', object)"); 201 out.println("cell.innerHTML= '" + getHtmlRowInner(request, dynamicTableName, repository) + "';\n"); out.println("document.getElementById(prefix + 'AccessType').value = object.accessType;\n"); 204 out.println("document.getElementById(prefix + 'AccessRight').value = object.accessRight;\n"); 206 out.println("}"); 208 out.println(dynamicTableName + " = new MgnlDynamicTable('" + tableName + "',document.mgnlFormMain." + hiddenFieldName + ", aclGetNewPermissionObject, aclGetPermissionObject, acl" + repository + "RenderFunction, null);"); 214 addExistingAclToTable(out, role, dynamicTableName, repository); 217 218 out.println("</script>"); } 220 221 225 private void addExistingAclToTable(PrintWriter out, Content role, String dynamicTableName, String repository) { 226 boolean noAcl = false; 227 try { 228 Content acl = role.getContent("acl_" + repository); if (acl.getChildren().size() == 0) { 230 noAcl = true; 231 } 232 Iterator it = acl.getChildren().iterator(); 233 boolean skipNext = false; 234 while (it.hasNext()) { 235 Content c = (Content) it.next(); 236 237 if (skipNext) { 238 skipNext = false; 239 } 240 else { 241 String path = c.getNodeData("path").getString(); String accessRight = c.getNodeData("permissions").getString(); String accessType; 244 245 if (!StringUtils.contains(path, "/*")) { skipNext = true; 248 accessType = "self"; } 250 else { 251 if (path.equals("/*")) { path = "/"; accessType = "self"; } 255 else { 256 path = StringUtils.substringBeforeLast(path, "/*"); accessType = "sub"; } 259 } 260 261 out.println(dynamicTableName + ".add({accessRight:" + accessRight + ",accessType:'" + accessType + "',path:'" + path + "'});"); } 266 } 267 } 268 catch (Exception e) { 269 noAcl = true; 270 } 271 if (noAcl) { 272 out.println(dynamicTableName + ".addNew();"); } 274 } 275 276 280 private Select getReposiotySelect(HttpServletRequest request) { 281 Select repositorySelect = new Select(); 282 repositorySelect.setName("aclRepository"); repositorySelect.setCssClass("mgnlDialogControlSelect"); repositorySelect.setEvent("onchange", "aclChangeRepository(this.value)"); repositorySelect.setSaveInfo(false); 286 repositorySelect.setValue(ContentRepository.WEBSITE); 287 288 for (int i = 0; i < ContentRepository.getAllRepositoryNames().length; i++) { 290 String name = ContentRepository.getAllRepositoryNames()[i]; 291 String label = MessagesManager.get(request, "repository." + name); repositorySelect.setOptions(label, name); 293 } 294 return repositorySelect; 295 } 296 } | Popular Tags |