]> git.argeo.org Git - lgpl/argeo-commons.git/blob - NewUserWizard.java
ca817492b5a2cdaf6353e9b1d8faa8fb11830532
[lgpl/argeo-commons.git] / 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.cms.users;
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.dialogs.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.NewUserDetails;
29 import org.eclipse.jface.wizard.Wizard;
30
31 /** Wizard to create a new user */
32 public class NewUserWizard extends Wizard {
33 private final static Log log = LogFactory.getLog(NewUserWizard.class);
34 private Session session;
35 private UserAdminService userAdminService;
36 // private JcrSecurityModel jcrSecurityModel;
37
38 // pages
39 private MainUserInfoWizardPage mainUserInfo;
40
41 public NewUserWizard(Session session, UserAdminService userAdminService) {
42 this.session = session;
43 this.userAdminService = userAdminService;
44 // this.jcrSecurityModel = jcrSecurityModel;
45 }
46
47 @Override
48 public void addPages() {
49 mainUserInfo = new MainUserInfoWizardPage(userAdminService);
50 addPage(mainUserInfo);
51 }
52
53 @Override
54 public boolean performFinish() {
55 if (!canFinish())
56 return false;
57
58 String username = mainUserInfo.getUsername();
59 try {
60 // Node userProfile = SecurityJcrUtils.createUserProfile(session,
61 // username);
62 // Node userProfile = jcrSecurityModel.sync(session, username,
63 // null);
64 // session.getWorkspace().getVersionManager()
65 // .checkout(userProfile.getPath());
66 // mainUserInfo.mapToProfileNode(userProfile);
67 char[] password = mainUserInfo.getPassword();
68 // TODO add roles
69 NewUserDetails jcrUserDetails = new NewUserDetails(username,
70 password) {
71 private static final long serialVersionUID = 7480071525603380742L;
72
73 @Override
74 public void mapToProfileNode(Node userProfile)
75 throws RepositoryException {
76 mainUserInfo.mapToProfileNode(userProfile);
77 }
78 };
79 // session.save();
80 // session.getWorkspace().getVersionManager()
81 // .checkin(userProfile.getPath());
82 userAdminService.createUser(jcrUserDetails);
83 return true;
84 } catch (Exception e) {
85 JcrUtils.discardQuietly(session);
86 Node userHome = UserJcrUtils.getUserHome(session, username);
87 if (userHome != null) {
88 try {
89 userHome.remove();
90 session.save();
91 } catch (RepositoryException e1) {
92 JcrUtils.discardQuietly(session);
93 log.warn("Error when trying to clean up failed new user "
94 + username, e1);
95 }
96 }
97 ErrorFeedback.show("Cannot create new user " + username, e);
98 return false;
99 }
100 }
101
102 public void setSession(Session session) {
103 this.session = session;
104 }
105
106 }