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