2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.security
.ui
.admin
.commands
;
18 import java
.util
.Iterator
;
20 import java
.util
.TreeMap
;
22 import javax
.transaction
.UserTransaction
;
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
;
40 /** Deletes the selected users */
41 public class DeleteUsers
extends AbstractHandler
{
42 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
45 /* DEPENDENCY INJECTION */
46 private UserAdmin userAdmin
;
47 private UserTransaction userTransaction
;
49 @SuppressWarnings("unchecked")
50 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
51 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
52 if (selection
.isEmpty())
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);
66 toDelete
.put(userName
, currUser
.getName());
71 HandlerUtil
.getActiveShell(event
),
73 "Are you sure that you want to delete users "
76 + "This might lead to inconsistencies in the application."))
79 UiAdminUtils
.beginTransactionIfNeeded(userTransaction
);
80 for (String name
: toDelete
.values()) {
81 userAdmin
.removeRole(name
);
84 // TODO rather notify the update listener
89 private void forceRefresh(ExecutionEvent event
) {
90 IWorkbenchWindow iww
= HandlerUtil
.getActiveWorkbenchWindow(event
);
93 IWorkbenchPage activePage
= iww
.getActivePage();
94 IWorkbenchPart part
= activePage
.getActivePart();
95 if (part
instanceof UsersView
)
96 ((UsersView
) part
).refresh();
99 /* DEPENDENCY INJECTION */
100 public void setUserAdmin(UserAdmin userAdmin
) {
101 this.userAdmin
= userAdmin
;
104 public void setUserTransaction(UserTransaction userTransaction
) {
105 this.userTransaction
= userTransaction
;