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
.argeo
.security
.ui
.admin
.views
.UsersView
;
33 import org
.eclipse
.core
.commands
.AbstractHandler
;
34 import org
.eclipse
.core
.commands
.ExecutionEvent
;
35 import org
.eclipse
.core
.commands
.ExecutionException
;
36 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
37 import org
.eclipse
.jface
.viewers
.ISelection
;
38 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
39 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
41 /** Deletes the select user nodes */
42 public class DeleteUser
extends AbstractHandler
{
43 private final static Log log
= LogFactory
.getLog(DeleteUser
.class);
45 private UserAdminService userAdminService
;
47 @SuppressWarnings("unchecked")
48 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
49 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
50 if (selection
.isEmpty())
53 Map
<String
, Node
> toDelete
= new TreeMap
<String
, Node
>();
54 Iterator
<Node
> it
= ((IStructuredSelection
) selection
).iterator();
55 nodes
: while (it
.hasNext()) {
56 Node profileNode
= it
.next();
58 String userName
= profileNode
.getProperty(
59 ArgeoNames
.ARGEO_USER_ID
).getString();
60 if (userName
.equals(profileNode
.getSession().getUserID())) {
61 log
.warn("Cannot delete its own user: " + userName
);
64 toDelete
.put(userName
, profileNode
);
65 } catch (RepositoryException e
) {
66 log
.warn("Cannot interpred user " + profileNode
);
72 HandlerUtil
.getActiveShell(event
),
74 "Are you sure that you want to delete users "
77 + "This may lead to inconsistencies in the application."))
80 for (String username
: toDelete
.keySet()) {
81 Session session
= null;
83 Node profileNode
= toDelete
.get(username
);
84 userAdminService
.deleteUser(username
);
85 profileNode
.getParent().remove();
86 session
= profileNode
.getSession();
88 } catch (RepositoryException e
) {
89 JcrUtils
.discardQuietly(session
);
90 throw new ArgeoException("Cannot list users", e
);
94 userAdminService
.synchronize();
95 UsersView view
= (UsersView
) HandlerUtil
96 .getActiveWorkbenchWindow(event
).getActivePage()
97 .findView(UsersView
.ID
);
102 public void setUserAdminService(UserAdminService userAdminService
) {
103 this.userAdminService
= userAdminService
;