X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=security%2Fplugins%2Forg.argeo.security.ui.admin%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fsecurity%2Fui%2Fadmin%2Fwizards%2FNewUserWizard.java;h=c2d041f56832bf07f6ddc92bb6440983fe1d6e36;hb=3a3d316af102ba410d1d9e6de349d0c8f7ac044f;hp=6e9704a4961f9b2a4be1d0e0b73d9d47851c4825;hpb=019e0f2af17286be08ab17c1c9e1d8ba871ec9b2;p=lgpl%2Fargeo-commons.git diff --git a/security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/wizards/NewUserWizard.java b/security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/wizards/NewUserWizard.java index 6e9704a49..c2d041f56 100644 --- a/security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/wizards/NewUserWizard.java +++ b/security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/wizards/NewUserWizard.java @@ -1,19 +1,100 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * 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.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.eclipse.ui.ErrorFeedback; +import org.argeo.jcr.JcrUtils; +import org.argeo.jcr.UserJcrUtils; +import org.argeo.security.UserAdminService; +import org.argeo.security.jcr.JcrSecurityModel; +import org.argeo.security.jcr.JcrUserDetails; import org.eclipse.jface.wizard.Wizard; +import org.springframework.security.GrantedAuthority; +/** Wizard to create a new user */ public class NewUserWizard extends Wizard { + private final static Log log = LogFactory.getLog(NewUserWizard.class); + private Session session; + private UserAdminService userAdminService; + private JcrSecurityModel jcrSecurityModel; + + // pages private MainUserInfoWizardPage mainUserInfo; + public NewUserWizard(Session session, UserAdminService userAdminService, + JcrSecurityModel jcrSecurityModel) { + this.session = session; + this.userAdminService = userAdminService; + this.jcrSecurityModel = jcrSecurityModel; + } + @Override public void addPages() { - mainUserInfo = new MainUserInfoWizardPage(); + mainUserInfo = new MainUserInfoWizardPage(userAdminService); addPage(mainUserInfo); } @Override public boolean performFinish() { - return false; + if (!canFinish()) + return false; + + String username = mainUserInfo.getUsername(); + try { + // Node userProfile = SecurityJcrUtils.createUserProfile(session, + // username); + Node userProfile = jcrSecurityModel.sync(session, username, null); + session.getWorkspace().getVersionManager() + .checkout(userProfile.getPath()); + mainUserInfo.mapToProfileNode(userProfile); + String password = mainUserInfo.getPassword(); + // TODO add roles + JcrUserDetails jcrUserDetails = new JcrUserDetails(userProfile, + password, new GrantedAuthority[0]); + session.save(); + session.getWorkspace().getVersionManager() + .checkin(userProfile.getPath()); + userAdminService.createUser(jcrUserDetails); + return true; + } catch (Exception e) { + JcrUtils.discardQuietly(session); + Node userHome = UserJcrUtils.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; + } + } + + public void setSession(Session session) { + this.session = session; } }