001/*
002 *  Copyright 2025 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;
017
018import java.time.Period;
019import java.time.ZonedDateTime;
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.avalon.framework.service.Serviceable;
026
027import org.ametys.cms.repository.Content;
028import org.ametys.core.trace.ForensicLogger;
029import org.ametys.core.user.UserIdentity;
030import org.ametys.core.user.population.UserPopulationDAO;
031import org.ametys.core.user.status.PersonalDataPolicy;
032import org.ametys.core.user.status.UserStatusInfo;
033import org.ametys.runtime.config.Config;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035
036/**
037 * Delete user content that belongs to an unknown user after a delay
038 */
039public class UserContentDataPolicy extends AbstractLogEnabled implements PersonalDataPolicy, Serviceable
040{
041    /** The user directory helper */
042    protected UserDirectoryHelper _userDirectoryHelper;
043    /** The user content delete component */
044    protected DeleteUserComponent _userContentDAO;
045    private Period _retentionPeriod;
046
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _userDirectoryHelper = (UserDirectoryHelper) manager.lookup(UserDirectoryHelper.ROLE);
050        _userContentDAO = (DeleteUserComponent) manager.lookup(DeleteUserComponent.ROLE);
051        
052        Long config = Config.getInstance().<Long>getValue("user-directory.user.content.data-policy.retention.period", false, null);
053        _retentionPeriod = config != null && config >= 0 ? Period.ofMonths(config.intValue()) : null;
054    }
055    
056    public AnonymizationResult process(UserStatusInfo userStatusInfo)
057    {
058        if (_retentionPeriod == null)
059        {
060            return AnonymizationResult.TOO_EARLY;
061        }
062        else if (userStatusInfo.getMissingSinceDate().isBefore(ZonedDateTime.now().minus(_retentionPeriod)))
063        {
064            UserIdentity userIdentity = userStatusInfo.getUserIdentity();
065            List<Content> userContents = _userDirectoryHelper.getUserContents(userIdentity);
066            if (userContents.size() > 0)
067            {
068                _userContentDAO.deleteContents(userContents.stream().map(Content::getId).toList(), Map.of(), Map.of(), getLogger());
069                ForensicLogger.info("data.policy.gdpr.remove.user.contents", Map.of("handled", Integer.toString(userContents.size()), "identity", userIdentity), UserPopulationDAO.SYSTEM_USER_IDENTITY);
070                return AnonymizationResult.PROCESSED;
071            }
072            return AnonymizationResult.NO_DATA;
073        }
074        else
075        {
076            return AnonymizationResult.TOO_EARLY;
077        }
078    }
079
080}