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