]> 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
Update license headers
[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.security.UserAdminService;
27 import org.argeo.security.jcr.JcrUserDetails;
28 import org.eclipse.jface.wizard.Wizard;
29 import org.springframework.security.GrantedAuthority;
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
37 // pages
38 private MainUserInfoWizardPage mainUserInfo;
39
40 public NewUserWizard(Session session, UserAdminService userAdminService) {
41 this.session = session;
42 this.userAdminService = userAdminService;
43 }
44
45 @Override
46 public void addPages() {
47 mainUserInfo = new MainUserInfoWizardPage(userAdminService);
48 addPage(mainUserInfo);
49 }
50
51 @Override
52 public boolean performFinish() {
53 if (!canFinish())
54 return false;
55
56 String username = mainUserInfo.getUsername();
57 try {
58 Node userProfile = JcrUtils.createUserProfile(session, username);
59 // session.getWorkspace().getVersionManager()
60 // .checkout(userProfile.getPath());
61 mainUserInfo.mapToProfileNode(userProfile);
62 String password = mainUserInfo.getPassword();
63 // TODO add roles
64 JcrUserDetails jcrUserDetails = new JcrUserDetails(userProfile,
65 password, new GrantedAuthority[0]);
66 session.save();
67 session.getWorkspace().getVersionManager()
68 .checkin(userProfile.getPath());
69 userAdminService.createUser(jcrUserDetails);
70 return true;
71 } catch (Exception e) {
72 JcrUtils.discardQuietly(session);
73 Node userHome = JcrUtils.getUserHome(session, username);
74 if (userHome != null) {
75 try {
76 userHome.remove();
77 session.save();
78 } catch (RepositoryException e1) {
79 JcrUtils.discardQuietly(session);
80 log.warn("Error when trying to clean up failed new user "
81 + username, e1);
82 }
83 }
84 ErrorFeedback.show("Cannot create new user " + username, e);
85 return false;
86 }
87 }
88
89 public void setSession(Session session) {
90 this.session = session;
91 }
92
93 }