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.core.Content;
37 import info.magnolia.cms.core.HierarchyManager;
38 import info.magnolia.cms.core.NodeData;
39 import info.magnolia.cms.gui.dialog.Dialog;
40 import info.magnolia.cms.gui.dialog.DialogControlImpl;
41 import info.magnolia.cms.security.AccessDeniedException;
42 import info.magnolia.context.MgnlContext;
43 import info.magnolia.module.admininterface.SaveHandler;
44 import info.magnolia.repository.RepositoryConstants;
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 @Override
88 public String getRepository() {
89 String repository = super.getRepository();
90 if (repository == null) {
91 repository = RepositoryConstants.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 @Override
110 protected void configureSaveHandler(SaveHandler save) {
111 super.configureSaveHandler(save);
112 save.setPath(path);
113 }
114
115
116
117
118
119
120
121 @Override
122 protected Dialog createDialog(Content configNode, Content storageNode) throws RepositoryException {
123 Dialog dialog = super.createDialog(configNode, storageNode);
124
125 if (this.getCommand().equalsIgnoreCase(COMMAND_SAVE)) {
126 return dialog;
127 }
128
129
130 DialogControlImpl control = dialog.getSub("groups");
131
132 HierarchyManager groupsHM = MgnlContext.getSystemContext().getHierarchyManager(RepositoryConstants.USER_GROUPS);
133
134 replaceUUIDsWithNames(control, groupsHM);
135
136 control = dialog.getSub("roles");
137
138 HierarchyManager rolesHM = MgnlContext.getSystemContext().getHierarchyManager(RepositoryConstants.USER_ROLES);
139
140 replaceUUIDsWithNames(control, rolesHM);
141
142 return dialog;
143 }
144
145 private void replaceUUIDsWithNames(DialogControlImpl control, HierarchyManager hm) throws RepositoryException {
146 List values = new ArrayList<String>();
147 Iterator it = control.getValues().iterator();
148 while(it.hasNext()){
149 String uuid = (String) it.next();
150 if (StringUtils.isEmpty(uuid)) {
151 continue;
152 }
153 try {
154 values.add(hm.getContentByUUID(uuid).getHandle());
155 }
156 catch (ItemNotFoundException e) {
157
158 }
159 }
160 control.getValues().clear();
161 control.getValues().addAll(values);
162 }
163
164
165
166
167
168 protected void writeACL(Content node) throws RepositoryException {
169
170
171
172 }
173
174 @Override
175 protected boolean onPostSave(SaveHandler saveControl) {
176
177 Content node = this.getStorageNode();
178
179 HierarchyManager groupsHM = MgnlContext.getHierarchyManager(
180 RepositoryConstants.USER_GROUPS);
181 HierarchyManager rolesHM = MgnlContext.getHierarchyManager(
182 RepositoryConstants.USER_ROLES);
183
184 try {
185 this.writeRolesOrGroups(groupsHM, node, "groups");
186 this.writeRolesOrGroups(rolesHM, node, "roles");
187 this.writeACL(node);
188 node.save();
189 return true;
190 } catch (RepositoryException re) {
191 log.error("Failed to update user, reverting all transient modifications made for this node", re);
192 try {
193 node.refresh(false);
194 } catch (RepositoryException e) {
195 log.error("Failed to revert transient modifications", re);
196 }
197 }
198 return false;
199 }
200
201 private void writeRolesOrGroups(HierarchyManager hm, Content parentNode, String nodeName)
202 throws RepositoryException {
203 try {
204 Content groupOrRoleNode = parentNode.getContent(nodeName);
205
206 Iterator existingNodes = groupOrRoleNode.getNodeDataCollection().iterator();
207 while (existingNodes.hasNext()) {
208 ((NodeData) existingNodes.next()).delete();
209 }
210 List values = getDialog().getSub(nodeName).getValues();
211 String path = null;
212 for (int index = 0; index < values.size(); index++) {
213 try {
214 path = (String) values.get(index);
215 if (StringUtils.isNotEmpty(path)) {
216 groupOrRoleNode.createNodeData(Integer.toString(index)).setValue(hm.getContent(path).getUUID());
217 }
218 } catch(AccessDeniedException e) {
219 String user = MgnlContext.getUser().getName();
220 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())});
221 }
222 }
223 } catch (PathNotFoundException e) {
224
225 }
226 }
227 }