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.core.ItemType;
39 import info.magnolia.cms.core.Path;
40 import info.magnolia.cms.gui.dialog.Dialog;
41 import info.magnolia.module.admininterface.SaveHandler;
42 import info.magnolia.module.admininterface.config.AclTypeConfiguration;
43
44 import java.util.Iterator;
45
46 import javax.jcr.RepositoryException;
47 import javax.jcr.PathNotFoundException;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
51 import org.apache.commons.lang.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55
56
57
58
59
60 public class RolesEditDialog extends ConfiguredDialog {
61
62 protected static Logger log = LoggerFactory.getLogger("roles dialog");
63
64
65
66
67 private static final long serialVersionUID = 222L;
68
69
70
71
72
73
74
75 public RolesEditDialog(String name, HttpServletRequest request, HttpServletResponse response, Content configNode) {
76 super(name, request, response, configNode);
77 }
78
79 public String getRepository() {
80 String repository = super.getRepository();
81 if (repository == null) {
82 repository = ContentRepository.USER_ROLES;
83 }
84 return repository;
85 }
86
87
88
89
90
91
92 protected Dialog createDialog(Content configNode, Content storageNode) throws RepositoryException {
93
94 Dialog dialog = super.createDialog(configNode, storageNode);
95
96 dialog.setJavascriptSources(request.getContextPath() + "/.resources/admin-js/dialogs/pages/rolesACLPage.js");
97 dialog.setCssSources(request.getContextPath() + "/.resources/admin-css/dialogs/pages/rolesEditPage.css");
98 return dialog;
99 }
100
101
102
103
104 protected void configureSaveHandler(SaveHandler save) {
105 super.configureSaveHandler(save);
106 save.setPath(path);
107 }
108
109 protected boolean onPostSave(SaveHandler saveControl) {
110 Content role = this.getStorageNode();
111 try {
112 saveACLs(role, "uri");
113
114
115 Iterator repositoryNames = ContentRepository.getAllRepositoryNames();
116 while (repositoryNames.hasNext()) {
117 saveACLs(role, (String) repositoryNames.next());
118 }
119
120 role.save();
121 return true;
122 } catch (RepositoryException re) {
123 log.error("Failed to update role, reverting all transient modifications made for this node", re);
124 try {
125 role.refresh(false);
126 } catch (RepositoryException e) {
127 log.error("Failed to revert transient modifications", e);
128 }
129 }
130 return false;
131 }
132
133 protected void saveACLs(Content role, String repository) throws RepositoryException {
134
135
136
137
138 try {
139 role.delete("acl_" + repository);
140 }
141 catch (PathNotFoundException re) {
142
143 }
144
145 Content acl = role.createContent("acl_" + repository, ItemType.CONTENTNODE);
146 String aclValueStr = form.getParameter("acl" + repository + "List");
147 if (StringUtils.isNotEmpty(aclValueStr)) {
148 String[] aclEntries = aclValueStr.split(";");
149 for (int i = 0; i < aclEntries.length; i++) {
150 String path = StringUtils.EMPTY;
151 long accessRight = 0;
152 int accessType = 0;
153
154 String[] aclValuePairs = aclEntries[i].split(",");
155 for (int j = 0; j < aclValuePairs.length; j++) {
156 String[] aclValuePair = aclValuePairs[j].split(":");
157 String aclName = aclValuePair[0].trim();
158 String aclValue = StringUtils.EMPTY;
159 if (aclValuePair.length > 1) {
160 aclValue = aclValuePair[1].trim();
161 }
162
163 if (aclName.equals("path")) {
164 path = aclValue;
165 }
166 else if (aclName.equals("accessType")) {
167 accessType = Integer.valueOf(aclValue).intValue();
168 }
169 else if (aclName.equals("accessRight")) {
170 try {
171 accessRight = Long.parseLong(aclValue);
172 }
173 catch (NumberFormatException e) {
174 accessRight = 0;
175 }
176 }
177 }
178
179 if (StringUtils.isNotEmpty(path)) {
180 if (repository.equalsIgnoreCase("uri")) {
181
182 accessType = AclTypeConfiguration.TYPE_THIS;
183 } else if (path.equals("/")) {
184 accessType = AclTypeConfiguration.TYPE_SUBS;
185 path = StringUtils.EMPTY;
186 }
187
188 if ((accessType & AclTypeConfiguration.TYPE_THIS) != 0) {
189 try {
190 String newLabel = Path.getUniqueLabel(hm, acl.getHandle(), "0");
191 Content r = acl.createContent(newLabel, ItemType.CONTENTNODE);
192 r.createNodeData("path").setValue(path);
193 r.createNodeData("permissions").setValue(accessRight);
194 }
195 catch (Exception e) {
196 log.error(e.getMessage(), e);
197 }
198 }
199
200 if ((accessType & AclTypeConfiguration.TYPE_SUBS) != 0) {
201 try {
202 String newLabel = Path.getUniqueLabel(hm, acl.getHandle(), "0");
203 Content r = acl.createContent(newLabel, ItemType.CONTENTNODE);
204 r.createNodeData("path").setValue(path + "/*");
205 r.createNodeData("permissions").setValue(accessRight);
206 }
207 catch (Exception e) {
208 log.error(e.getMessage(), e);
209 }
210 }
211 }
212 }
213 }
214 }
215
216 }