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