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