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.io.Serializable; 022import java.net.URI; 023import java.util.HashMap; 024import java.util.Map; 025 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.NuxeoException; 028import org.nuxeo.ecm.core.api.NuxeoPrincipal; 029import org.nuxeo.runtime.api.Framework; 030import org.nuxeo.usermapper.service.UserMapperService; 031 032import com.unboundid.scim.data.Meta; 033import com.unboundid.scim.data.UserResource; 034import com.unboundid.scim.schema.CoreSchema; 035 036/** 037 * Mapper implementation that uses the {@link UserMapperService} 038 * 039 * @author tiry 040 * @since 7.4 041 */ 042public class ConfigurableUserMapper extends AbstractMapper { 043 044 protected UserMapperService mapperService; 045 046 protected static final String MAPPING_NAME = "scim"; 047 048 public ConfigurableUserMapper(String baseUrl) { 049 super(baseUrl); 050 mapperService = Framework.getService(UserMapperService.class); 051 } 052 053 @Override 054 public UserResource getUserResourceFromNuxeoUser(DocumentModel userModel) throws Exception { 055 056 UserResource userResource = new UserResource(CoreSchema.USER_DESCRIPTOR); 057 058 String userId = (String) userModel.getProperty(um.getUserSchemaName(), um.getUserIdField()); 059 userResource.setUserName(userId); 060 userResource.setId(userId); 061 userResource.setExternalId(userId); 062 NuxeoPrincipal principal = um.getPrincipal(userId); 063 064 URI location = new URI(baseUrl + "/" + userId); 065 Meta meta = new Meta(null, null, location, "1"); 066 userResource.setMeta(meta); 067 068 return (UserResource) mapperService.wrapNuxeoPrincipal(MAPPING_NAME, principal, userResource, null); 069 070 } 071 072 @Override 073 public DocumentModel createNuxeoUserFromUserResource(UserResource user) throws NuxeoException { 074 075 NuxeoPrincipal principal = mapperService.getOrCreateAndUpdateNuxeoPrincipal(MAPPING_NAME, user, true, true, 076 null); 077 078 if (principal != null) { 079 return um.getUserModel(principal.getName()); 080 } 081 return null; 082 } 083 084 @Override 085 public DocumentModel updateNuxeoUserFromUserResource(String uid, UserResource user) throws NuxeoException { 086 087 Map<String, Serializable> params = new HashMap<String, Serializable>(); 088 params.put("uid", uid); 089 NuxeoPrincipal principal = mapperService.getOrCreateAndUpdateNuxeoPrincipal(MAPPING_NAME, user, false, true, 090 params); 091 if (principal != null) { 092 return um.getUserModel(principal.getName()); 093 } 094 return null; 095 } 096 097}