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.NodeData;
40 import info.magnolia.cms.core.HierarchyManager;
41 import info.magnolia.cms.gui.dialog.Dialog;
42 import info.magnolia.cms.gui.dialog.DialogControlImpl;
43 import info.magnolia.cms.security.AccessDeniedException;
44 import info.magnolia.cms.security.Permission;
45 import info.magnolia.context.MgnlContext;
46 import info.magnolia.module.admininterface.SaveHandler;
47
48 import java.util.Iterator;
49 import java.util.List;
50
51 import javax.jcr.ItemNotFoundException;
52 import javax.jcr.PathNotFoundException;
53 import javax.jcr.RepositoryException;
54 import javax.servlet.http.HttpServletRequest;
55 import javax.servlet.http.HttpServletResponse;
56
57 import org.apache.commons.lang.StringUtils;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61
62
63
64
65
66 public class UserEditDialog extends ConfiguredDialog {
67
68
69
70
71 private static final long serialVersionUID = 222L;
72
73
74
75
76 protected static Logger log = LoggerFactory.getLogger(UserEditDialog.class);
77
78 protected static final String NODE_ACLUSERS = "acl_users";
79
80 protected static final String NODE_ACLROLES = "acl_userroles";
81
82 protected static final String NODE_ACLCONFIG = "acl_config";
83
84
85
86
87
88 public String getRepository() {
89 String repository = super.getRepository();
90 if (repository == null) {
91 repository = ContentRepository.USERS;
92 }
93 return repository;
94 }
95
96
97
98
99
100
101
102 public UserEditDialog(String name, HttpServletRequest request, HttpServletResponse response, Content configNode) {
103 super(name, request, response, configNode);
104 }
105
106
107
108
109 protected void configureSaveHandler(SaveHandler save) {
110 super.configureSaveHandler(save);
111 save.setPath(path);
112 }
113
114
115
116
117
118
119
120 protected Dialog createDialog(Content configNode, Content storageNode) throws RepositoryException {
121 Dialog dialog = super.createDialog(configNode, storageNode);
122
123 if (this.getCommand().equalsIgnoreCase(COMMAND_SAVE)) {
124 return dialog;
125 }
126
127
128 DialogControlImpl control = dialog.getSub("groups");
129
130 HierarchyManager groupsHM = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.USER_GROUPS);
131 List values = control.getValues();
132 for (int index = 0; index < values.size(); index++) {
133
134 String uuid = (String) values.get(index);
135 if (StringUtils.isEmpty(uuid)) {
136 continue;
137 }
138 try {
139 values.set(index, groupsHM.getContentByUUID(uuid).getHandle());
140 }
141 catch (ItemNotFoundException e) {
142
143 values.remove(index);
144 }
145 }
146
147 control = dialog.getSub("roles");
148
149 HierarchyManager rolesHM = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.USER_ROLES);
150 values = control.getValues();
151 for (int index = 0; index < values.size(); index++) {
152
153 String uuid = (String) values.get(index);
154 if (StringUtils.isEmpty(uuid)) {
155 continue;
156 }
157 try {
158 values.set(index, rolesHM.getContentByUUID(uuid).getHandle());
159 }
160 catch (ItemNotFoundException e) {
161
162 values.remove(index);
163 }
164 }
165 return dialog;
166 }
167
168
169
170
171
172 protected void writeACL(Content node) throws RepositoryException {
173
174
175
176 }
177
178 protected boolean onPostSave(SaveHandler saveControl) {
179
180 Content node = this.getStorageNode();
181
182 HierarchyManager groupsHM = MgnlContext.getHierarchyManager(
183 ContentRepository.USER_GROUPS);
184 HierarchyManager rolesHM = MgnlContext.getHierarchyManager(
185 ContentRepository.USER_ROLES);
186
187 try {
188 this.writeRolesOrGroups(groupsHM, node, "groups");
189 this.writeRolesOrGroups(rolesHM, node, "roles");
190 this.writeACL(node);
191 node.save();
192 return true;
193 } catch (RepositoryException re) {
194 log.error("Failed to update user, reverting all transient modifications made for this node", re);
195 try {
196 node.refresh(false);
197 } catch (RepositoryException e) {
198 log.error("Failed to revert transient modifications", re);
199 }
200 }
201 return false;
202 }
203
204 private void writeRolesOrGroups(HierarchyManager hm, Content parentNode, String nodeName)
205 throws RepositoryException {
206 try {
207 Content groupOrRoleNode = parentNode.getContent(nodeName);
208
209 Iterator existingNodes = groupOrRoleNode.getNodeDataCollection().iterator();
210 while (existingNodes.hasNext()) {
211 ((NodeData) existingNodes.next()).delete();
212 }
213 List values = getDialog().getSub(nodeName).getValues();
214 String path = null;
215 for (int index = 0; index < values.size(); index++) {
216 try {
217 path = (String) values.get(index);
218 if (StringUtils.isNotEmpty(path)) {
219 groupOrRoleNode.createNodeData(Integer.toString(index)).setValue(hm.getContent(path).getUUID());
220 }
221 } catch(AccessDeniedException e) {
222 String user = MgnlContext.getUser().getName();
223 log.warn("User {} tried to assign {} {} to {} without having privileges to do so.", new Object[] {user, nodeName.substring(0, nodeName.length() - 1), path, (parentNode.getName() == user ? "self" : parentNode.getName())});
224 }
225 }
226 } catch (PathNotFoundException e) {
227
228 }
229 }
230 }