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
.internal
.commands
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
22 import org
.argeo
.security
.ui
.admin
.SecurityAdminPlugin
;
23 import org
.argeo
.security
.ui
.admin
.internal
.UiAdminUtils
;
24 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminWrapper
;
25 import org
.eclipse
.core
.commands
.AbstractHandler
;
26 import org
.eclipse
.core
.commands
.ExecutionEvent
;
27 import org
.eclipse
.core
.commands
.ExecutionException
;
28 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
29 import org
.eclipse
.jface
.viewers
.ISelection
;
30 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
31 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
32 import org
.osgi
.service
.useradmin
.User
;
33 import org
.osgi
.service
.useradmin
.UserAdmin
;
34 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
36 /** Deletes the selected users */
37 public class DeleteUsers
extends AbstractHandler
{
38 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
41 /* DEPENDENCY INJECTION */
42 private UserAdminWrapper userAdminWrapper
;
44 @SuppressWarnings("unchecked")
45 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
46 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
47 if (selection
.isEmpty())
50 Iterator
<User
> it
= ((IStructuredSelection
) selection
).iterator();
51 List
<User
> users
= new ArrayList
<User
>();
52 StringBuilder builder
= new StringBuilder();
54 while (it
.hasNext()) {
55 User currUser
= it
.next();
56 String userName
= UiAdminUtils
.getUsername(currUser
);
57 if (UiAdminUtils
.isCurrentUser(currUser
)) {
58 MessageDialog
.openError(HandlerUtil
.getActiveShell(event
),
60 "You cannot delete your own user this way.");
63 builder
.append(userName
).append("; ");
67 if (!MessageDialog
.openQuestion(
68 HandlerUtil
.getActiveShell(event
),
70 "Are you sure that you want to delete these users?\n"
71 + builder
.substring(0, builder
.length() - 2)))
74 userAdminWrapper
.beginTransactionIfNeeded();
75 UserAdmin userAdmin
= userAdminWrapper
.getUserAdmin();
76 for (User user
: users
) {
77 userAdmin
.removeRole(user
.getName());
78 userAdminWrapper
.notifyListeners(new UserAdminEvent(null,
79 UserAdminEvent
.ROLE_REMOVED
, user
));
84 /* DEPENDENCY INJECTION */
85 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
86 this.userAdminWrapper
= userAdminWrapper
;