1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.e4.users.handlers;
17
18 import java.util.List;
19
20 import javax.inject.Inject;
21 import javax.inject.Named;
22
23 import org.argeo.cms.auth.UserAdminUtils;
24 import org.argeo.cms.e4.users.UserAdminWrapper;
25 import org.argeo.cms.e4.users.UsersView;
26 import org.eclipse.e4.core.di.annotations.CanExecute;
27 import org.eclipse.e4.core.di.annotations.Execute;
28 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
29 import org.eclipse.e4.ui.services.IServiceConstants;
30 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.swt.widgets.Display;
33 import org.osgi.service.useradmin.User;
34 import org.osgi.service.useradmin.UserAdmin;
35 import org.osgi.service.useradmin.UserAdminEvent;
36
37
38 public class DeleteUsers {
39
40
41
42 @Inject
43 private UserAdminWrapper userAdminWrapper;
44
45 @SuppressWarnings("unchecked")
46 @Execute
47 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, ESelectionService selectionService) {
48
49
50
51 List<User> selection = (List<User>) selectionService.getSelection();
52 if (selection == null)
53 return;
54
55
56
57 StringBuilder builder = new StringBuilder();
58
59 for(User user:selection) {
60 User currUser = user;
61
62 String userName = UserAdminUtils.getUserLocalId(currUser.getName());
63 if (UserAdminUtils.isCurrentUser(currUser)) {
64 MessageDialog.openError(Display.getCurrent().getActiveShell(), "Deletion forbidden",
65 "You cannot delete your own user this way.");
66 return;
67 }
68 builder.append(userName).append("; ");
69
70 }
71
72 if (!MessageDialog.openQuestion(Display.getCurrent().getActiveShell(), "Delete Users",
73 "Are you sure that you want to delete these users?\n" + builder.substring(0, builder.length() - 2)))
74 return;
75
76 userAdminWrapper.beginTransactionIfNeeded();
77 UserAdmin userAdmin = userAdminWrapper.getUserAdmin();
78
79
80
81 for (User user : selection) {
82 String userName = user.getName();
83
84
85
86
87
88
89
90 userAdmin.removeRole(userName);
91 }
92 userAdminWrapper.commitOrNotifyTransactionStateChange();
93
94 for (User user : selection) {
95 userAdminWrapper.notifyListeners(new UserAdminEvent(null, UserAdminEvent.ROLE_REMOVED, user));
96 }
97 }
98
99 @CanExecute
100 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part, ESelectionService selectionService) {
101 return part.getObject() instanceof UsersView && selectionService.getSelection() != null;
102 }
103 }