]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/org.argeo.cms.e4/src/org/argeo/cms/e4/users/handlers/DeleteUsers.java
Simplify hierarchy units
[lgpl/argeo-commons.git] / eclipse / org.argeo.cms.e4 / src / org / argeo / cms / e4 / users / handlers / DeleteUsers.java
1 package org.argeo.cms.e4.users.handlers;
2
3 import java.util.List;
4
5 import javax.inject.Inject;
6 import javax.inject.Named;
7
8 import org.argeo.cms.auth.UserAdminUtils;
9 import org.argeo.cms.e4.users.UserAdminWrapper;
10 import org.argeo.cms.e4.users.UsersView;
11 import org.eclipse.e4.core.di.annotations.CanExecute;
12 import org.eclipse.e4.core.di.annotations.Execute;
13 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
14 import org.eclipse.e4.ui.services.IServiceConstants;
15 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.swt.widgets.Display;
18 import org.osgi.service.useradmin.User;
19 import org.osgi.service.useradmin.UserAdmin;
20 import org.osgi.service.useradmin.UserAdminEvent;
21
22 /** Delete the selected users */
23 public class DeleteUsers {
24 // public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".deleteUsers";
25
26 /* DEPENDENCY INJECTION */
27 @Inject
28 private UserAdminWrapper userAdminWrapper;
29
30 @SuppressWarnings("unchecked")
31 @Execute
32 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, ESelectionService selectionService) {
33 // ISelection selection = null;// HandlerUtil.getCurrentSelection(event);
34 // if (selection.isEmpty())
35 // return null;
36 List<User> selection = (List<User>) selectionService.getSelection();
37 if (selection == null)
38 return;
39
40 // Iterator<User> it = ((IStructuredSelection) selection).iterator();
41 // List<User> users = new ArrayList<User>();
42 StringBuilder builder = new StringBuilder();
43
44 for(User user:selection) {
45 User currUser = user;
46 // User currUser = it.next();
47 String userName = UserAdminUtils.getUserLocalId(currUser.getName());
48 if (UserAdminUtils.isCurrentUser(currUser)) {
49 MessageDialog.openError(Display.getCurrent().getActiveShell(), "Deletion forbidden",
50 "You cannot delete your own user this way.");
51 return;
52 }
53 builder.append(userName).append("; ");
54 // users.add(currUser);
55 }
56
57 if (!MessageDialog.openQuestion(Display.getCurrent().getActiveShell(), "Delete Users",
58 "Are you sure that you want to delete these users?\n" + builder.substring(0, builder.length() - 2)))
59 return;
60
61 userAdminWrapper.beginTransactionIfNeeded();
62 UserAdmin userAdmin = userAdminWrapper.getUserAdmin();
63 // IWorkbenchPage iwp =
64 // HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
65
66 for (User user : selection) {
67 String userName = user.getName();
68 // TODO find a way to close the editor cleanly if opened. Cannot be
69 // done through the UserAdminListeners, it causes a
70 // java.util.ConcurrentModificationException because disposing the
71 // editor unregisters and disposes the listener
72 // IEditorPart part = iwp.findEditor(new UserEditorInput(userName));
73 // if (part != null)
74 // iwp.closeEditor(part, false);
75 userAdmin.removeRole(userName);
76 }
77 userAdminWrapper.commitOrNotifyTransactionStateChange();
78
79 for (User user : selection) {
80 userAdminWrapper.notifyListeners(new UserAdminEvent(null, UserAdminEvent.ROLE_REMOVED, user));
81 }
82 }
83
84 @CanExecute
85 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part, ESelectionService selectionService) {
86 return part.getObject() instanceof UsersView && selectionService.getSelection() != null;
87 }
88 }