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.HashSet;
021import java.util.List;
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.UserIdentity;
035import org.ametys.core.user.population.PopulationContextHelper;
036import org.ametys.core.user.population.UserPopulation;
037import org.ametys.core.user.population.UserPopulationDAO;
038import org.ametys.runtime.config.Config;
039import org.ametys.runtime.i18n.I18nizableText;
040import org.ametys.web.repository.page.Page;
041
042import com.google.common.collect.Multimap;
043
044/**
045 * Generate information to render the lost password/change password service.
046 */
047public class UserPasswordGenerator extends UserSignupGenerator
048{
049    private UserPopulationDAO _userPopulationDAO;
050    private PopulationContextHelper _populationContextHelper;
051
052    @Override
053    public void service(ServiceManager serviceManager) throws ServiceException
054    {
055        super.service(serviceManager);
056        
057        _userPopulationDAO = (UserPopulationDAO) serviceManager.lookup(UserPopulationDAO.ROLE);
058        _populationContextHelper = (PopulationContextHelper) serviceManager.lookup(PopulationContextHelper.ROLE);
059    }
060    
061    @Override
062    public void generate() throws IOException, SAXException, ProcessingException
063    {
064        Request request = ObjectModelHelper.getRequest(objectModel);
065        String siteName = (String) request.getAttribute("site");
066        String language = (String) request.getAttribute("sitemapLanguage");
067        Page page = (Page) request.getAttribute(Page.class.getName());
068        
069        UserIdentity foUser = _currentUserProvider.getUser();
070        Page signupPage = _userSignupManager.getSignupPage(siteName, language);
071        Page pwdChangePage = _userSignupManager.getPwdChangePage(siteName, language);
072        Page userMainPrefsPage = _userSignupManager.getUserMainPrefsPage(siteName, language);
073        
074        @SuppressWarnings("unchecked")
075        Multimap<String, I18nizableText> errors = (Multimap<String, I18nizableText>) request.getAttribute("errors");
076        String login = request.getParameter("login");
077        String population = request.getParameter("population");
078        String email = request.getParameter("email");
079        String token = request.getParameter("token");
080        
081        contentHandler.startDocument();
082        
083        AttributesImpl attrs = new AttributesImpl();
084        if (page != null)
085        {
086            attrs.addCDATAAttribute("current-page", page.getId());
087        }
088        if (signupPage != null)
089        {
090            attrs.addCDATAAttribute("signup-page-id", signupPage.getId());
091        }
092        if (pwdChangePage != null)
093        {
094            attrs.addCDATAAttribute("password-change-page-id", pwdChangePage.getId());
095        }
096        if (userMainPrefsPage != null)
097        {
098            attrs.addCDATAAttribute("user-prefs-page-id", userMainPrefsPage.getId());
099        }
100        if (foUser != null)
101        {
102            attrs.addCDATAAttribute("fo-user-login", foUser.getLogin());
103            attrs.addCDATAAttribute("fo-user-population", foUser.getPopulationId());
104        }
105        if (StringUtils.isNotEmpty(email))
106        {
107            attrs.addCDATAAttribute("email", email);
108        }
109        if (StringUtils.isNotEmpty(population))
110        {
111            attrs.addCDATAAttribute("population", population);
112        }
113        if (StringUtils.isNotEmpty(login))
114        {
115            attrs.addCDATAAttribute("login", login);
116        }
117        if (StringUtils.isNotEmpty(token))
118        {
119            attrs.addCDATAAttribute("token", token);
120        }
121        
122        XMLUtils.startElement(contentHandler, "user-password", attrs);
123        
124        if (errors != null)
125        {
126            saxErrors(errors);
127        }
128        
129        boolean isPublic = Config.getInstance() != null ? Config.getInstance().getValueAsBoolean("runtime.ametys.public") : false;
130        if (isPublic)
131        {
132            XMLUtils.startElement(contentHandler, "UserPopulations");
133            
134            List<String> userPopulationsOnSite = _populationContextHelper.getUserPopulationsOnContexts(new HashSet<>(Arrays.asList("/sites/" + siteName, "/sites-fo/" + siteName)), false);
135            List<UserPopulation> usersPopulations = userPopulationsOnSite.stream().map(_userPopulationDAO::getUserPopulation).collect(Collectors.toList());
136            for (UserPopulation up : usersPopulations)
137            {
138                AttributesImpl attrs2 = new AttributesImpl();
139                attrs2.addCDATAAttribute("id", up.getId());
140                XMLUtils.startElement(contentHandler, "UserPopulation", attrs2);
141                up.getLabel().toSAX(contentHandler, "label");
142                XMLUtils.endElement(contentHandler, "UserPopulation");
143            }
144            
145            XMLUtils.endElement(contentHandler, "UserPopulations");
146        }
147        
148        XMLUtils.endElement(contentHandler, "user-password");
149        
150        contentHandler.endDocument();
151    }
152    
153}