001/* 002 * (C) Copyright 2015 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 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 */ 018 019package org.nuxeo.scim.server.mapper; 020 021import java.net.URI; 022import java.util.ArrayList; 023import java.util.Collection; 024import java.util.List; 025 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.NuxeoException; 028import org.nuxeo.usermapper.service.UserMapperService; 029 030import com.unboundid.scim.data.Entry; 031import com.unboundid.scim.data.Meta; 032import com.unboundid.scim.data.Name; 033import com.unboundid.scim.data.UserResource; 034import com.unboundid.scim.schema.CoreSchema; 035import com.unboundid.scim.sdk.SCIMConstants; 036 037/** 038 * Static / Hardcoded Mapper implementation (in case {@link UserMapperService} is not available) 039 * 040 * @author tiry 041 * @since 7.4 042 */ 043public class StaticUserMapper extends AbstractMapper { 044 045 public StaticUserMapper(String baseUrl) { 046 super(baseUrl); 047 } 048 049 @Override 050 public UserResource getUserResourceFromNuxeoUser(DocumentModel userModel) throws Exception { 051 052 UserResource userResource = new UserResource(CoreSchema.USER_DESCRIPTOR); 053 054 String userId = (String) userModel.getProperty(um.getUserSchemaName(), um.getUserIdField()); 055 userResource.setUserName(userId); 056 userResource.setId(userId); 057 userResource.setExternalId(userId); 058 059 String fname = (String) userModel.getProperty(um.getUserSchemaName(), "firstName"); 060 String lname = (String) userModel.getProperty(um.getUserSchemaName(), "lastName"); 061 String email = (String) userModel.getProperty(um.getUserSchemaName(), "email"); 062 String company = (String) userModel.getProperty(um.getUserSchemaName(), "company"); 063 064 String displayName = fname + " " + lname; 065 displayName = displayName.trim(); 066 userResource.setDisplayName(displayName); 067 Collection<Entry<String>> emails = new ArrayList<>(); 068 if (email != null) { 069 emails.add(new Entry<String>(email, "string")); 070 userResource.setEmails(emails); 071 } 072 073 Name fullName = new Name(displayName, lname, "", fname, "", ""); 074 userResource.setSingularAttributeValue(SCIMConstants.SCHEMA_URI_CORE, "name", Name.NAME_RESOLVER, fullName); 075 URI location = new URI(baseUrl + "/" + userId); 076 Meta meta = new Meta(null, null, location, "1"); 077 userResource.setMeta(meta); 078 079 // manage groups 080 List<String> groupIds = um.getPrincipal(userId).getAllGroups(); 081 Collection<Entry<String>> groups = new ArrayList<>(); 082 for (String groupId : groupIds) { 083 groups.add(new Entry<String>(groupId, "string")); 084 } 085 userResource.setGroups(groups); 086 087 userResource.setActive(true); 088 089 return userResource; 090 } 091 092 @Override 093 public DocumentModel createNuxeoUserFromUserResource(UserResource user) throws NuxeoException { 094 095 DocumentModel newUser = um.getBareUserModel(); 096 097 String userId = user.getId(); 098 if (userId == null || userId.isEmpty()) { 099 userId = user.getUserName(); 100 } 101 newUser.setProperty(um.getUserSchemaName(), um.getUserIdField(), userId); 102 103 updateUserModel(newUser, user); 104 return um.createUser(newUser); 105 } 106 107 @Override 108 public DocumentModel updateNuxeoUserFromUserResource(String uid, UserResource user) throws NuxeoException { 109 110 DocumentModel userModel = um.getUserModel(uid); 111 if (userModel == null) { 112 return null; 113 } 114 updateUserModel(userModel, user); 115 um.updateUser(userModel); 116 return userModel; 117 } 118 119 protected void updateUserModel(DocumentModel userModel, UserResource userResouce) throws NuxeoException { 120 if (userResouce.getEmails() != null && userResouce.getEmails().size() > 0) { 121 userModel.setProperty(um.getUserSchemaName(), "email", userResouce.getEmails().iterator().next().getValue()); 122 } 123 String displayName = userResouce.getDisplayName(); 124 if (displayName != null && !displayName.isEmpty()) { 125 int idx = displayName.indexOf(" "); 126 if (idx > 0) { 127 userModel.setProperty(um.getUserSchemaName(), "firstName", displayName.substring(0, idx).trim()); 128 userModel.setProperty(um.getUserSchemaName(), "lastName", displayName.substring(idx + 1).trim()); 129 } else { 130 userModel.setProperty(um.getUserSchemaName(), "firstName", displayName); 131 userModel.setProperty(um.getUserSchemaName(), "lastName", ""); 132 } 133 } 134 135 // XXX 136 } 137 138}