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