]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/commands/DeleteUser.java
- Start factorizing LDIF and LDAP
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / 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.eclipse.core.commands.AbstractHandler;
33 import org.eclipse.core.commands.ExecutionEvent;
34 import org.eclipse.core.commands.ExecutionException;
35 import org.eclipse.jface.dialogs.MessageDialog;
36 import org.eclipse.jface.viewers.ISelection;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.ui.handlers.HandlerUtil;
39
40 /** Deletes the selected user nodes */
41 public class DeleteUser extends AbstractHandler {
42 private final static Log log = LogFactory.getLog(DeleteUser.class);
43
44 private UserAdminService userAdminService;
45
46 @SuppressWarnings("unchecked")
47 public Object execute(ExecutionEvent event) throws ExecutionException {
48 ISelection selection = HandlerUtil.getCurrentSelection(event);
49 if (selection.isEmpty())
50 return null;
51
52 Map<String, Node> toDelete = new TreeMap<String, Node>();
53 Iterator<Node> it = ((IStructuredSelection) selection).iterator();
54 nodes: while (it.hasNext()) {
55 Node profileNode = it.next();
56 try {
57 String userName = profileNode.getProperty(
58 ArgeoNames.ARGEO_USER_ID).getString();
59 if (userName.equals(profileNode.getSession().getUserID())) {
60 log.warn("Cannot delete its own user: " + userName);
61 continue nodes;
62 }
63 toDelete.put(userName, profileNode);
64 } catch (RepositoryException e) {
65 log.warn("Cannot interpred user " + profileNode);
66 }
67 }
68
69 if (!MessageDialog
70 .openQuestion(
71 HandlerUtil.getActiveShell(event),
72 "Delete User",
73 "Are you sure that you want to delete users "
74 + toDelete.keySet()
75 + "?\n"
76 + "This may lead to inconsistencies in the application."))
77 return null;
78
79 for (String username : toDelete.keySet()) {
80 Session session = null;
81 try {
82 Node profileNode = toDelete.get(username);
83 userAdminService.deleteUser(username);
84 profileNode.getParent().remove();
85 session = profileNode.getSession();
86 session.save();
87 } catch (RepositoryException e) {
88 JcrUtils.discardQuietly(session);
89 throw new ArgeoException("Cannot list users", e);
90 }
91 }
92
93 userAdminService.synchronize();
94 // UsersView view = (UsersView) HandlerUtil
95 // .getActiveWorkbenchWindow(event).getActivePage()
96 // .findView(UsersView.ID);
97 // view.refresh();
98 return null;
99 }
100
101 public void setUserAdminService(UserAdminService userAdminService) {
102 this.userAdminService = userAdminService;
103 }
104 }