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.workspaces.signup;
017
018import java.util.List;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.core.user.User;
024import org.ametys.plugins.workspaces.members.ProjectInvitationHelper;
025import org.ametys.plugins.workspaces.project.ProjectManager;
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.web.repository.page.Page;
028import org.ametys.web.usermanagement.UserManagementException;
029
030import com.google.common.collect.Multimap;
031
032/**
033 * This component overrides {@link org.ametys.plugins.userdirectory.signup.UserSignupManager} to handle invitations by mail for workspaces
034 */
035public class UserSignupManager extends org.ametys.plugins.userdirectory.signup.UserSignupManager
036{
037    private ProjectManager _projectManager;
038    private ProjectInvitationHelper _invitationHelper;
039    
040    @Override
041    public void service(ServiceManager serviceManager) throws ServiceException
042    {
043        super.service(serviceManager);
044        _projectManager = (ProjectManager) serviceManager.lookup(ProjectManager.ROLE);
045        _invitationHelper = (ProjectInvitationHelper) serviceManager.lookup(ProjectInvitationHelper.ROLE);
046    }
047    
048    @Override
049    public void temporarySignup(String siteName, String language, String email, String population, String userDirectoryId) throws UserManagementException
050    {
051        String signupSiteName = _getSignupSiteName(siteName);
052        boolean sendMail = signupSiteName.equals(siteName);
053        
054        super.temporarySignup(signupSiteName, language, email, population, userDirectoryId, sendMail);
055    }
056    
057    @Override
058    public void additionalSignupOperations(User createdUser, Multimap<String, I18nizableText> errors) throws UserManagementException
059    {
060        super.additionalSignupOperations(createdUser, errors);
061        
062        _invitationHelper.createMemberFromInvitations(createdUser);
063    }
064    
065    @Override
066    public boolean isPublicSignupAllowed(String siteName)
067    {
068        return super.isPublicSignupAllowed(_getSignupSiteName(siteName));
069    }
070    
071    @Override
072    public Page getSignupPage(String siteName, String language)
073    {
074        return super.getSignupPage(_getSignupSiteName(siteName), language);
075    }
076    
077    @Override
078    public List<Page> getSignupPages(String siteName, String language)
079    {
080        return super.getSignupPages(_getSignupSiteName(siteName), language);
081    }
082    
083    @Override
084    public Page getPwdChangePage(String siteName, String language)
085    {
086        return super.getPwdChangePage(_getSignupSiteName(siteName), language);
087    }
088    
089    @Override
090    public List<Page> getPwdChangePages(String siteName, String language)
091    {
092        return super.getPwdChangePages(_getSignupSiteName(siteName), language);
093    }
094    
095    private String _getSignupSiteName(String siteName)
096    {
097        String signupSite = siteName;
098        
099        List<String> projectNames = _projectManager.getProjectsForSite(siteName);
100        if (!projectNames.isEmpty())
101        {
102            signupSite = _projectManager.getCatalogSiteName();
103        }
104        
105        return signupSite;
106    }
107}