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.Collections;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.collections4.MapUtils;
026import org.slf4j.Logger;
027
028import org.ametys.cms.repository.Content;
029import org.ametys.cms.repository.ModifiableDefaultContent;
030import org.ametys.core.user.UserManager;
031import org.ametys.plugins.contentio.synchronize.impl.LDAPSynchronizableContentsCollection;
032import org.ametys.plugins.repository.AmetysObjectIterable;
033import org.ametys.plugins.userdirectory.DeleteUserComponent;
034
035/**
036 * SQL synchronizable collection for users
037 */
038public class LDAPSynchronizableUsersCollection extends LDAPSynchronizableContentsCollection
039{
040    private static final String __PARAM_LDAP_TABLE_LOGIN = "login";
041    private static final String __PARAM_POPULATION_ID = "populationId";
042    
043    /** The user manager */
044    protected UserManager _userManager;
045
046    /** The user SCC helper */
047    protected UserSCCHelper _userSCCHelper;
048
049    /** The delete user component */
050    protected DeleteUserComponent _deleteUserComponent;
051    
052    @Override
053    public void service(ServiceManager smanager) throws ServiceException
054    {
055        super.service(smanager);
056        _userSCCHelper = (UserSCCHelper) smanager.lookup(UserSCCHelper.ROLE);
057        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
058        _deleteUserComponent = (DeleteUserComponent) smanager.lookup(DeleteUserComponent.ROLE);
059    }
060    
061    @Override
062    public String getIdField()
063    {
064        return UserSCCHelper.USER_UNIQUE_ID_METADATA_NAME;
065    }
066    
067    @Override
068    public Map<String, List<String>> getMapping()
069    {
070        Map<String, List<String>> mapping = super.getMapping();
071        mapping.put(getIdField(), Collections.singletonList(getLoginLDAPColumnName()));
072        
073        return mapping;
074    }
075    
076    /**
077     * Get the login column name of user LDAP
078     * @return The login column name of user LDAP
079     */
080    public String getLoginLDAPColumnName()
081    {
082        return (String) getParameterValues().get(__PARAM_LDAP_TABLE_LOGIN);
083    }
084    
085    /**
086     * Get population Id
087     * @return The population Id
088     */
089    public String getPopulationId()
090    {
091        return (String) getParameterValues().get(__PARAM_POPULATION_ID);
092    }
093    
094    @Override
095    protected boolean _fillContent(Map<String, List<Object>> remoteValues, ModifiableDefaultContent content, boolean create, Logger logger)
096    {
097        boolean hasChanges = super._fillContent(remoteValues, content, create, logger);
098        
099        String newLogin = remoteValues.get(getIdField()).get(0).toString();
100        hasChanges = _userSCCHelper.synchronizeUserMetadata(newLogin, getPopulationId(), content, logger) || hasChanges;
101        
102        return hasChanges;
103    }
104    
105    @Override
106    protected Map<String, Map<String, Object>> internalSearch(Map<String, Object> parameters, int offset, int limit, List<Object> sort, Logger logger)
107    {
108        Map<String, Map<String, Object>> internalSearch = super.internalSearch(parameters, offset, limit, sort, logger);
109        
110        String population = getPopulationId();
111        Map<String, Map<String, Object>> filteredSearchResult = new HashMap<>();
112        for (String login : internalSearch.keySet())
113        {
114            if (_userManager.getUser(population, login) != null)
115            {
116                filteredSearchResult.put(login, internalSearch.get(login));
117            }
118            else
119            {
120                logger.warn("The user " + login + " don't belong to population " + population);
121            }
122        }
123        
124        return filteredSearchResult;
125    }
126    
127    @Override
128    protected void deleteUnexistingContents(Logger logger)
129    {
130        String query = _getContentPathQuery(null, null, null);
131        AmetysObjectIterable<ModifiableDefaultContent> contents = _resolver.query(query);
132        
133        List<Content> contentsToRemove = _getContentsToRemove(contents);
134        if (!contentsToRemove.isEmpty())
135        {
136            _nbDeletedContents += _deleteUserComponent.deleteContentsWithLog(contentsToRemove, MapUtils.EMPTY_SORTED_MAP, MapUtils.EMPTY_SORTED_MAP, logger);
137        }
138    }
139}