001/*
002 *  Copyright 2012 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.web.usermanagement;
017
018import java.io.IOException;
019import java.util.Arrays;
020import java.util.List;
021import java.util.Set;
022import java.util.stream.Collectors;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.xml.AttributesImpl;
030import org.apache.cocoon.xml.XMLUtils;
031import org.apache.commons.lang.StringUtils;
032import org.xml.sax.SAXException;
033
034import org.ametys.core.user.User;
035import org.ametys.core.user.UserIdentity;
036import org.ametys.core.user.UserManager;
037import org.ametys.core.user.directory.UserDirectory;
038import org.ametys.core.user.population.PopulationContextHelper;
039import org.ametys.core.user.population.UserPopulation;
040import org.ametys.core.user.population.UserPopulationDAO;
041import org.ametys.plugins.core.impl.user.directory.JdbcUserDirectory;
042import org.ametys.runtime.config.Config;
043import org.ametys.runtime.i18n.I18nizableText;
044import org.ametys.web.repository.page.Page;
045
046import com.google.common.collect.Multimap;
047
048/**
049 * Generate information to render the lost password/change password service.
050 */
051public class UserPasswordGenerator extends UserSignupGenerator
052{
053    private UserPopulationDAO _userPopulationDAO;
054    private PopulationContextHelper _populationContextHelper;
055    private UserManager _userManager;
056
057    @Override
058    public void service(ServiceManager serviceManager) throws ServiceException
059    {
060        super.service(serviceManager);
061        
062        _userPopulationDAO = (UserPopulationDAO) serviceManager.lookup(UserPopulationDAO.ROLE);
063        _populationContextHelper = (PopulationContextHelper) serviceManager.lookup(PopulationContextHelper.ROLE);
064        _userManager = (UserManager) serviceManager.lookup(UserManager.ROLE);
065    }
066    
067    @Override
068    public void generate() throws IOException, SAXException, ProcessingException
069    {
070        Request request = ObjectModelHelper.getRequest(objectModel);
071        String siteName = (String) request.getAttribute("site");
072        String language = (String) request.getAttribute("sitemapLanguage");
073        Page page = (Page) request.getAttribute(Page.class.getName());
074        
075        UserIdentity foUser = _currentUserProvider.getUser();
076        Page signupPage = _userSignupManager.getSignupPage(siteName, language);
077        Page pwdChangePage = _userSignupManager.getPwdChangePage(siteName, language);
078        
079        @SuppressWarnings("unchecked")
080        Multimap<String, I18nizableText> errors = (Multimap<String, I18nizableText>) request.getAttribute("errors");
081        String login = request.getParameter("login");
082        String population = request.getParameter("population");
083        String email = request.getParameter("email");
084        String token = request.getParameter("token");
085        
086        contentHandler.startDocument();
087        
088        AttributesImpl attrs = new AttributesImpl();
089        if (page != null)
090        {
091            attrs.addCDATAAttribute("current-page", page.getId());
092        }
093        if (signupPage != null)
094        {
095            attrs.addCDATAAttribute("signup-page-id", signupPage.getId());
096        }
097        if (pwdChangePage != null)
098        {
099            attrs.addCDATAAttribute("password-change-page-id", pwdChangePage.getId());
100        }
101        if (foUser != null)
102        {
103            attrs.addCDATAAttribute("fo-user-login", foUser.getLogin());
104            attrs.addCDATAAttribute("fo-user-population", foUser.getPopulationId());
105        }
106        if (StringUtils.isNotEmpty(email))
107        {
108            attrs.addCDATAAttribute("email", email);
109        }
110        if (StringUtils.isNotEmpty(population))
111        {
112            attrs.addCDATAAttribute("population", population);
113        }
114        if (StringUtils.isNotEmpty(login))
115        {
116            attrs.addCDATAAttribute("login", login);
117        }
118        if (StringUtils.isNotEmpty(token))
119        {
120            attrs.addCDATAAttribute("token", token);
121        }
122        
123        XMLUtils.startElement(contentHandler, "user-password", attrs);
124        
125        if (errors != null)
126        {
127            saxErrors(errors);
128        }
129        
130        if (StringUtils.isNotEmpty(login) && StringUtils.isNotEmpty(population))
131        {
132            User user = _userManager.getUser(population, login);
133            if (user != null)
134            {
135                UserDirectory userDirectory = user.getUserDirectory();
136                if (userDirectory instanceof JdbcUserDirectory jdbcUserDirectory && jdbcUserDirectory.useStrongPassword())
137                {
138                    jdbcUserDirectory.getStrongPasswordRequirements().toSAX(contentHandler, "password-requirements");
139                }
140            }
141        }
142        
143        boolean isPublic = Config.getInstance() != null ? Config.getInstance().getValue("runtime.ametys.public") : false;
144        if (!isPublic)
145        {
146            XMLUtils.startElement(contentHandler, "UserPopulations");
147            
148            Set<String> userPopulationsOnSite = _populationContextHelper.getUserPopulationsOnContexts(Arrays.asList("/sites/" + siteName, "/sites-fo/" + siteName), false, false);
149            List<UserPopulation> usersPopulations = userPopulationsOnSite.stream().map(_userPopulationDAO::getUserPopulation).collect(Collectors.toList());
150            for (UserPopulation up : usersPopulations)
151            {
152                AttributesImpl attrs2 = new AttributesImpl();
153                attrs2.addCDATAAttribute("id", up.getId());
154                XMLUtils.startElement(contentHandler, "UserPopulation", attrs2);
155                up.getLabel().toSAX(contentHandler, "label");
156                XMLUtils.endElement(contentHandler, "UserPopulation");
157            }
158            
159            XMLUtils.endElement(contentHandler, "UserPopulations");
160        }
161        
162        XMLUtils.endElement(contentHandler, "user-password");
163        
164        contentHandler.endDocument();
165    }
166    
167}