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