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.UserIdentity; 028import org.ametys.core.user.UserManager; 029import org.ametys.plugins.contentio.synchronize.impl.SQLSynchronizableContentsCollection; 030import org.ametys.plugins.userdirectory.DeleteOrgUnitComponent; 031import org.ametys.plugins.userdirectory.DeleteUserComponent; 032 033/** 034 * SQL synchronizable collection for users 035 */ 036public class SQLSynchronizableUsersCollection extends SQLSynchronizableContentsCollection 037{ 038 private static final String __PARAM_SQL_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(), Collections.singletonList(getLoginSQLColumnName())); 071 072 return mapping; 073 } 074 075 /** 076 * Get the login column name of user SQL table 077 * @return The login column name of user SQL table 078 */ 079 public String getLoginSQLColumnName() 080 { 081 return (String) getParameterValues().get(__PARAM_SQL_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, Object> _getSearchParameters(Map<String, Object> parameters, int offset, int limit, List<Object> sort, List<String> columns) 095 { 096 // Add the sql column name for the login. 097 String loginSQLColumn = getLoginSQLColumnName(); 098 if (!columns.contains(loginSQLColumn)) 099 { 100 columns.add(loginSQLColumn); 101 } 102 103 return super._getSearchParameters(parameters, offset, limit, sort, columns); 104 } 105 106 @Override 107 protected boolean _checkIdObjectValue(String remoteKey, Object idObjectValue, Logger logger) 108 { 109 if (!super._checkIdObjectValue(remoteKey, idObjectValue, logger)) 110 { 111 return false; 112 } 113 114 String login = idObjectValue.toString(); 115 String population = getPopulationId(); 116 if (_userManager.getUser(population, login) == null) 117 { 118 logger.warn("The user " + login + " don't belong to population " + population); 119 return false; 120 } 121 122 return true; 123 } 124 125 @Override 126 protected Map<String, Object> getAdditionalAttributeValues(String idValue, Content content, Map<String, Object> additionalParameters, boolean create, Logger logger) 127 { 128 Map<String, Object> additionalRemoteValues = super.getAdditionalAttributeValues(idValue, content, additionalParameters, create, logger); 129 UserIdentity user = new UserIdentity(idValue, getPopulationId()); 130 additionalRemoteValues.put(UserSCCConstants.USER_ATTRIBUTE_NAME, user); 131 return additionalRemoteValues; 132 } 133 134 @Override 135 protected int _deleteContents(List<Content> contentsToRemove, Logger logger) 136 { 137 return _deleteUserComponent.deleteContentsWithLog(contentsToRemove, Map.of(DeleteOrgUnitComponent.SCC_ID_PARAMETERS_KEY, getId()), Map.of(), logger); 138 } 139}