KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > dialogs > RolesEditDialog


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.module.admininterface.dialogs;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.ItemType;
18 import info.magnolia.cms.core.Path;
19 import info.magnolia.cms.gui.dialog.Dialog;
20 import info.magnolia.module.admininterface.SaveHandler;
21 import info.magnolia.module.admininterface.pages.RolesACLPage;
22
23 import java.util.Iterator JavaDoc;
24
25 import javax.jcr.RepositoryException;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34 /**
35  * @author Fabrizio Giustina
36  * @version $Id: RolesEditDialog.java 6341 2006-09-12 09:18:27Z philipp $
37  */

38 public class RolesEditDialog extends ConfiguredDialog {
39
40     protected static Logger log = LoggerFactory.getLogger("roles dialog"); //$NON-NLS-1$
41

42     /**
43      * Stable serialVersionUID.
44      */

45     private static final long serialVersionUID = 222L;
46
47     /**
48      * @param name
49      * @param request
50      * @param response
51      * @param configNode
52      */

53     public RolesEditDialog(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Content configNode) {
54         super(name, request, response, configNode);
55     }
56
57     public String JavaDoc getRepository() {
58         String JavaDoc repository = super.getRepository();
59         if (repository == null) {
60             repository = ContentRepository.USER_ROLES;
61         }
62         return repository;
63     }
64
65     /*
66      * (non-Javadoc)
67      * @see info.magnolia.module.admininterface.DialogMVCHandler#createDialog(info.magnolia.cms.core.Content,
68      * info.magnolia.cms.core.Content)
69      */

70     protected Dialog createDialog(Content configNode, Content storageNode) throws RepositoryException {
71
72         Dialog dialog = super.createDialog(configNode, storageNode);
73
74         dialog.setJavascriptSources(request.getContextPath() + "/.resources/admin-js/dialogs/DynamicTable.js"); //$NON-NLS-1$
75
dialog.setJavascriptSources(request.getContextPath() + "/.resources/admin-js/dialogs/pages/rolesACLPage.js"); //$NON-NLS-1$
76
dialog.setCssSources(request.getContextPath() + "/.resources/admin-css/dialogs/pages/rolesEditPage.css"); //$NON-NLS-1$
77
return dialog;
78     }
79
80     /**
81      * @see info.magnolia.module.admininterface.DialogMVCHandler#configureSaveHandler(info.magnolia.module.admininterface.SaveHandler)
82      */

83     protected void configureSaveHandler(SaveHandler save) {
84         super.configureSaveHandler(save);
85         save.setPath(path);
86     }
87
88     protected boolean onPostSave(SaveHandler saveControl) {
89         Content role = this.getStorageNode();
90
91         // for each repository
92
Iterator JavaDoc repositoryNames = ContentRepository.getAllRepositoryNames();
93         while (repositoryNames.hasNext()) {
94             String JavaDoc repository = (String JavaDoc) repositoryNames.next();
95
96             // ######################
97
// # acl
98
// ######################
99
// remove existing
100
try {
101                 role.delete("acl_" + repository); //$NON-NLS-1$
102
}
103             catch (RepositoryException re) {
104                 // ignore, not existing
105
}
106             // rewrite
107
try {
108                 Content acl = role.createContent("acl_" + repository, ItemType.CONTENTNODE); //$NON-NLS-1$
109
String JavaDoc aclValueStr = form.getParameter("acl" + repository + "List"); //$NON-NLS-1$ //$NON-NLS-2$
110
if (StringUtils.isNotEmpty(aclValueStr)) {
111                     String JavaDoc[] aclEntries = aclValueStr.split(";"); //$NON-NLS-1$
112
for (int i = 0; i < aclEntries.length; i++) {
113                         String JavaDoc path = StringUtils.EMPTY;
114                         long accessRight = 0;
115                         int accessType = 0;
116
117                         String JavaDoc[] aclValuePairs = aclEntries[i].split(","); //$NON-NLS-1$
118
for (int j = 0; j < aclValuePairs.length; j++) {
119                             String JavaDoc[] aclValuePair = aclValuePairs[j].split(":"); //$NON-NLS-1$
120
String JavaDoc aclName = aclValuePair[0].trim();
121                             String JavaDoc aclValue = StringUtils.EMPTY;
122                             if (aclValuePair.length > 1) {
123                                 aclValue = aclValuePair[1].trim();
124                             }
125
126                             if (aclName.equals("path")) { //$NON-NLS-1$
127
path = aclValue;
128                             }
129                             else if (aclName.equals("accessType")) { //$NON-NLS-1$
130
accessType = Integer.valueOf(aclValue).intValue();
131                             }
132                             else if (aclName.equals("accessRight")) { //$NON-NLS-1$
133
try {
134                                     accessRight = Long.parseLong(aclValue);
135                                 }
136                                 catch (NumberFormatException JavaDoc e) {
137                                     accessRight = 0;
138                                 }
139                             }
140                         }
141
142                         if (StringUtils.isNotEmpty(path)) {
143                             if (path.equals("/")) { //$NON-NLS-1$
144
accessType = RolesACLPage.TYPE_SUBS;
145                                 path = StringUtils.EMPTY;
146                             }
147
148                             if ((accessType & RolesACLPage.TYPE_THIS) != 0) {
149                                 try {
150                                     String JavaDoc newLabel = Path.getUniqueLabel(hm, acl.getHandle(), "0"); //$NON-NLS-1$
151
Content r = acl.createContent(newLabel, ItemType.CONTENTNODE);
152                                     r.createNodeData("path").setValue(path); //$NON-NLS-1$
153
r.createNodeData("permissions").setValue(accessRight); //$NON-NLS-1$
154
}
155                                 catch (Exception JavaDoc e) {
156                                     log.error(e.getMessage(), e);
157                                 }
158                             }
159
160                             if ((accessType & RolesACLPage.TYPE_SUBS) != 0) {
161                                 try {
162                                     String JavaDoc newLabel = Path.getUniqueLabel(hm, acl.getHandle(), "0"); //$NON-NLS-1$
163
Content r = acl.createContent(newLabel, ItemType.CONTENTNODE);
164                                     r.createNodeData("path").setValue(path + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
165
r.createNodeData("permissions").setValue(accessRight); //$NON-NLS-1$
166
}
167                                 catch (Exception JavaDoc e) {
168                                     log.error(e.getMessage(), e);
169                                 }
170                             }
171                         }
172                     }
173                 }
174                 hm.save();
175             }
176             catch (RepositoryException re) {
177                 log.error(re.getMessage(), re);
178             }
179         }
180         return true;
181     }
182
183 }
Popular Tags