]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/editors/ArgeoUserEditor.java
Move to the root the bundles which will be part of v1.4 and v2.2
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / main / java / org / argeo / security / ui / admin / editors / ArgeoUserEditor.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.editors;
17
18 import javax.jcr.Node;
19 import javax.jcr.Repository;
20 import javax.jcr.RepositoryException;
21 import javax.jcr.Session;
22
23 import org.argeo.ArgeoException;
24 import org.argeo.jcr.JcrUtils;
25 import org.argeo.jcr.UserJcrUtils;
26 import org.argeo.security.UserAdminService;
27 import org.argeo.security.jcr.JcrUserDetails;
28 import org.argeo.security.ui.admin.SecurityAdminPlugin;
29 import org.argeo.security.ui.admin.views.UsersView;
30 import org.eclipse.core.runtime.IProgressMonitor;
31 import org.eclipse.ui.IEditorInput;
32 import org.eclipse.ui.IEditorSite;
33 import org.eclipse.ui.IWorkbench;
34 import org.eclipse.ui.PartInitException;
35 import org.eclipse.ui.forms.editor.FormEditor;
36 import org.springframework.security.GrantedAuthority;
37
38 /** Editor for an Argeo user. */
39 public class ArgeoUserEditor extends FormEditor {
40 public final static String ID = SecurityAdminPlugin.PLUGIN_ID
41 + ".adminArgeoUserEditor";
42
43 /* DEPENDENCY INJECTION */
44 private Session session;
45 private UserAdminService userAdminService;
46
47 // private Node userHome;
48 private Node userProfile;
49 private JcrUserDetails userDetails;
50
51 public void init(IEditorSite site, IEditorInput input)
52 throws PartInitException {
53 super.init(site, input);
54 String username = ((ArgeoUserEditorInput) getEditorInput())
55 .getUsername();
56 userProfile = UserJcrUtils.getUserProfile(session, username);
57
58 if (userAdminService.userExists(username)) {
59 userDetails = (JcrUserDetails) userAdminService
60 .loadUserByUsername(username);
61 } else {
62 GrantedAuthority[] authorities = {};
63 try {
64 userDetails = new JcrUserDetails(session, username, null,
65 authorities);
66 } catch (RepositoryException e) {
67 throw new ArgeoException("Cannot retrieve disabled JCR profile");
68 }
69 }
70
71 this.setPartProperty("name", username != null ? username : "<new user>");
72 setPartName(username != null ? username : "<new user>");
73 }
74
75 protected void addPages() {
76 try {
77 addPage(new DefaultUserMainPage(this, userProfile));
78 addPage(new UserRolesPage(this, userDetails, userAdminService));
79 } catch (Exception e) {
80 throw new ArgeoException("Cannot add pages", e);
81 }
82 }
83
84 @Override
85 public void doSave(IProgressMonitor monitor) {
86 // list pages
87 // TODO: make it more generic
88 DefaultUserMainPage defaultUserMainPage = (DefaultUserMainPage) findPage(DefaultUserMainPage.ID);
89 if (defaultUserMainPage.isDirty()) {
90 defaultUserMainPage.doSave(monitor);
91 String newPassword = defaultUserMainPage.getNewPassword();
92 defaultUserMainPage.resetNewPassword();
93 if (newPassword != null)
94 userDetails = userDetails.cloneWithNewPassword(newPassword);
95 }
96
97 UserRolesPage userRolesPage = (UserRolesPage) findPage(UserRolesPage.ID);
98 if (userRolesPage.isDirty()) {
99 userRolesPage.doSave(monitor);
100 userDetails = userDetails.cloneWithNewRoles(userRolesPage
101 .getRoles());
102 }
103
104 userAdminService.updateUser(userDetails);
105
106 // if (userAdminService.userExists(user.getUsername()))
107 // userAdminService.updateUser(user);
108 // else {
109 // userAdminService.newUser(user);
110 // setPartName(user.getUsername());
111 // }
112 firePropertyChange(PROP_DIRTY);
113
114 userRolesPage.setUserDetails(userDetails);
115
116 // FIXME rather use a refresh command. Fails when called by another
117 // view.
118 // refresh users view
119 IWorkbench iw = SecurityAdminPlugin.getDefault().getWorkbench();
120 UsersView usersView = (UsersView) iw.getActiveWorkbenchWindow()
121 .getActivePage().findView(UsersView.ID);
122 if (usersView != null)
123 usersView.refresh();
124 }
125
126 @Override
127 public void doSaveAs() {
128 }
129
130 @Override
131 public boolean isSaveAsAllowed() {
132 return false;
133 }
134
135 public void refresh() {
136 UserRolesPage userRolesPage = (UserRolesPage) findPage(UserRolesPage.ID);
137 userRolesPage.refresh();
138 }
139
140 @Override
141 public void dispose() {
142 JcrUtils.logoutQuietly(session);
143 super.dispose();
144 }
145
146 /* DEPENDENCY INJECTION */
147 public void setUserAdminService(UserAdminService userAdminService) {
148 this.userAdminService = userAdminService;
149 }
150
151 public void setRepository(Repository repository) {
152 try {
153 session = repository.login();
154 } catch (RepositoryException re) {
155 throw new ArgeoException("Unable to initialise local session", re);
156 }
157 }
158 }