]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/wizards/NewUserWizard.java
c2d041f56832bf07f6ddc92bb6440983fe1d6e36
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / wizards / NewUserWizard.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.security.ui.admin.wizards;
17
18 import javax.jcr.Node;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.Session;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.eclipse.ui.ErrorFeedback;
25 import org.argeo.jcr.JcrUtils;
26 import org.argeo.jcr.UserJcrUtils;
27 import org.argeo.security.UserAdminService;
28 import org.argeo.security.jcr.JcrSecurityModel;
29 import org.argeo.security.jcr.JcrUserDetails;
30 import org.eclipse.jface.wizard.Wizard;
31 import org.springframework.security.GrantedAuthority;
32
33 /** Wizard to create a new user */
34 public class NewUserWizard extends Wizard {
35 private final static Log log = LogFactory.getLog(NewUserWizard.class);
36 private Session session;
37 private UserAdminService userAdminService;
38 private JcrSecurityModel jcrSecurityModel;
39
40 // pages
41 private MainUserInfoWizardPage mainUserInfo;
42
43 public NewUserWizard(Session session, UserAdminService userAdminService,
44 JcrSecurityModel jcrSecurityModel) {
45 this.session = session;
46 this.userAdminService = userAdminService;
47 this.jcrSecurityModel = jcrSecurityModel;
48 }
49
50 @Override
51 public void addPages() {
52 mainUserInfo = new MainUserInfoWizardPage(userAdminService);
53 addPage(mainUserInfo);
54 }
55
56 @Override
57 public boolean performFinish() {
58 if (!canFinish())
59 return false;
60
61 String username = mainUserInfo.getUsername();
62 try {
63 // Node userProfile = SecurityJcrUtils.createUserProfile(session,
64 // username);
65 Node userProfile = jcrSecurityModel.sync(session, username, null);
66 session.getWorkspace().getVersionManager()
67 .checkout(userProfile.getPath());
68 mainUserInfo.mapToProfileNode(userProfile);
69 String password = mainUserInfo.getPassword();
70 // TODO add roles
71 JcrUserDetails jcrUserDetails = new JcrUserDetails(userProfile,
72 password, new GrantedAuthority[0]);
73 session.save();
74 session.getWorkspace().getVersionManager()
75 .checkin(userProfile.getPath());
76 userAdminService.createUser(jcrUserDetails);
77 return true;
78 } catch (Exception e) {
79 JcrUtils.discardQuietly(session);
80 Node userHome = UserJcrUtils.getUserHome(session, username);
81 if (userHome != null) {
82 try {
83 userHome.remove();
84 session.save();
85 } catch (RepositoryException e1) {
86 JcrUtils.discardQuietly(session);
87 log.warn("Error when trying to clean up failed new user "
88 + username, e1);
89 }
90 }
91 ErrorFeedback.show("Cannot create new user " + username, e);
92 return false;
93 }
94 }
95
96 public void setSession(Session session) {
97 this.session = session;
98 }
99
100 }