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 java
.util
.ArrayList
;
20 import javax
.jcr
.Node
;
21 import javax
.jcr
.Repository
;
22 import javax
.jcr
.RepositoryException
;
23 import javax
.jcr
.Session
;
25 import org
.argeo
.ArgeoException
;
26 import org
.argeo
.jcr
.JcrUtils
;
27 import org
.argeo
.jcr
.UserJcrUtils
;
28 import org
.argeo
.security
.UserAdminService
;
29 import org
.argeo
.security
.jcr
.JcrUserDetails
;
30 import org
.argeo
.security
.ui
.admin
.SecurityAdminPlugin
;
31 import org
.argeo
.security
.ui
.admin
.views
.JcrUsersView
;
32 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
33 import org
.eclipse
.ui
.IEditorInput
;
34 import org
.eclipse
.ui
.IEditorSite
;
35 import org
.eclipse
.ui
.IWorkbench
;
36 import org
.eclipse
.ui
.PartInitException
;
37 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
38 import org
.springframework
.security
.core
.GrantedAuthority
;
40 /** Editor for an Argeo user. */
41 public class JcrArgeoUserEditor
extends FormEditor
{
42 private static final long serialVersionUID
= 1933296330339252869L;
44 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
45 + ".adminArgeoUserEditor";
47 /* DEPENDENCY INJECTION */
48 private Session session
;
49 private UserAdminService userAdminService
;
51 // private Node userHome;
52 private Node userProfile
;
53 private JcrUserDetails userDetails
;
55 public void init(IEditorSite site
, IEditorInput input
)
56 throws PartInitException
{
57 super.init(site
, input
);
58 String username
= ((ArgeoUserEditorInput
) getEditorInput())
60 userProfile
= UserJcrUtils
.getUserProfile(session
, username
);
62 if (userAdminService
.userExists(username
)) {
64 userDetails
= (JcrUserDetails
) userAdminService
65 .loadUserByUsername(username
);
66 } catch (Exception e
) {
67 throw new ArgeoException("Cannot retrieve userDetails for "
72 userDetails
= new JcrUserDetails(session
, username
, null,
73 new ArrayList
<GrantedAuthority
>());
74 } catch (RepositoryException e
) {
75 throw new ArgeoException("Cannot retrieve disabled JCR profile");
79 this.setPartProperty("name", username
!= null ? username
: "<new user>");
80 setPartName(username
!= null ? username
: "<new user>");
83 protected void addPages() {
85 addPage(new DefaultUserMainPage(this, userProfile
));
86 addPage(new UserRolesPage(this, userDetails
, userAdminService
));
87 } catch (Exception e
) {
88 throw new ArgeoException("Cannot add pages", e
);
93 public void doSave(IProgressMonitor monitor
) {
95 // TODO: make it more generic
96 DefaultUserMainPage defaultUserMainPage
= (DefaultUserMainPage
) findPage(DefaultUserMainPage
.ID
);
97 if (defaultUserMainPage
.isDirty()) {
98 defaultUserMainPage
.doSave(monitor
);
99 String newPassword
= defaultUserMainPage
.getNewPassword();
100 defaultUserMainPage
.resetNewPassword();
101 if (newPassword
!= null)
102 userDetails
= userDetails
.cloneWithNewPassword(newPassword
);
105 UserRolesPage userRolesPage
= (UserRolesPage
) findPage(UserRolesPage
.ID
);
106 if (userRolesPage
.isDirty()) {
107 userRolesPage
.doSave(monitor
);
108 userDetails
= userDetails
.cloneWithNewRoles(userRolesPage
112 userAdminService
.updateUser(userDetails
);
114 // if (userAdminService.userExists(user.getUsername()))
115 // userAdminService.updateUser(user);
117 // userAdminService.newUser(user);
118 // setPartName(user.getUsername());
120 firePropertyChange(PROP_DIRTY
);
122 userRolesPage
.setUserDetails(userDetails
);
124 // FIXME rather use a refresh command. Fails when called by another
126 // refresh users view
127 IWorkbench iw
= SecurityAdminPlugin
.getDefault().getWorkbench();
128 JcrUsersView usersView
= (JcrUsersView
) iw
.getActiveWorkbenchWindow()
129 .getActivePage().findView(JcrUsersView
.ID
);
130 if (usersView
!= null)
137 public void doSaveAs() {
141 public boolean isSaveAsAllowed() {
145 public void refresh() {
146 UserRolesPage userRolesPage
= (UserRolesPage
) findPage(UserRolesPage
.ID
);
147 userRolesPage
.refresh();
151 public void dispose() {
152 JcrUtils
.logoutQuietly(session
);
156 /* DEPENDENCY INJECTION */
157 public void setUserAdminService(UserAdminService userAdminService
) {
158 this.userAdminService
= userAdminService
;
161 public void setRepository(Repository repository
) {
163 session
= repository
.login();
164 } catch (RepositoryException re
) {
165 throw new ArgeoException("Unable to initialise local session", re
);