001/* 002 * (C) Copyright 2011 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * Contributors: 014 * Nuxeo - initial API and implementation 015 */ 016 017package org.nuxeo.ecm.user.invite; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021import org.nuxeo.ecm.core.api.CoreSession; 022import org.nuxeo.ecm.core.api.DocumentModel; 023import org.nuxeo.ecm.core.api.NuxeoPrincipal; 024import org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl; 025import org.nuxeo.ecm.platform.usermanager.UserConfig; 026import org.nuxeo.ecm.platform.usermanager.UserManager; 027import org.nuxeo.runtime.api.Framework; 028 029public class DefaultInvitationUserFactory implements InvitationUserFactory { 030 031 private static final Log log = LogFactory.getLog(DefaultInvitationUserFactory.class); 032 033 public static final String PASSWORD_KEY = "invitationPassword"; 034 035 @Override 036 public void doPostUserCreation(CoreSession session, DocumentModel registrationDoc, NuxeoPrincipal user) 037 throws UserRegistrationException { 038 // Nothing to do in the default implementation 039 } 040 041 @Override 042 public NuxeoPrincipal doCreateUser(CoreSession session, DocumentModel registrationDoc, 043 UserRegistrationConfiguration configuration) throws UserRegistrationException { 044 UserManager userManager = Framework.getService(UserManager.class); 045 046 String email = (String) registrationDoc.getPropertyValue(configuration.getUserInfoEmailField()); 047 if (email == null) { 048 throw new UserRegistrationException("Email address must be specififed"); 049 } 050 051 String login = (String) registrationDoc.getPropertyValue(configuration.getUserInfoUsernameField()); 052 NuxeoPrincipal user = userManager.getPrincipal(login); 053 if (user == null) { 054 DocumentModel newUserDoc = userManager.getBareUserModel(); 055 newUserDoc.setPropertyValue(UserConfig.USERNAME_COLUMN, login); 056 newUserDoc.setPropertyValue(UserConfig.PASSWORD_COLUMN, 057 registrationDoc.getContextData(PASSWORD_KEY)); 058 newUserDoc.setPropertyValue(UserConfig.FIRSTNAME_COLUMN, 059 registrationDoc.getPropertyValue(configuration.getUserInfoFirstnameField())); 060 newUserDoc.setPropertyValue(UserConfig.LASTNAME_COLUMN, 061 registrationDoc.getPropertyValue(configuration.getUserInfoLastnameField())); 062 newUserDoc.setPropertyValue(UserConfig.EMAIL_COLUMN, 063 registrationDoc.getPropertyValue(configuration.getUserInfoEmailField())); 064 newUserDoc.setPropertyValue(UserConfig.COMPANY_COLUMN, 065 registrationDoc.getPropertyValue(configuration.getUserInfoCompanyField())); 066 newUserDoc.setPropertyValue(UserConfig.GROUPS_COLUMN, 067 registrationDoc.getPropertyValue(configuration.getUserInfoGroupsField())); 068 userManager.createUser(newUserDoc); 069 user = userManager.getPrincipal(login); 070 071 log.info("New user created:" + user.getName()); 072 } else { 073 if (!email.equals(((NuxeoPrincipalImpl) user).getEmail())) { 074 throw new UserRegistrationException("This login is not available"); 075 } 076 } 077 return user; 078 } 079}