]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - 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
index 6c894d62a57bedf3315a6c44754e00c9342abbfc..f9a3024ea10770b5cac1be800dee01181bd1a36b 100644 (file)
@@ -1,54 +1,87 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.argeo.security.ui.admin.wizards;
 
 import javax.jcr.Node;
+import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
-import org.argeo.eclipse.ui.dialogs.Error;
-import org.argeo.jcr.ArgeoNames;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.eclipse.ui.ErrorFeedback;
 import org.argeo.jcr.JcrUtils;
+import org.argeo.security.UserAdminService;
 import org.argeo.security.jcr.JcrUserDetails;
 import org.eclipse.jface.wizard.Wizard;
 import org.springframework.security.GrantedAuthority;
-import org.springframework.security.userdetails.UserDetailsManager;
 
 /** Wizard to create a new user */
 public class NewUserWizard extends Wizard {
-       private String homeBasePath = "/home";
+       private final static Log log = LogFactory.getLog(NewUserWizard.class);
        private Session session;
-       private UserDetailsManager userDetailsManager;
+       private UserAdminService userAdminService;
 
        // pages
        private MainUserInfoWizardPage mainUserInfo;
 
-       public NewUserWizard(Session session, UserDetailsManager userDetailsManager) {
+       public NewUserWizard(Session session, UserAdminService userAdminService) {
                this.session = session;
-               this.userDetailsManager = userDetailsManager;
+               this.userAdminService = userAdminService;
        }
 
        @Override
        public void addPages() {
-               mainUserInfo = new MainUserInfoWizardPage();
+               mainUserInfo = new MainUserInfoWizardPage(userAdminService);
                addPage(mainUserInfo);
        }
 
        @Override
        public boolean performFinish() {
+               if (!canFinish())
+                       return false;
+
+               String username = mainUserInfo.getUsername();
                try {
-                       String username = mainUserInfo.getUsername();
-                       Node userHome = JcrUtils.createUserHome(session, homeBasePath,
-                                       username);
-                       Node userProfile = userHome.getNode(ArgeoNames.ARGEO_PROFILE);
+                       Node userProfile = JcrUtils.createUserProfile(session, username);
+                       // session.getWorkspace().getVersionManager()
+                       // .checkout(userProfile.getPath());
                        mainUserInfo.mapToProfileNode(userProfile);
                        String password = mainUserInfo.getPassword();
-                       JcrUserDetails jcrUserDetails = new JcrUserDetails(
-                                       userHome.getPath(), username, password, true, true, true,
-                                       true, new GrantedAuthority[0]);
+                       // TODO add roles
+                       JcrUserDetails jcrUserDetails = new JcrUserDetails(userProfile,
+                                       password, new GrantedAuthority[0]);
                        session.save();
-                       userDetailsManager.createUser(jcrUserDetails);
+                       session.getWorkspace().getVersionManager()
+                                       .checkin(userProfile.getPath());
+                       userAdminService.createUser(jcrUserDetails);
                        return true;
                } catch (Exception e) {
                        JcrUtils.discardQuietly(session);
-                       Error.show("Cannot create new user", e);
+                       Node userHome = JcrUtils.getUserHome(session, username);
+                       if (userHome != null) {
+                               try {
+                                       userHome.remove();
+                                       session.save();
+                               } catch (RepositoryException e1) {
+                                       JcrUtils.discardQuietly(session);
+                                       log.warn("Error when trying to clean up failed new user "
+                                                       + username, e1);
+                               }
+                       }
+                       ErrorFeedback.show("Cannot create new user " + username, e);
                        return false;
                }
        }