]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/commands/DeleteGroups.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 / DeleteGroups.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.internal.UserAdminConstants;
27 import org.argeo.security.ui.admin.views.GroupsView;
28 import org.eclipse.core.commands.AbstractHandler;
29 import org.eclipse.core.commands.ExecutionEvent;
30 import org.eclipse.core.commands.ExecutionException;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.ui.IWorkbenchPage;
35 import org.eclipse.ui.IWorkbenchPart;
36 import org.eclipse.ui.IWorkbenchWindow;
37 import org.eclipse.ui.handlers.HandlerUtil;
38 import org.osgi.service.useradmin.Group;
39 import org.osgi.service.useradmin.UserAdmin;
40
41 /** Deletes the selected groups */
42 public class DeleteGroups extends AbstractHandler {
43 public final static String ID = SecurityAdminPlugin.PLUGIN_ID
44 + ".deleteGroups";
45
46 /* DEPENDENCY INJECTION */
47 private UserAdmin userAdmin;
48 private UserTransaction userTransaction;
49
50 @SuppressWarnings("unchecked")
51 public Object execute(ExecutionEvent event) throws ExecutionException {
52 ISelection selection = HandlerUtil.getCurrentSelection(event);
53 if (selection.isEmpty())
54 return null;
55
56 Map<String, String> toDelete = new TreeMap<String, String>();
57 // List<String> names = new ArrayList<String>();
58 Iterator<Group> it = ((IStructuredSelection) selection).iterator();
59 while (it.hasNext()) {
60 Group currGroup = it.next();
61 String groupName = UiAdminUtils.getProperty(currGroup,
62 UserAdminConstants.KEY_CN);
63 // TODO add checks
64 toDelete.put(groupName, currGroup.getName());
65 }
66
67 if (!MessageDialog
68 .openQuestion(
69 HandlerUtil.getActiveShell(event),
70 "Delete Groups",
71 "Are you sure that you want to delete groups "
72 + toDelete.keySet()
73 + "?\n"
74 + "This might lead to inconsistencies in the application."))
75 return null;
76
77 UiAdminUtils.beginTransactionIfNeeded(userTransaction);
78 for (String name : toDelete.values())
79 userAdmin.removeRole(name);
80
81 // TODO rather notify the update listener
82 forceRefresh(event);
83 return null;
84 }
85
86 private void forceRefresh(ExecutionEvent event) {
87 IWorkbenchWindow iww = HandlerUtil.getActiveWorkbenchWindow(event);
88 if (iww == null)
89 return;
90 IWorkbenchPage activePage = iww.getActivePage();
91 IWorkbenchPart part = activePage.getActivePart();
92 if (part instanceof GroupsView)
93 ((GroupsView) part).refresh();
94 }
95
96 /* DEPENDENCY INJECTION */
97 public void setUserAdmin(UserAdmin userAdmin) {
98 this.userAdmin = userAdmin;
99 }
100
101 public void setUserTransaction(UserTransaction userTransaction) {
102 this.userTransaction = userTransaction;
103 }
104 }