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