]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/commands/RefreshUsersList.java
Prevent modification on current user while launching user batch update
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui.admin / src / main / java / org / argeo / security / ui / admin / commands / RefreshUsersList.java
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.security.ui.admin.commands;
17
18 import java.util.Set;
19
20 import javax.jcr.Node;
21 import javax.jcr.NodeIterator;
22 import javax.jcr.Repository;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.Session;
25 import javax.jcr.query.Query;
26
27 import org.argeo.ArgeoException;
28 import org.argeo.jcr.ArgeoNames;
29 import org.argeo.jcr.ArgeoTypes;
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.ui.handlers.HandlerUtil;
37
38 /**
39 * Refreshes the main user list, removing nodes which are not referenced by user
40 * admin service.
41 */
42 public class RefreshUsersList extends AbstractHandler {
43 private UserAdminService userAdminService;
44 private Repository repository;
45
46 public Object execute(ExecutionEvent event) throws ExecutionException {
47 Set<String> users = userAdminService.listUsers();
48 Session session = null;
49 try {
50 session = repository.login();
51 Query query = session
52 .getWorkspace()
53 .getQueryManager()
54 .createQuery(
55 "select * from [" + ArgeoTypes.ARGEO_USER_HOME
56 + "]", Query.JCR_SQL2);
57 NodeIterator nit = query.execute().getNodes();
58 while (nit.hasNext()) {
59 Node node = nit.nextNode();
60 String username = node.getProperty(ArgeoNames.ARGEO_USER_ID)
61 .getString();
62 if (!users.contains(username))
63 node.remove();
64 }
65 session.save();
66 } catch (RepositoryException e) {
67 JcrUtils.discardQuietly(session);
68 throw new ArgeoException("Cannot list users", e);
69 } finally {
70 JcrUtils.logoutQuietly(session);
71 }
72
73 userAdminService.synchronize();
74 UsersView view = (UsersView) HandlerUtil
75 .getActiveWorkbenchWindow(event).getActivePage()
76 .findView(UsersView.ID);
77 view.refresh();
78 return null;
79 }
80
81 public void setUserAdminService(UserAdminService userAdminService) {
82 this.userAdminService = userAdminService;
83 }
84
85 public void setRepository(Repository repository) {
86 this.repository = repository;
87 }
88
89 }