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.User; 028import org.ametys.core.user.UserIdentity; 029import org.ametys.core.user.UserManager; 030import org.ametys.plugins.contentio.synchronize.impl.LDAPSynchronizableContentsCollection; 031import org.ametys.plugins.userdirectory.DeleteOrgUnitComponent; 032import org.ametys.plugins.userdirectory.DeleteUserComponent; 033 034/** 035 * SQL synchronizable collection for users 036 */ 037public class LDAPSynchronizableUsersCollection extends LDAPSynchronizableContentsCollection 038{ 039 private static final String __PARAM_LDAP_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(), List.of(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 String checkAndTransformIdObjectValue(String remoteKey, Object idObjectValue, Logger logger) 096 { 097 String login = super.checkAndTransformIdObjectValue(remoteKey, idObjectValue, logger); 098 if (login != null) 099 { 100 String population = getPopulationId(); 101 User userFound = _userManager.getUser(population, login); 102 if (userFound != null) 103 { 104 return userFound.getIdentity().getLogin(); 105 } 106 else 107 { 108 logger.warn("The user " + login + " don't belong to population " + population); 109 return null; 110 } 111 } 112 113 return null; 114 } 115 116 @Override 117 protected Map<String, Object> getAdditionalAttributeValues(String idValue, Content content, Map<String, Object> additionalParameters, boolean create, Logger logger) 118 { 119 Map<String, Object> additionalRemoteValues = super.getAdditionalAttributeValues(idValue, content, additionalParameters, create, logger); 120 UserIdentity user = new UserIdentity(idValue, getPopulationId()); 121 additionalRemoteValues.put(UserSCCConstants.USER_ATTRIBUTE_NAME, user); 122 return additionalRemoteValues; 123 } 124 125 @Override 126 protected int _deleteContents(List<Content> contentsToRemove, Logger logger) 127 { 128 return _deleteUserComponent.deleteContentsWithLog(contentsToRemove, Map.of(DeleteOrgUnitComponent.SCC_ID_PARAMETERS_KEY, getId()), Map.of(), logger); 129 } 130 131}