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.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.User;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.core.user.UserManager;
030import org.ametys.plugins.contentio.synchronize.impl.SQLSynchronizableContentsCollection;
031import org.ametys.plugins.userdirectory.DeleteOrgUnitComponent;
032import org.ametys.plugins.userdirectory.DeleteUserComponent;
033
034/**
035 * SQL synchronizable collection for users
036 */
037public class SQLSynchronizableUsersCollection extends SQLSynchronizableContentsCollection
038{
039    private static final String __PARAM_SQL_TABLE_LOGIN = "login";
040    private static final String __PARAM_POPULATION_ID = "populationId";
041    
042    /** The user manager */
043    protected UserManager _userManager;
044    
045    /** The delete user component */
046    protected DeleteUserComponent _deleteUserComponent;
047    
048    @Override
049    public void service(ServiceManager smanager) throws ServiceException
050    {
051        super.service(smanager);
052        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
053        _deleteUserComponent = (DeleteUserComponent) smanager.lookup(DeleteUserComponent.ROLE);
054    }
055    
056    public boolean handleRightAssignmentContext()
057    {
058        return false;
059    }
060    
061    @Override
062    public String getIdField()
063    {
064        return UserSCCConstants.USER_UNIQUE_ID_ATTRIBUTE_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(getLoginSQLColumnName()));
072        
073        return mapping;
074    }
075    
076    /**
077     * Get the login column name of user SQL table
078     * @return The login column name of user SQL table
079     */
080    public String getLoginSQLColumnName()
081    {
082        return (String) getParameterValues().get(__PARAM_SQL_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 Map<String, Object> _getSearchParameters(Map<String, Object> parameters, int offset, int limit, List<Object> sort, List<String> columns)
096    {
097        // Add the sql column name for the login.
098        String loginSQLColumn = getLoginSQLColumnName();
099        if (!columns.contains(loginSQLColumn))
100        {
101            columns.add(loginSQLColumn);
102        }
103        
104        return super._getSearchParameters(parameters, offset, limit, sort, columns);
105    }
106    
107    @Override
108    protected String checkAndTransformIdObjectValue(String remoteKey, Object idObjectValue, Logger logger)
109    {
110        String login = super.checkAndTransformIdObjectValue(remoteKey, idObjectValue, logger);
111        if (login != null)
112        {
113            String population = getPopulationId();
114            User userFound = _userManager.getUser(population, login);
115            if (userFound != null)
116            {
117                return userFound.getIdentity().getLogin();
118            }
119            else
120            {
121                logger.warn("The user " + login + " don't belong to population " + population);
122                return null;
123            }
124        }
125        
126        return null;
127    }
128    
129    @Override
130    protected Map<String, Object> getAdditionalAttributeValues(String idValue, Content content, Map<String, Object> additionalParameters, boolean create, Logger logger)
131    {
132        Map<String, Object> additionalRemoteValues = super.getAdditionalAttributeValues(idValue, content, additionalParameters, create, logger);
133        UserIdentity user = new UserIdentity(idValue, getPopulationId());
134        additionalRemoteValues.put(UserSCCConstants.USER_ATTRIBUTE_NAME, user);
135        return additionalRemoteValues;
136    }
137    
138    @Override
139    protected int _deleteContents(List<Content> contentsToRemove, Logger logger)
140    {
141        return _deleteUserComponent.deleteContentsWithLog(contentsToRemove, Map.of(DeleteOrgUnitComponent.SCC_ID_PARAMETERS_KEY, getId()), Map.of(), logger);
142    }
143}