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.lang3.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 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 078 contentHandler.startDocument(); 079 080 AttributesImpl attrs = new AttributesImpl(); 081 _saxAttributes(attrs, request, siteName); 082 083 XMLUtils.startElement(contentHandler, "user-password", attrs); 084 085 if (errors != null) 086 { 087 saxErrors(errors); 088 } 089 090 if (StringUtils.isNotEmpty(login) && StringUtils.isNotEmpty(population)) 091 { 092 User user = _userManager.getUser(population, login); 093 if (user != null) 094 { 095 UserDirectory userDirectory = user.getUserDirectory(); 096 if (userDirectory instanceof JdbcUserDirectory jdbcUserDirectory && jdbcUserDirectory.useStrongPassword()) 097 { 098 jdbcUserDirectory.getStrongPasswordRequirements().toSAX(contentHandler, "password-requirements"); 099 } 100 } 101 } 102 103 boolean isPublic = Config.getInstance() != null ? Config.getInstance().getValue("runtime.ametys.public") : false; 104 if (!isPublic) 105 { 106 XMLUtils.startElement(contentHandler, "UserPopulations"); 107 108 Set<String> userPopulationsOnSite = _populationContextHelper.getUserPopulationsOnContexts(Arrays.asList("/sites/" + siteName, "/sites-fo/" + siteName), false, false); 109 List<UserPopulation> usersPopulations = userPopulationsOnSite.stream().map(_userPopulationDAO::getUserPopulation).collect(Collectors.toList()); 110 for (UserPopulation up : usersPopulations) 111 { 112 AttributesImpl attrs2 = new AttributesImpl(); 113 attrs2.addCDATAAttribute("id", up.getId()); 114 XMLUtils.startElement(contentHandler, "UserPopulation", attrs2); 115 up.getLabel().toSAX(contentHandler, "label"); 116 XMLUtils.endElement(contentHandler, "UserPopulation"); 117 } 118 119 XMLUtils.endElement(contentHandler, "UserPopulations"); 120 } 121 122 XMLUtils.endElement(contentHandler, "user-password"); 123 124 contentHandler.endDocument(); 125 } 126 127 private void _saxAttributes(AttributesImpl attrs, Request request, String siteName) 128 { 129 String language = (String) request.getAttribute("sitemapLanguage"); 130 Page page = (Page) request.getAttribute(Page.class.getName()); 131 132 UserIdentity foUser = _currentUserProvider.getUser(); 133 Page signupPage = _userSignupManager.getSignupPage(siteName, language); 134 Page pwdChangePage = _userSignupManager.getPwdChangePage(siteName, language); 135 136 String login = request.getParameter("login"); 137 String population = request.getParameter("population"); 138 String email = request.getParameter("email"); 139 String token = request.getParameter("token"); 140 141 if (page != null) 142 { 143 attrs.addCDATAAttribute("current-page", page.getId()); 144 } 145 if (signupPage != null) 146 { 147 attrs.addCDATAAttribute("signup-page-id", signupPage.getId()); 148 } 149 if (pwdChangePage != null) 150 { 151 attrs.addCDATAAttribute("password-change-page-id", pwdChangePage.getId()); 152 } 153 if (foUser != null) 154 { 155 attrs.addCDATAAttribute("fo-user-login", foUser.getLogin()); 156 attrs.addCDATAAttribute("fo-user-population", foUser.getPopulationId()); 157 } 158 if (StringUtils.isNotEmpty(email)) 159 { 160 attrs.addCDATAAttribute("email", email); 161 } 162 if (StringUtils.isNotEmpty(population)) 163 { 164 attrs.addCDATAAttribute("population", population); 165 } 166 if (StringUtils.isNotEmpty(login)) 167 { 168 attrs.addCDATAAttribute("login", login); 169 } 170 if (StringUtils.isNotEmpty(token)) 171 { 172 attrs.addCDATAAttribute("token", token); 173 } 174 } 175}