]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/wizards/NewUserWizard.java
Refactor JCR utils and home usage
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui.admin / src / main / java / org / argeo / security / ui / admin / wizards / NewUserWizard.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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.jcr.security.SecurityJcrUtils;
28 import org.argeo.security.UserAdminService;
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
39 // pages
40 private MainUserInfoWizardPage mainUserInfo;
41
42 public NewUserWizard(Session session, UserAdminService userAdminService) {
43 this.session = session;
44 this.userAdminService = userAdminService;
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, username);
61 // session.getWorkspace().getVersionManager()
62 // .checkout(userProfile.getPath());
63 mainUserInfo.mapToProfileNode(userProfile);
64 String password = mainUserInfo.getPassword();
65 // TODO add roles
66 JcrUserDetails jcrUserDetails = new JcrUserDetails(userProfile,
67 password, new GrantedAuthority[0]);
68 session.save();
69 session.getWorkspace().getVersionManager()
70 .checkin(userProfile.getPath());
71 userAdminService.createUser(jcrUserDetails);
72 return true;
73 } catch (Exception e) {
74 JcrUtils.discardQuietly(session);
75 Node userHome = UserJcrUtils.getUserHome(session, username);
76 if (userHome != null) {
77 try {
78 userHome.remove();
79 session.save();
80 } catch (RepositoryException e1) {
81 JcrUtils.discardQuietly(session);
82 log.warn("Error when trying to clean up failed new user "
83 + username, e1);
84 }
85 }
86 ErrorFeedback.show("Cannot create new user " + username, e);
87 return false;
88 }
89 }
90
91 public void setSession(Session session) {
92 this.session = session;
93 }
94
95 }