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.ametys.core.group.Group;
023import org.ametys.core.group.GroupIdentity;
024import org.ametys.core.observation.Event;
025import org.ametys.core.user.User;
026import org.ametys.core.user.UserIdentity;
027import org.ametys.core.util.LambdaUtils.BiPredicate;
028import org.ametys.plugins.workspaces.ObservationConstants;
029import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType;
030import org.ametys.plugins.workspaces.project.objects.Project;
031
032/**
033 * Abstract Observer to send mail notifications on workspace member removal
034 */
035public abstract class AbstractRemoveMemberMailNotifierObserver extends AbstractMemberMailNotifierObserver
036{
037    public boolean supports(Event event)
038    {
039        return event.getId().equals(ObservationConstants.EVENT_MEMBER_DELETED);
040    }
041
042    public void observe(Event event, Map<String, Object> transientVars) throws Exception
043    {
044        Map<String, Object> args = event.getArguments();
045        
046        // Retrieve removed member
047        String identity = (String) args.get(ObservationConstants.ARGS_MEMBER_IDENTITY);
048        MemberType identityType = (MemberType) args.get(ObservationConstants.ARGS_MEMBER_IDENTITY_TYPE);
049
050        // Retrieve project
051        Project projectFromArgs = (Project) args.get(ObservationConstants.ARGS_PROJECT);
052        Project project = _resolver.resolveById(projectFromArgs.getId()); // Observer is asynchrone, project JCR session might be closed
053
054        if (MemberType.USER == identityType)
055        {
056            User user = Optional.of(identity)
057                    .map(UserIdentity::stringToUserIdentity)
058                    .filter(userIdentity -> !_projectMemberManager.isProjectMember(project, userIdentity)) // user is not in a group that is still part of the project
059                    .map(_userManager::getUser)
060                    .orElse(null);
061            if (user != null)
062            {
063                sendMail(project, List.of(user));
064            }
065        }
066        else if (MemberType.GROUP == identityType)
067        {
068            GroupIdentity groupIdentity = GroupIdentity.stringToGroupIdentity(identity);
069            if (groupIdentity != null)
070            {
071                Group group = _groupManager.getGroup(groupIdentity);
072                if (group != null)
073                {
074                    List<User> removedUsers = _projectMemberManager.getGroupUsersFromProject(group, project, BiPredicate.not(_projectMemberManager::isProjectMember));
075                    
076                    if (removedUsers.size() > 0)
077                    {
078                        // at least one user in the group has lost access to the project
079                        sendMail(project, removedUsers);
080                    }
081                }
082            }
083        }
084    }
085    
086    /**
087     * Send mail about removed users from project
088     * @param project The project
089     * @param removedUsers The removed users
090     */
091    protected abstract void sendMail(Project project, List<User> removedUsers);
092    
093}