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
.cms
.util
.useradmin
.UserAdminUtils
;
23 import org
.argeo
.security
.ui
.admin
.SecurityAdminPlugin
;
24 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminWrapper
;
25 import org
.argeo
.security
.ui
.admin
.internal
.parts
.UserEditorInput
;
26 import org
.eclipse
.core
.commands
.AbstractHandler
;
27 import org
.eclipse
.core
.commands
.ExecutionEvent
;
28 import org
.eclipse
.core
.commands
.ExecutionException
;
29 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
30 import org
.eclipse
.jface
.viewers
.ISelection
;
31 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
32 import org
.eclipse
.ui
.IEditorPart
;
33 import org
.eclipse
.ui
.IWorkbenchPage
;
34 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
35 import org
.osgi
.service
.useradmin
.User
;
36 import org
.osgi
.service
.useradmin
.UserAdmin
;
37 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
39 /** Delete the selected users */
40 public class DeleteUsers
extends AbstractHandler
{
41 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
44 /* DEPENDENCY INJECTION */
45 private UserAdminWrapper userAdminWrapper
;
47 @SuppressWarnings("unchecked")
48 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
49 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
50 if (selection
.isEmpty())
53 Iterator
<User
> it
= ((IStructuredSelection
) selection
).iterator();
54 List
<User
> users
= new ArrayList
<User
>();
55 StringBuilder builder
= new StringBuilder();
57 while (it
.hasNext()) {
58 User currUser
= it
.next();
59 String userName
= UserAdminUtils
.getUsername(currUser
);
60 if (UserAdminUtils
.isCurrentUser(currUser
)) {
61 MessageDialog
.openError(HandlerUtil
.getActiveShell(event
),
63 "You cannot delete your own user this way.");
66 builder
.append(userName
).append("; ");
70 if (!MessageDialog
.openQuestion(
71 HandlerUtil
.getActiveShell(event
),
73 "Are you sure that you want to delete these users?\n"
74 + builder
.substring(0, builder
.length() - 2)))
77 userAdminWrapper
.beginTransactionIfNeeded();
78 UserAdmin userAdmin
= userAdminWrapper
.getUserAdmin();
79 IWorkbenchPage iwp
= HandlerUtil
.getActiveWorkbenchWindow(event
)
82 for (User user
: users
) {
83 String userName
= user
.getName();
84 // TODO find a way to close the editor cleanly if opened. Cannot be
85 // done through the UserAdminListeners, it causes a
86 // java.util.ConcurrentModificationException because disposing the
87 // editor unregisters and disposes the listener
88 IEditorPart part
= iwp
.findEditor(new UserEditorInput(userName
));
90 iwp
.closeEditor(part
, false);
91 userAdmin
.removeRole(userName
);
93 userAdminWrapper
.commitOrNotifyTransactionStateChange();
95 for (User user
: users
) {
96 userAdminWrapper
.notifyListeners(new UserAdminEvent(null,
97 UserAdminEvent
.ROLE_REMOVED
, user
));
102 /* DEPENDENCY INJECTION */
103 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
104 this.userAdminWrapper
= userAdminWrapper
;