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