001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * Contributors: 016 * Nuxeo - initial API and implementation 017 */ 018 019package org.nuxeo.ecm.user.invite; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.core.api.CoreSession; 024import org.nuxeo.ecm.core.api.DocumentModel; 025import org.nuxeo.ecm.core.api.NuxeoPrincipal; 026import org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl; 027import org.nuxeo.ecm.platform.usermanager.UserConfig; 028import org.nuxeo.ecm.platform.usermanager.UserManager; 029import org.nuxeo.runtime.api.Framework; 030 031public class DefaultInvitationUserFactory implements InvitationUserFactory { 032 033 private static final Log log = LogFactory.getLog(DefaultInvitationUserFactory.class); 034 035 public static final String PASSWORD_KEY = "invitationPassword"; 036 037 @Override 038 public void doPostUserCreation(CoreSession session, DocumentModel registrationDoc, NuxeoPrincipal user) 039 throws UserRegistrationException { 040 // Nothing to do in the default implementation 041 } 042 043 @Override 044 public NuxeoPrincipal doCreateUser(CoreSession session, DocumentModel registrationDoc, 045 UserRegistrationConfiguration configuration) throws UserRegistrationException { 046 UserManager userManager = Framework.getService(UserManager.class); 047 048 String email = (String) registrationDoc.getPropertyValue(configuration.getUserInfoEmailField()); 049 if (email == null) { 050 throw new UserRegistrationException("Email address must be specififed"); 051 } 052 053 String login = (String) registrationDoc.getPropertyValue(configuration.getUserInfoUsernameField()); 054 NuxeoPrincipal user = userManager.getPrincipal(login); 055 if (user == null) { 056 DocumentModel newUserDoc = userManager.getBareUserModel(); 057 newUserDoc.setPropertyValue(UserConfig.USERNAME_COLUMN, login); 058 newUserDoc.setPropertyValue(UserConfig.PASSWORD_COLUMN, 059 registrationDoc.getContextData(PASSWORD_KEY)); 060 newUserDoc.setPropertyValue(UserConfig.FIRSTNAME_COLUMN, 061 registrationDoc.getPropertyValue(configuration.getUserInfoFirstnameField())); 062 newUserDoc.setPropertyValue(UserConfig.LASTNAME_COLUMN, 063 registrationDoc.getPropertyValue(configuration.getUserInfoLastnameField())); 064 newUserDoc.setPropertyValue(UserConfig.EMAIL_COLUMN, 065 registrationDoc.getPropertyValue(configuration.getUserInfoEmailField())); 066 newUserDoc.setPropertyValue(UserConfig.COMPANY_COLUMN, 067 registrationDoc.getPropertyValue(configuration.getUserInfoCompanyField())); 068 newUserDoc.setPropertyValue(UserConfig.GROUPS_COLUMN, 069 registrationDoc.getPropertyValue(configuration.getUserInfoGroupsField())); 070 newUserDoc.setPropertyValue(UserConfig.TENANT_ID_COLUMN, 071 registrationDoc.getPropertyValue(configuration.getUserInfoTenantIdField())); 072 userManager.createUser(newUserDoc); 073 user = userManager.getPrincipal(login); 074 075 log.info("New user created:" + user.getName()); 076 } else { 077 if (!email.equals(((NuxeoPrincipalImpl) user).getEmail())) { 078 throw new UserRegistrationException("This login is not available"); 079 } 080 } 081 return user; 082 } 083}