]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/commands/DeleteUsers.java
Enable user and group deletion. Remove dynamic Editor icon that triggered NPE on...
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / commands / DeleteUsers.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.transaction.UserTransaction;
23
24 import org.argeo.security.ui.admin.SecurityAdminPlugin;
25 import org.argeo.security.ui.admin.internal.UiAdminUtils;
26 import org.argeo.security.ui.admin.views.UsersView;
27 import org.eclipse.core.commands.AbstractHandler;
28 import org.eclipse.core.commands.ExecutionEvent;
29 import org.eclipse.core.commands.ExecutionException;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchPart;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.handlers.HandlerUtil;
37 import org.osgi.service.useradmin.User;
38 import org.osgi.service.useradmin.UserAdmin;
39
40 /** Deletes the selected users */
41 public class DeleteUsers extends AbstractHandler {
42 public final static String ID = SecurityAdminPlugin.PLUGIN_ID
43 + ".deleteUsers";
44
45 /* DEPENDENCY INJECTION */
46 private UserAdmin userAdmin;
47 private UserTransaction userTransaction;
48
49 @SuppressWarnings("unchecked")
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51 ISelection selection = HandlerUtil.getCurrentSelection(event);
52 if (selection.isEmpty())
53 return null;
54
55 Map<String, String> toDelete = new TreeMap<String, String>();
56 // Map<String, User> toDelete = new TreeMap<String, User>();
57 Iterator<User> it = ((IStructuredSelection) selection).iterator();
58 users: while (it.hasNext()) {
59 User currUser = it.next();
60 String userName = UiAdminUtils.getUsername(currUser);
61 // TODO check not deleting own user
62 // if (userName.equals(profileNode.getSession().getUserID())) {
63 // log.warn("Cannot delete its own user: " + userName);
64 // continue nodes;
65 // }
66 toDelete.put(userName, currUser.getName());
67 }
68
69 if (!MessageDialog
70 .openQuestion(
71 HandlerUtil.getActiveShell(event),
72 "Delete Users",
73 "Are you sure that you want to delete users "
74 + toDelete.keySet()
75 + "?\n"
76 + "This might lead to inconsistencies in the application."))
77 return null;
78
79 UiAdminUtils.beginTransactionIfNeeded(userTransaction);
80 for (String name : toDelete.values()) {
81 userAdmin.removeRole(name);
82 }
83
84 // TODO rather notify the update listener
85 forceRefresh(event);
86 return null;
87 }
88
89 private void forceRefresh(ExecutionEvent event) {
90 IWorkbenchWindow iww = HandlerUtil.getActiveWorkbenchWindow(event);
91 if (iww == null)
92 return;
93 IWorkbenchPage activePage = iww.getActivePage();
94 IWorkbenchPart part = activePage.getActivePart();
95 if (part instanceof UsersView)
96 ((UsersView) part).refresh();
97 }
98
99 /* DEPENDENCY INJECTION */
100 public void setUserAdmin(UserAdmin userAdmin) {
101 this.userAdmin = userAdmin;
102 }
103
104 public void setUserTransaction(UserTransaction userTransaction) {
105 this.userTransaction = userTransaction;
106 }
107 }