001/*
002 * (C) Copyright 2014 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 *     Nelson Silva <nelson.silva@inevo.pt>
018 */
019package org.nuxeo.ecm.platform.auth.saml.user;
020
021import java.security.Principal;
022import java.util.Map;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.NuxeoException;
028import org.nuxeo.ecm.platform.auth.saml.SAMLCredential;
029import org.nuxeo.ecm.platform.usermanager.UserManager;
030import org.nuxeo.runtime.api.Framework;
031
032public abstract class AbstractUserResolver implements UserResolver {
033
034    private static final Log log = LogFactory.getLog(AbstractUserResolver.class);
035
036
037    public abstract String findNuxeoUser(SAMLCredential userInfo);
038
039    public abstract String getLoginName(SAMLCredential userInfo);
040
041    public DocumentModel createNuxeoUser(String nuxeoLogin) {
042        DocumentModel userDoc;
043
044        try {
045            UserManager userManager = Framework.getService(UserManager.class);
046
047            userDoc = userManager.getBareUserModel();
048            userDoc.setPropertyValue(userManager.getUserIdField(), nuxeoLogin);
049
050            userManager.createUser(userDoc);
051
052        } catch (NuxeoException e) {
053            log.error("Error while creating user " + nuxeoLogin + "in UserManager", e);
054            return null;
055        }
056
057        return userDoc;
058    }
059
060    public abstract DocumentModel updateUserInfo(DocumentModel user, SAMLCredential userInfo);
061
062    @Override
063    public String findOrCreateNuxeoUser(SAMLCredential userInfo) {
064
065        String login = getLoginName(userInfo);
066        if (login!=null) {
067            UserManager userManager = Framework.getService(UserManager.class);
068            Principal principal = userManager.getPrincipal(login);
069            if (principal!=null) {
070                return login;
071            }
072        }
073        String user = findNuxeoUser(userInfo);
074        if (user == null) {
075            DocumentModel userDoc = createNuxeoUser(login);
076            updateUserInfo(userDoc, userInfo);
077        }
078        return user;
079    }
080
081    @Override
082    public void init(Map<String, String> parameters) {
083        //NOP
084    }
085
086}