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
.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
;
38 /** Editor for an Argeo user. */
39 public class ArgeoUserEditor
extends FormEditor
{
40 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
41 + ".adminArgeoUserEditor";
43 /* DEPENDENCY INJECTION */
44 private Session session
;
45 private UserAdminService userAdminService
;
47 // private Node userHome;
48 private Node userProfile
;
49 private JcrUserDetails userDetails
;
51 public void init(IEditorSite site
, IEditorInput input
)
52 throws PartInitException
{
53 super.init(site
, input
);
54 String username
= ((ArgeoUserEditorInput
) getEditorInput())
56 userProfile
= UserJcrUtils
.getUserProfile(session
, username
);
58 if (userAdminService
.userExists(username
)) {
59 userDetails
= (JcrUserDetails
) userAdminService
60 .loadUserByUsername(username
);
62 GrantedAuthority
[] authorities
= {};
64 userDetails
= new JcrUserDetails(session
, username
, null,
66 } catch (RepositoryException e
) {
67 throw new ArgeoException("Cannot retrieve disabled JCR profile");
71 this.setPartProperty("name", username
!= null ? username
: "<new user>");
72 setPartName(username
!= null ? username
: "<new user>");
75 protected void addPages() {
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
);
85 public void doSave(IProgressMonitor monitor
) {
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
);
97 UserRolesPage userRolesPage
= (UserRolesPage
) findPage(UserRolesPage
.ID
);
98 if (userRolesPage
.isDirty()) {
99 userRolesPage
.doSave(monitor
);
100 userDetails
= userDetails
.cloneWithNewRoles(userRolesPage
104 userAdminService
.updateUser(userDetails
);
106 // if (userAdminService.userExists(user.getUsername()))
107 // userAdminService.updateUser(user);
109 // userAdminService.newUser(user);
110 // setPartName(user.getUsername());
112 firePropertyChange(PROP_DIRTY
);
114 userRolesPage
.setUserDetails(userDetails
);
116 // FIXME rather use a refresh command. Fails when called by another
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)
127 public void doSaveAs() {
131 public boolean isSaveAsAllowed() {
135 public void refresh() {
136 UserRolesPage userRolesPage
= (UserRolesPage
) findPage(UserRolesPage
.ID
);
137 userRolesPage
.refresh();
141 public void dispose() {
142 JcrUtils
.logoutQuietly(session
);
146 /* DEPENDENCY INJECTION */
147 public void setUserAdminService(UserAdminService userAdminService
) {
148 this.userAdminService
= userAdminService
;
151 public void setRepository(Repository repository
) {
153 session
= repository
.login();
154 } catch (RepositoryException re
) {
155 throw new ArgeoException("Unable to initialise local session", re
);