]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/commands/DeleteUser.java
Bug 28
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui.admin / src / main / java / org / argeo / security / ui / admin / commands / DeleteUser.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.ui.admin.commands;
17
18 import java.util.Iterator;
19 import java.util.Map;
20 import java.util.TreeMap;
21
22 import javax.jcr.Node;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.Session;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.ArgeoException;
29 import org.argeo.jcr.ArgeoNames;
30 import org.argeo.jcr.JcrUtils;
31 import org.argeo.security.UserAdminService;
32 import org.argeo.security.ui.admin.views.UsersView;
33 import org.eclipse.core.commands.AbstractHandler;
34 import org.eclipse.core.commands.ExecutionEvent;
35 import org.eclipse.core.commands.ExecutionException;
36 import org.eclipse.jface.dialogs.MessageDialog;
37 import org.eclipse.jface.viewers.ISelection;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.ui.handlers.HandlerUtil;
40
41 /** Deletes the select user nodes */
42 public class DeleteUser extends AbstractHandler {
43 private final static Log log = LogFactory.getLog(DeleteUser.class);
44
45 private UserAdminService userAdminService;
46
47 @SuppressWarnings("unchecked")
48 public Object execute(ExecutionEvent event) throws ExecutionException {
49 ISelection selection = HandlerUtil.getCurrentSelection(event);
50 if (selection.isEmpty())
51 return null;
52
53 Map<String, Node> toDelete = new TreeMap<String, Node>();
54 Iterator<Node> it = ((IStructuredSelection) selection).iterator();
55 nodes: while (it.hasNext()) {
56 Node profileNode = it.next();
57 try {
58 String userName = profileNode.getProperty(
59 ArgeoNames.ARGEO_USER_ID).getString();
60 if (userName.equals(profileNode.getSession().getUserID())) {
61 log.warn("Cannot delete its own user: " + userName);
62 continue nodes;
63 }
64 toDelete.put(userName, profileNode);
65 } catch (RepositoryException e) {
66 log.warn("Cannot interpred user " + profileNode);
67 }
68 }
69
70 if (!MessageDialog
71 .openQuestion(
72 HandlerUtil.getActiveShell(event),
73 "Delete User",
74 "Are you sure that you want to delete users "
75 + toDelete.keySet()
76 + "?\n"
77 + "This may lead to inconsistencies in the application."))
78 return null;
79
80 for (String username : toDelete.keySet()) {
81 Session session = null;
82 try {
83 Node profileNode = toDelete.get(username);
84 userAdminService.deleteUser(username);
85 profileNode.getParent().remove();
86 session = profileNode.getSession();
87 session.save();
88 } catch (RepositoryException e) {
89 JcrUtils.discardQuietly(session);
90 throw new ArgeoException("Cannot list users", e);
91 }
92 }
93
94 userAdminService.synchronize();
95 UsersView view = (UsersView) HandlerUtil
96 .getActiveWorkbenchWindow(event).getActivePage()
97 .findView(UsersView.ID);
98 view.refresh();
99 return null;
100 }
101
102 public void setUserAdminService(UserAdminService userAdminService) {
103 this.userAdminService = userAdminService;
104 }
105 }