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.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.slf4j.Logger;
025
026import org.ametys.cms.repository.Content;
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 Map<String, Map<String, Object>> internalSearch(Map<String, Object> searchParameters, int offset, int limit, List<Object> sort, Logger logger)
095    {
096        Map<String, Map<String, Object>> internalSearch = super.internalSearch(searchParameters, offset, limit, sort, logger);
097        
098        String population = getPopulationId();
099        Map<String, Map<String, Object>> filteredSearchResult = new HashMap<>();
100        for (String login : internalSearch.keySet())
101        {
102            if (_userManager.getUser(population, login) != null)
103            {
104                filteredSearchResult.put(login, internalSearch.get(login));
105            }
106            else
107            {
108                logger.warn("The user " + login + " don't belong to population " + population);
109            }
110        }
111        
112        return filteredSearchResult;
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}