001/*
002 *  Copyright 2019 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.workspaces.project.notification;
017
018import java.util.List;
019import java.util.Map;
020import java.util.Optional;
021
022import org.apache.commons.lang3.StringUtils;
023
024import org.ametys.core.group.Group;
025import org.ametys.core.group.GroupIdentity;
026import org.ametys.core.observation.Event;
027import org.ametys.core.user.User;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.plugins.workspaces.ObservationConstants;
030import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType;
031import org.ametys.plugins.workspaces.project.objects.Project;
032import org.ametys.runtime.config.Config;
033import org.ametys.runtime.i18n.I18nizableText;
034
035/**
036 * Observer to send mail notifications on workspace member removal
037 */
038public class RemoveMemberMailNotifierObserver extends AbstractMemberMailNotifierObserver
039{
040    @Override
041    public boolean supports(Event event)
042    {
043        if (ObservationConstants.EVENT_MEMBER_DELETED.equals(event.getId()))
044        {
045            return Config.getInstance().getValue("workspaces.member.removed.send.notification", true, false);
046        }
047        return false;
048    }
049    
050    @Override
051    protected I18nizableText getI18nSubject(Event event, Project project)
052    {
053        return new I18nizableText("plugin." + _pluginName, "PROJECT_MAIL_NOTIFICATION_EVENT_SUBJECT_MEMBER_DELETED", List.of(project.getTitle()));
054    }
055    
056    @Override
057    protected String getMailBodyURI(Event event, Project project)
058    {
059        return "cocoon://_plugins/workspaces/notification-mail-member-event";
060    }
061
062    @Override
063    protected List<String> getUserToNotify(Event event, Project project)
064    {
065        // Retrieve removed member
066        Map<String, Object> args = event.getArguments();
067        String identity = (String) args.get(ObservationConstants.ARGS_MEMBER_IDENTITY);
068        MemberType identityType = (MemberType) args.get(ObservationConstants.ARGS_MEMBER_IDENTITY_TYPE);
069        
070        if (MemberType.USER == identityType)
071        {
072            return Optional.of(identity)
073                    .map(UserIdentity::stringToUserIdentity)
074                    .filter(userIdentity -> !_projectMemberManager.isProjectMember(project, userIdentity)) // user is not in a group that is still part of the project
075                    .map(_userManager::getUser)
076                    .map(User::getEmail)
077                    .filter(StringUtils::isNotEmpty)
078                    .map(e -> List.of(e))
079                    .orElse(List.of());
080        }
081        else if (MemberType.GROUP == identityType)
082        {
083            GroupIdentity groupIdentity = GroupIdentity.stringToGroupIdentity(identity);
084            if (groupIdentity != null)
085            {
086                Group group = _groupManager.getGroup(groupIdentity);
087                if (group != null)
088                {
089                    return group.getUsers().stream()
090                        .filter(u -> !_projectMemberManager.isProjectMember(project, u))
091                        .map(_userManager::getUser)
092                        .map(User::getEmail)
093                        .filter(StringUtils::isNotEmpty)
094                        .toList();
095                }
096            }
097        }
098        return List.of();
099    }
100}