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
.UsersView
;
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 ArgeoUserEditor
extends FormEditor
{
42 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
43 + ".adminArgeoUserEditor";
45 /* DEPENDENCY INJECTION */
46 private Session session
;
47 private UserAdminService userAdminService
;
49 // private Node userHome;
50 private Node userProfile
;
51 private JcrUserDetails userDetails
;
53 public void init(IEditorSite site
, IEditorInput input
)
54 throws PartInitException
{
55 super.init(site
, input
);
56 String username
= ((ArgeoUserEditorInput
) getEditorInput())
58 userProfile
= UserJcrUtils
.getUserProfile(session
, username
);
60 if (userAdminService
.userExists(username
)) {
62 userDetails
= (JcrUserDetails
) userAdminService
63 .loadUserByUsername(username
);
64 } catch (Exception e
) {
65 throw new ArgeoException("Cannot retrieve userDetails for "
70 userDetails
= new JcrUserDetails(session
, username
, null,
71 new ArrayList
<GrantedAuthority
>());
72 } catch (RepositoryException e
) {
73 throw new ArgeoException("Cannot retrieve disabled JCR profile");
77 this.setPartProperty("name", username
!= null ? username
: "<new user>");
78 setPartName(username
!= null ? username
: "<new user>");
81 protected void addPages() {
83 addPage(new DefaultUserMainPage(this, userProfile
));
84 addPage(new UserRolesPage(this, userDetails
, userAdminService
));
85 } catch (Exception e
) {
86 throw new ArgeoException("Cannot add pages", e
);
91 public void doSave(IProgressMonitor monitor
) {
93 // TODO: make it more generic
94 DefaultUserMainPage defaultUserMainPage
= (DefaultUserMainPage
) findPage(DefaultUserMainPage
.ID
);
95 if (defaultUserMainPage
.isDirty()) {
96 defaultUserMainPage
.doSave(monitor
);
97 String newPassword
= defaultUserMainPage
.getNewPassword();
98 defaultUserMainPage
.resetNewPassword();
99 if (newPassword
!= null)
100 userDetails
= userDetails
.cloneWithNewPassword(newPassword
);
103 UserRolesPage userRolesPage
= (UserRolesPage
) findPage(UserRolesPage
.ID
);
104 if (userRolesPage
.isDirty()) {
105 userRolesPage
.doSave(monitor
);
106 userDetails
= userDetails
.cloneWithNewRoles(userRolesPage
110 userAdminService
.updateUser(userDetails
);
112 // if (userAdminService.userExists(user.getUsername()))
113 // userAdminService.updateUser(user);
115 // userAdminService.newUser(user);
116 // setPartName(user.getUsername());
118 firePropertyChange(PROP_DIRTY
);
120 userRolesPage
.setUserDetails(userDetails
);
122 // FIXME rather use a refresh command. Fails when called by another
124 // refresh users view
125 IWorkbench iw
= SecurityAdminPlugin
.getDefault().getWorkbench();
126 UsersView usersView
= (UsersView
) iw
.getActiveWorkbenchWindow()
127 .getActivePage().findView(UsersView
.ID
);
128 if (usersView
!= null)
133 public void doSaveAs() {
137 public boolean isSaveAsAllowed() {
141 public void refresh() {
142 UserRolesPage userRolesPage
= (UserRolesPage
) findPage(UserRolesPage
.ID
);
143 userRolesPage
.refresh();
147 public void dispose() {
148 JcrUtils
.logoutQuietly(session
);
152 /* DEPENDENCY INJECTION */
153 public void setUserAdminService(UserAdminService userAdminService
) {
154 this.userAdminService
= userAdminService
;
157 public void setRepository(Repository repository
) {
159 session
= repository
.login();
160 } catch (RepositoryException re
) {
161 throw new ArgeoException("Unable to initialise local session", re
);