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.security.app.action;
35
36 import info.magnolia.cms.security.Group;
37 import info.magnolia.cms.security.GroupManager;
38 import info.magnolia.cms.security.SecuritySupport;
39 import info.magnolia.cms.security.User;
40 import info.magnolia.cms.security.UserManager;
41 import info.magnolia.commands.CommandsManager;
42 import info.magnolia.event.EventBus;
43 import info.magnolia.i18nsystem.SimpleTranslator;
44 import info.magnolia.jcr.util.NodeTypes;
45 import info.magnolia.jcr.util.NodeUtil;
46 import info.magnolia.jcr.util.NodeVisitor;
47 import info.magnolia.ui.api.action.ActionExecutionException;
48 import info.magnolia.ui.api.context.UiContext;
49 import info.magnolia.ui.api.event.AdmincentralEventBus;
50 import info.magnolia.ui.api.overlay.ConfirmationCallback;
51 import info.magnolia.ui.framework.action.DeleteAction;
52 import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
53 import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
54
55 import java.util.ArrayList;
56 import java.util.Collection;
57 import java.util.HashMap;
58 import java.util.List;
59 import java.util.Map;
60
61 import javax.inject.Named;
62 import javax.jcr.Node;
63 import javax.jcr.RepositoryException;
64
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67
68 import com.google.inject.Inject;
69
70
71
72
73
74
75 public class DeleteFolderAction extends DeleteAction<DeleteFolderActionDefinition> {
76
77 private static final Logger log = LoggerFactory.getLogger(DeleteFolderAction.class);
78
79 private final SecuritySupport securitySupport;
80
81 @Inject
82 public DeleteFolderAction(DeleteFolderActionDefinition definition, JcrItemAdapter item, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n, SecuritySupport securitySupport) {
83 super(definition, item, commandsManager, eventBus, uiContext, i18n);
84 this.securitySupport = securitySupport;
85 }
86
87 @Inject
88 public DeleteFolderAction(DeleteFolderActionDefinition definition, List<JcrItemAdapter> items, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n, SecuritySupport securitySupport) {
89 super(definition, items, commandsManager, eventBus, uiContext, i18n);
90 this.securitySupport = securitySupport;
91 }
92
93 @Override
94 public void execute() throws ActionExecutionException {
95 try {
96 executeOnConfirmation();
97 } catch (RepositoryException e) {
98 throw new ActionExecutionException(getVerificationErrorMessage() + e.getMessage());
99 }
100 }
101
102 private String getConfirmationDialogStatement() throws RepositoryException {
103 StringBuilder confirmMessage = new StringBuilder("<ul>");
104 final Map<String, List<String>> assignedTo = new HashMap<>();
105 for (JcrItemAdapter item : getSortedItems(getItemComparator())) {
106 final Map<String, List<String>> assignedToItem = new HashMap<>();
107 try {
108 Map<String, List<String>> dependenciesMap = getAssignedUsersAndGroupsMap(item);
109 if (!dependenciesMap.isEmpty()) {
110 confirmMessage.append("<li>");
111 confirmMessage.append(item.getJcrItem().getName());
112 confirmMessage.append("</li>");
113 assignedToItem.putAll(dependenciesMap);
114 }
115 } catch (RepositoryException e) {
116 throw new RepositoryException("Cannot get the users/groups the group or role is assigned to.", e);
117 }
118 confirmMessage.append(formatUserAndGroupList(assignedToItem));
119 assignedTo.putAll(assignedToItem);
120 }
121 confirmMessage.append("</ul>");
122 return !assignedTo.isEmpty() ? confirmMessage.toString() : "";
123 }
124
125 private void executeOnConfirmation() throws RepositoryException {
126 final String message = getConfirmationDialogStatement();
127 getUiContext().openConfirmation(MessageStyleTypeEnum.WARNING,
128 getConfirmationDialogTitle(),
129 (!message.isEmpty() ? "<br />" + getI18n().translate("security-app.delete.confirmationDialog.body.label", message) + "<br />" : "") + getConfirmationDialogBody(),
130 getConfirmationDialogProceedLabel(),
131 getConfirmationDialogCancelLabel(),
132 true,
133 new ConfirmationCallback() {
134 @Override
135 public void onCancel() {
136
137 }
138
139 @Override
140 public void onSuccess() {
141 try {
142 DeleteFolderAction.super.execute();
143 } catch (Exception e) {
144 onError(e);
145 }
146 }
147 });
148 }
149
150 @Override
151 protected void onPreExecute() throws Exception {
152 super.onPreExecute();
153
154 final Map<String, List<String>> assignedTo = getAssignedUsersAndGroupsMap();
155 if (!assignedTo.isEmpty()) {
156 if (getCurrentItem().isNode()) {
157 Node folder = (Node) getCurrentItem().getJcrItem();
158
159 NodeUtil.visit(folder, new NodeVisitor() {
160 @Override
161 public void visit(Node node) throws RepositoryException {
162 if (NodeUtil.isNodeType(node, NodeTypes.Role.NAME) || NodeUtil.isNodeType(node, NodeTypes.Group.NAME)) {
163 try {
164 removeDependencies(node);
165 } catch (Exception e) {
166 onError(e);
167 }
168 }
169 }
170 });
171 }
172 }
173 }
174
175
176
177
178 private List<String> getAssignedUsersAndGroups(Node node) throws RepositoryException {
179 List<String> assignedTo = new ArrayList<String>();
180
181 final String groupOrRoleName = node.getName();
182
183 final String translatedUserString = getI18n().translate("security.delete.userIdentifier");
184
185 final String translatedGroupString = getI18n().translate("security.delete.groupIdentifier");
186
187 if (NodeUtil.isNodeType(node, NodeTypes.Group.NAME)) {
188
189 for (String user : securitySupport.getUserManager().getUsersWithGroup(groupOrRoleName)) {
190 assignedTo.add(translatedUserString + ":" + user);
191 }
192 for (String group : securitySupport.getGroupManager().getGroupsWithGroup(groupOrRoleName)) {
193 assignedTo.add(translatedGroupString + ":" + group);
194 }
195 } else if (NodeUtil.isNodeType(node, NodeTypes.Role.NAME)) {
196
197 for (String user : securitySupport.getUserManager().getUsersWithRole(groupOrRoleName)) {
198 assignedTo.add(translatedUserString + ":" + user);
199 }
200 for (String group : securitySupport.getGroupManager().getGroupsWithRole(groupOrRoleName)) {
201 assignedTo.add(translatedGroupString + ":" + group);
202 }
203 }
204
205 return assignedTo;
206 }
207
208 protected String getVerificationErrorMessage() {
209 return getI18n().translate("security.delete.folder.cannotVerifyError");
210 }
211
212
213
214
215 @Deprecated
216 protected Collection<String> getGroupsOrRoles(User user) {
217 List<String> groupsAndRoles = new ArrayList<String>();
218 groupsAndRoles.addAll(user.getGroups());
219 groupsAndRoles.addAll(user.getRoles());
220 return groupsAndRoles;
221 }
222
223
224
225
226 @Deprecated
227 protected Collection<String> getGroupsOrRoles(Group group) {
228 List<String> groupsAndRoles = new ArrayList<String>();
229 groupsAndRoles.addAll(group.getGroups());
230 groupsAndRoles.addAll(group.getRoles());
231 return groupsAndRoles;
232 }
233
234
235
236
237 @Deprecated
238 protected String getUserAndGroupListForErrorMessage(List<String> usersAndGroups) {
239 Map<String, List<String>> usersAndGroupsMap = new HashMap<String, List<String>>();
240 usersAndGroupsMap.put("dependencies", usersAndGroups);
241 return formatUserAndGroupList(usersAndGroupsMap);
242 }
243
244 protected String formatUserAndGroupList(Map<String, List<String>> usersAndGroups) {
245 StringBuilder message = new StringBuilder("<ul>");
246 for (String key : usersAndGroups.keySet()) {
247 int i = 0;
248 message.append("<li>").append(key).append("</li>");
249 message.append("<ul>");
250 for (String name : usersAndGroups.get(key)) {
251 message.append("<li>").append(name).append("</li>");
252 if (i > 4) {
253 message.append("<li>...</li>");
254 break;
255 }
256 i++;
257 }
258 message.append("</ul>");
259 }
260 message.append("</ul>");
261 return message.toString();
262 }
263
264 protected String getConfirmationDialogTitle() {
265 return getI18n().translate("security.folders.actions.confirmDeleteFolder.confirmationHeader");
266 }
267
268 protected String getConfirmationDialogBody() {
269 return getI18n().translate("security.folders.actions.confirmDeleteFolder.confirmationMessage");
270 }
271
272 protected String getConfirmationDialogProceedLabel() {
273 return getI18n().translate("security.folders.actions.confirmDeleteFolder.proceedLabel");
274 }
275
276 protected String getConfirmationDialogCancelLabel() {
277 return getI18n().translate("security.folders.actions.confirmDeleteFolder.cancelLabel");
278 }
279
280 protected String getBaseErrorMessage() {
281 return getI18n().translate("security.delete.folder.roleOrGroupInfolderStillInUse");
282 }
283
284 private void removeDependencies(Node node) throws RepositoryException, ActionExecutionException {
285 final String groupOrRoleName = node.getName();
286 final UserManager userManager = securitySupport.getUserManager();
287 final GroupManager groupManager = securitySupport.getGroupManager();
288 if (NodeUtil.isNodeType(node, NodeTypes.Group.NAME)) {
289
290 for (String user : securitySupport.getUserManager().getUsersWithGroup(groupOrRoleName)) {
291 userManager.removeGroup(userManager.getUser(user), groupOrRoleName);
292 }
293 for (String group : securitySupport.getGroupManager().getGroupsWithGroup(groupOrRoleName)) {
294 groupManager.removeGroup(groupManager.getGroup(group), groupOrRoleName);
295 }
296 } else if (NodeUtil.isNodeType(node, NodeTypes.Role.NAME)) {
297
298 for (String user : securitySupport.getUserManager().getUsersWithRole(groupOrRoleName)) {
299 userManager.removeRole(userManager.getUser(user), groupOrRoleName);
300 }
301 for (String group : securitySupport.getGroupManager().getGroupsWithRole(groupOrRoleName)) {
302 groupManager.removeRole(groupManager.getGroup(group), groupOrRoleName);
303 }
304 }
305 }
306
307 private Map<String, List<String>> getAssignedUsersAndGroupsMap() throws RepositoryException {
308 return getAssignedUsersAndGroupsMap(getCurrentItem());
309 }
310 private Map<String, List<String>> getAssignedUsersAndGroupsMap(JcrItemAdapter jcrItemAdapter) throws RepositoryException {
311 final Map<String, List<String>> assignedTo = new HashMap<>();
312 try {
313 if (jcrItemAdapter.isNode()) {
314 Node folder = (Node) jcrItemAdapter.getJcrItem();
315
316 NodeUtil.visit(folder, new NodeVisitor() {
317 @Override
318 public void visit(Node node) throws RepositoryException {
319 if (NodeUtil.isNodeType(node, NodeTypes.Role.NAME) || NodeUtil.isNodeType(node, NodeTypes.Group.NAME)) {
320 List<String> assignedToItem = getAssignedUsersAndGroups(node);
321 if (!assignedToItem.isEmpty()) {
322 assignedTo.put(node.getName(), assignedToItem);
323 }
324 }
325 }
326 });
327 }
328 } catch (RepositoryException e) {
329 throw new RepositoryException("Cannot get the users/groups the group or role is assigned to.", e);
330 }
331 return assignedTo;
332 }
333 }