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.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 073 @SuppressWarnings("unchecked") 074 Multimap<String, I18nizableText> errors = (Multimap<String, I18nizableText>) request.getAttribute("errors"); 075 String login = request.getParameter("login"); 076 String population = request.getParameter("population"); 077 String email = request.getParameter("email"); 078 String token = request.getParameter("token"); 079 080 contentHandler.startDocument(); 081 082 AttributesImpl attrs = new AttributesImpl(); 083 if (page != null) 084 { 085 attrs.addCDATAAttribute("current-page", page.getId()); 086 } 087 if (signupPage != null) 088 { 089 attrs.addCDATAAttribute("signup-page-id", signupPage.getId()); 090 } 091 if (pwdChangePage != null) 092 { 093 attrs.addCDATAAttribute("password-change-page-id", pwdChangePage.getId()); 094 } 095 if (foUser != null) 096 { 097 attrs.addCDATAAttribute("fo-user-login", foUser.getLogin()); 098 attrs.addCDATAAttribute("fo-user-population", foUser.getPopulationId()); 099 } 100 if (StringUtils.isNotEmpty(email)) 101 { 102 attrs.addCDATAAttribute("email", email); 103 } 104 if (StringUtils.isNotEmpty(population)) 105 { 106 attrs.addCDATAAttribute("population", population); 107 } 108 if (StringUtils.isNotEmpty(login)) 109 { 110 attrs.addCDATAAttribute("login", login); 111 } 112 if (StringUtils.isNotEmpty(token)) 113 { 114 attrs.addCDATAAttribute("token", token); 115 } 116 117 XMLUtils.startElement(contentHandler, "user-password", attrs); 118 119 if (errors != null) 120 { 121 saxErrors(errors); 122 } 123 124 boolean isPublic = Config.getInstance() != null ? Config.getInstance().getValue("runtime.ametys.public") : false; 125 if (!isPublic) 126 { 127 XMLUtils.startElement(contentHandler, "UserPopulations"); 128 129 Set<String> userPopulationsOnSite = _populationContextHelper.getUserPopulationsOnContexts(Arrays.asList("/sites/" + siteName, "/sites-fo/" + siteName), false, false); 130 List<UserPopulation> usersPopulations = userPopulationsOnSite.stream().map(_userPopulationDAO::getUserPopulation).collect(Collectors.toList()); 131 for (UserPopulation up : usersPopulations) 132 { 133 AttributesImpl attrs2 = new AttributesImpl(); 134 attrs2.addCDATAAttribute("id", up.getId()); 135 XMLUtils.startElement(contentHandler, "UserPopulation", attrs2); 136 up.getLabel().toSAX(contentHandler, "label"); 137 XMLUtils.endElement(contentHandler, "UserPopulation"); 138 } 139 140 XMLUtils.endElement(contentHandler, "UserPopulations"); 141 } 142 143 XMLUtils.endElement(contentHandler, "user-password"); 144 145 contentHandler.endDocument(); 146 } 147 148}