View Javadoc
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.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  /** Delete the selected users */
38  public class DeleteUsers {
39  	// public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".deleteUsers";
40  
41  	/* DEPENDENCY INJECTION */
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  		// ISelection selection = null;// HandlerUtil.getCurrentSelection(event);
49  		// if (selection.isEmpty())
50  		// return null;
51  		List<User> selection = (List<User>) selectionService.getSelection();
52  		if (selection == null)
53  			return;
54  
55  //		Iterator<User> it = ((IStructuredSelection) selection).iterator();
56  //		List<User> users = new ArrayList<User>();
57  		StringBuilder builder = new StringBuilder();
58  
59  		for(User user:selection) {
60  			User currUser = user;
61  //			User currUser = it.next();
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  //			users.add(currUser);
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  		// IWorkbenchPage iwp =
79  		// HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
80  
81  		for (User user : selection) {
82  			String userName = user.getName();
83  			// TODO find a way to close the editor cleanly if opened. Cannot be
84  			// done through the UserAdminListeners, it causes a
85  			// java.util.ConcurrentModificationException because disposing the
86  			// editor unregisters and disposes the listener
87  			// IEditorPart part = iwp.findEditor(new UserEditorInput(userName));
88  			// if (part != null)
89  			// iwp.closeEditor(part, false);
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 }