]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/commands/DeleteGroups.java
Introduce a UserAdmin wrapper service
[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.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
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.Group;
33 import org.osgi.service.useradmin.UserAdmin;
34 import org.osgi.service.useradmin.UserAdminEvent;
35
36 /** Deletes the selected groups */
37 public class DeleteGroups extends AbstractHandler {
38 public final static String ID = SecurityAdminPlugin.PLUGIN_ID
39 + ".deleteGroups";
40
41 /* DEPENDENCY INJECTION */
42 private UserAdminWrapper userAdminWrapper;
43
44 @SuppressWarnings("unchecked")
45 public Object execute(ExecutionEvent event) throws ExecutionException {
46 ISelection selection = HandlerUtil.getCurrentSelection(event);
47 if (selection.isEmpty())
48 return null;
49
50 List<Group> groups = new ArrayList<Group>();
51 Iterator<Group> it = ((IStructuredSelection) selection).iterator();
52 StringBuilder builder = new StringBuilder();
53 while (it.hasNext()) {
54 Group currGroup = it.next();
55 String groupName = UiAdminUtils.getUsername(currGroup);
56 // TODO add checks
57 builder.append(groupName).append("; ");
58 groups.add(currGroup);
59 }
60
61 if (!MessageDialog.openQuestion(HandlerUtil.getActiveShell(event),
62 "Delete Groups",
63 "Are you sure that you " + "want to delete these groups?\n"
64 + builder.substring(0, builder.length() - 2)))
65 return null;
66
67 userAdminWrapper.beginTransactionIfNeeded();
68 UserAdmin userAdmin = userAdminWrapper.getUserAdmin();
69 for (Group group : groups) {
70 userAdmin.removeRole(group.getName());
71 userAdminWrapper.notifyListeners(new UserAdminEvent(null,
72 UserAdminEvent.ROLE_REMOVED, group));
73 }
74 return null;
75 }
76
77 /* DEPENDENCY INJECTION */
78 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
79 this.userAdminWrapper = userAdminWrapper;
80 }
81 }