2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.security
.ui
.admin
.commands
;
18 import java
.util
.Iterator
;
20 import java
.util
.TreeMap
;
22 import javax
.jcr
.Node
;
23 import javax
.jcr
.RepositoryException
;
24 import javax
.jcr
.Session
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.ArgeoException
;
29 import org
.argeo
.jcr
.ArgeoNames
;
30 import org
.argeo
.jcr
.JcrUtils
;
31 import org
.argeo
.security
.UserAdminService
;
32 import org
.eclipse
.core
.commands
.AbstractHandler
;
33 import org
.eclipse
.core
.commands
.ExecutionEvent
;
34 import org
.eclipse
.core
.commands
.ExecutionException
;
35 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
36 import org
.eclipse
.jface
.viewers
.ISelection
;
37 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
38 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
40 /** Deletes the selected user nodes */
41 public class DeleteUser
extends AbstractHandler
{
42 private final static Log log
= LogFactory
.getLog(DeleteUser
.class);
44 private UserAdminService userAdminService
;
46 @SuppressWarnings("unchecked")
47 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
48 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
49 if (selection
.isEmpty())
52 Map
<String
, Node
> toDelete
= new TreeMap
<String
, Node
>();
53 Iterator
<Node
> it
= ((IStructuredSelection
) selection
).iterator();
54 nodes
: while (it
.hasNext()) {
55 Node profileNode
= it
.next();
57 String userName
= profileNode
.getProperty(
58 ArgeoNames
.ARGEO_USER_ID
).getString();
59 if (userName
.equals(profileNode
.getSession().getUserID())) {
60 log
.warn("Cannot delete its own user: " + userName
);
63 toDelete
.put(userName
, profileNode
);
64 } catch (RepositoryException e
) {
65 log
.warn("Cannot interpred user " + profileNode
);
71 HandlerUtil
.getActiveShell(event
),
73 "Are you sure that you want to delete users "
76 + "This may lead to inconsistencies in the application."))
79 for (String username
: toDelete
.keySet()) {
80 Session session
= null;
82 Node profileNode
= toDelete
.get(username
);
83 userAdminService
.deleteUser(username
);
84 profileNode
.getParent().remove();
85 session
= profileNode
.getSession();
87 } catch (RepositoryException e
) {
88 JcrUtils
.discardQuietly(session
);
89 throw new ArgeoException("Cannot list users", e
);
93 userAdminService
.synchronize();
94 // UsersView view = (UsersView) HandlerUtil
95 // .getActiveWorkbenchWindow(event).getActivePage()
96 // .findView(UsersView.ID);
101 public void setUserAdminService(UserAdminService userAdminService
) {
102 this.userAdminService
= userAdminService
;