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
)) {
60 userDetails
= (JcrUserDetails
) userAdminService
61 .loadUserByUsername(username
);
62 } catch (Exception e
) {
63 throw new ArgeoException("Cannot retrieve userDetails for "
67 GrantedAuthority
[] authorities
= {};
69 userDetails
= new JcrUserDetails(session
, username
, null,
71 } catch (RepositoryException e
) {
72 throw new ArgeoException("Cannot retrieve disabled JCR profile");
76 this.setPartProperty("name", username
!= null ? username
: "<new user>");
77 setPartName(username
!= null ? username
: "<new user>");
80 protected void addPages() {
82 addPage(new DefaultUserMainPage(this, userProfile
));
83 addPage(new UserRolesPage(this, userDetails
, userAdminService
));
84 } catch (Exception e
) {
85 throw new ArgeoException("Cannot add pages", e
);
90 public void doSave(IProgressMonitor monitor
) {
92 // TODO: make it more generic
93 DefaultUserMainPage defaultUserMainPage
= (DefaultUserMainPage
) findPage(DefaultUserMainPage
.ID
);
94 if (defaultUserMainPage
.isDirty()) {
95 defaultUserMainPage
.doSave(monitor
);
96 String newPassword
= defaultUserMainPage
.getNewPassword();
97 defaultUserMainPage
.resetNewPassword();
98 if (newPassword
!= null)
99 userDetails
= userDetails
.cloneWithNewPassword(newPassword
);
102 UserRolesPage userRolesPage
= (UserRolesPage
) findPage(UserRolesPage
.ID
);
103 if (userRolesPage
.isDirty()) {
104 userRolesPage
.doSave(monitor
);
105 userDetails
= userDetails
.cloneWithNewRoles(userRolesPage
109 userAdminService
.updateUser(userDetails
);
111 // if (userAdminService.userExists(user.getUsername()))
112 // userAdminService.updateUser(user);
114 // userAdminService.newUser(user);
115 // setPartName(user.getUsername());
117 firePropertyChange(PROP_DIRTY
);
119 userRolesPage
.setUserDetails(userDetails
);
121 // FIXME rather use a refresh command. Fails when called by another
123 // refresh users view
124 IWorkbench iw
= SecurityAdminPlugin
.getDefault().getWorkbench();
125 UsersView usersView
= (UsersView
) iw
.getActiveWorkbenchWindow()
126 .getActivePage().findView(UsersView
.ID
);
127 if (usersView
!= null)
132 public void doSaveAs() {
136 public boolean isSaveAsAllowed() {
140 public void refresh() {
141 UserRolesPage userRolesPage
= (UserRolesPage
) findPage(UserRolesPage
.ID
);
142 userRolesPage
.refresh();
146 public void dispose() {
147 JcrUtils
.logoutQuietly(session
);
151 /* DEPENDENCY INJECTION */
152 public void setUserAdminService(UserAdminService userAdminService
) {
153 this.userAdminService
= userAdminService
;
156 public void setRepository(Repository repository
) {
158 session
= repository
.login();
159 } catch (RepositoryException re
) {
160 throw new ArgeoException("Unable to initialise local session", re
);