]> git.argeo.org Git - lgpl/argeo-commons.git/blob - DeleteUsers.java
d2c34b55dcd6c8312bf6961af0a7c53c9254efb8
[lgpl/argeo-commons.git] / 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.cms.ui.workbench.internal.useradmin.commands;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
23 import org.argeo.cms.ui.workbench.internal.useradmin.UserAdminWrapper;
24 import org.argeo.cms.ui.workbench.internal.useradmin.parts.UserEditorInput;
25 import org.argeo.cms.util.useradmin.UserAdminUtils;
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;
38
39 /** Delete the selected users */
40 public class DeleteUsers extends AbstractHandler {
41 public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".deleteUsers";
42
43 /* DEPENDENCY INJECTION */
44 private UserAdminWrapper userAdminWrapper;
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 Iterator<User> it = ((IStructuredSelection) selection).iterator();
53 List<User> users = new ArrayList<User>();
54 StringBuilder builder = new StringBuilder();
55
56 while (it.hasNext()) {
57 User currUser = it.next();
58 String userName = UserAdminUtils.getUsername(currUser);
59 if (UserAdminUtils.isCurrentUser(currUser)) {
60 MessageDialog.openError(HandlerUtil.getActiveShell(event),
61 "Deletion forbidden",
62 "You cannot delete your own user this way.");
63 return null;
64 }
65 builder.append(userName).append("; ");
66 users.add(currUser);
67 }
68
69 if (!MessageDialog.openQuestion(
70 HandlerUtil.getActiveShell(event),
71 "Delete Users",
72 "Are you sure that you want to delete these users?\n"
73 + builder.substring(0, builder.length() - 2)))
74 return null;
75
76 userAdminWrapper.beginTransactionIfNeeded();
77 UserAdmin userAdmin = userAdminWrapper.getUserAdmin();
78 IWorkbenchPage iwp = HandlerUtil.getActiveWorkbenchWindow(event)
79 .getActivePage();
80
81 for (User user : users) {
82 String userName = user.getName();
83 // TODO find a way to close the editor cleanly if opened. Cannot be
84 // done through the UserAdminListeners, it causes a
85 // java.util.ConcurrentModificationException because disposing the
86 // editor unregisters and disposes the listener
87 IEditorPart part = iwp.findEditor(new UserEditorInput(userName));
88 if (part != null)
89 iwp.closeEditor(part, false);
90 userAdmin.removeRole(userName);
91 }
92 userAdminWrapper.commitOrNotifyTransactionStateChange();
93
94 for (User user : users) {
95 userAdminWrapper.notifyListeners(new UserAdminEvent(null,
96 UserAdminEvent.ROLE_REMOVED, user));
97 }
98 return null;
99 }
100
101 /* DEPENDENCY INJECTION */
102 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
103 this.userAdminWrapper = userAdminWrapper;
104 }
105 }