001/*
002 *  Copyright 2018 Anyware Services
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 */
016package org.ametys.plugins.userdirectory.synchronize;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.slf4j.Logger;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.core.user.User;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.core.user.UserManager;
029import org.ametys.plugins.contentio.synchronize.impl.LDAPSynchronizableContentsCollection;
030import org.ametys.plugins.userdirectory.DeleteOrgUnitComponent;
031import org.ametys.plugins.userdirectory.DeleteUserComponent;
032
033/**
034 * SQL synchronizable collection for users
035 */
036public class LDAPSynchronizableUsersCollection extends LDAPSynchronizableContentsCollection
037{
038    private static final String __PARAM_LDAP_TABLE_LOGIN = "login";
039    private static final String __PARAM_POPULATION_ID = "populationId";
040    
041    /** The user manager */
042    protected UserManager _userManager;
043
044    /** The delete user component */
045    protected DeleteUserComponent _deleteUserComponent;
046    
047    @Override
048    public void service(ServiceManager smanager) throws ServiceException
049    {
050        super.service(smanager);
051        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
052        _deleteUserComponent = (DeleteUserComponent) smanager.lookup(DeleteUserComponent.ROLE);
053    }
054    
055    public boolean handleRightAssignmentContext()
056    {
057        return false;
058    }
059    
060    @Override
061    public String getIdField()
062    {
063        return UserSCCConstants.USER_UNIQUE_ID_ATTRIBUTE_NAME;
064    }
065    
066    @Override
067    public Map<String, List<String>> getMapping()
068    {
069        Map<String, List<String>> mapping = super.getMapping();
070        mapping.put(getIdField(), List.of(getLoginLDAPColumnName()));
071        
072        return mapping;
073    }
074    
075    /**
076     * Get the login column name of user LDAP
077     * @return The login column name of user LDAP
078     */
079    public String getLoginLDAPColumnName()
080    {
081        return (String) getParameterValues().get(__PARAM_LDAP_TABLE_LOGIN);
082    }
083    
084    /**
085     * Get population Id
086     * @return The population Id
087     */
088    public String getPopulationId()
089    {
090        return (String) getParameterValues().get(__PARAM_POPULATION_ID);
091    }
092    
093    @Override
094    protected String checkAndTransformIdObjectValue(String remoteKey, Object idObjectValue, Logger logger)
095    {
096        String login = super.checkAndTransformIdObjectValue(remoteKey, idObjectValue, logger);
097        if (login != null)
098        {
099            String population = getPopulationId();
100            User userFound = _userManager.getUser(population, login);
101            if (userFound != null)
102            {
103                return userFound.getIdentity().getLogin();
104            }
105            else
106            {
107                logger.warn("The user " + login + " don't belong to population " + population);
108                return null;
109            }
110        }
111        
112        return null;
113    }
114    
115    @Override
116    protected Map<String, Object> getAdditionalAttributeValues(String idValue, Content content, Map<String, Object> additionalParameters, boolean create, Logger logger)
117    {
118        Map<String, Object> additionalRemoteValues = super.getAdditionalAttributeValues(idValue, content, additionalParameters, create, logger);
119        UserIdentity user = new UserIdentity(idValue, getPopulationId());
120        additionalRemoteValues.put(UserSCCConstants.USER_ATTRIBUTE_NAME, user);
121        return additionalRemoteValues;
122    }
123    
124    @Override
125    protected int _deleteContents(List<Content> contentsToRemove, Logger logger)
126    {
127        return _deleteUserComponent.deleteContentsWithLog(contentsToRemove, Map.of(DeleteOrgUnitComponent.SCC_ID_PARAMETERS_KEY, getId()), Map.of(), logger);
128    }
129    
130}