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
.editors
;
18 import javax
.jcr
.Node
;
19 import javax
.jcr
.Repository
;
20 import javax
.jcr
.RepositoryException
;
21 import javax
.jcr
.Session
;
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
;
37 /** Editor for an Argeo user. */
38 public class ArgeoUserEditor
extends FormEditor
{
39 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
40 + ".adminArgeoUserEditor";
42 /* DEPENDENCY INJECTION */
43 private Session session
;
44 private UserAdminService userAdminService
;
46 // private Node userHome;
47 private Node userProfile
;
48 private JcrUserDetails userDetails
;
50 public void init(IEditorSite site
, IEditorInput input
)
51 throws PartInitException
{
52 super.init(site
, input
);
53 String username
= ((ArgeoUserEditorInput
) getEditorInput())
55 userProfile
= UserJcrUtils
.getUserProfile(session
, username
);
57 if (userAdminService
.userExists(username
)) {
58 userDetails
= (JcrUserDetails
) userAdminService
59 .loadUserByUsername(username
);
61 GrantedAuthority
[] authorities
= {};
63 userDetails
= new JcrUserDetails(session
, username
, null,
65 } catch (RepositoryException e
) {
66 throw new ArgeoException("Cannot retrieve disabled JCR profile");
70 this.setPartProperty("name", username
!= null ? username
: "<new user>");
71 setPartName(username
!= null ? username
: "<new user>");
74 protected void addPages() {
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
);
84 public void doSave(IProgressMonitor monitor
) {
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
);
96 UserRolesPage userRolesPage
= (UserRolesPage
) findPage(UserRolesPage
.ID
);
97 if (userRolesPage
.isDirty()) {
98 userRolesPage
.doSave(monitor
);
99 userDetails
= userDetails
.cloneWithNewRoles(userRolesPage
103 userAdminService
.updateUser(userDetails
);
105 // if (userAdminService.userExists(user.getUsername()))
106 // userAdminService.updateUser(user);
108 // userAdminService.newUser(user);
109 // setPartName(user.getUsername());
111 firePropertyChange(PROP_DIRTY
);
113 userRolesPage
.setUserDetails(userDetails
);
115 // FIXME rather use a refresh command. Fails when called by another
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)
126 public void doSaveAs() {
130 public boolean isSaveAsAllowed() {
134 public void refresh() {
135 UserRolesPage userRolesPage
= (UserRolesPage
) findPage(UserRolesPage
.ID
);
136 userRolesPage
.refresh();
139 /* DEPENDENCY INJECTION */
140 public void setUserAdminService(UserAdminService userAdminService
) {
141 this.userAdminService
= userAdminService
;
144 public void setRepository(Repository repository
) {
146 session
= repository
.login();
147 } catch (RepositoryException re
) {
148 throw new ArgeoException("Unable to initialise local session", re
);