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.List;
020
021import org.apache.cocoon.ProcessingException;
022import org.apache.cocoon.environment.ObjectModelHelper;
023import org.apache.cocoon.environment.Request;
024import org.apache.cocoon.xml.AttributesImpl;
025import org.apache.cocoon.xml.XMLUtils;
026import org.apache.commons.lang.StringUtils;
027import org.xml.sax.SAXException;
028
029import org.ametys.core.user.UserIdentity;
030import org.ametys.runtime.i18n.I18nizableText;
031
032import com.google.common.collect.ArrayListMultimap;
033import com.google.common.collect.Multimap;
034
035/**
036 * Validate user input on the change password service form and generate errors, if any.
037 */
038public class UserPasswordValidateGenerator extends UserSignupGenerator
039{
040    
041    @Override
042    public void generate() throws IOException, SAXException, ProcessingException
043    {
044        Request request = ObjectModelHelper.getRequest(objectModel);
045        String siteName = (String) request.getAttribute("site");
046        UserIdentity foUser = _currentUserProvider.getUser();
047        
048        try
049        {
050            Multimap<String, I18nizableText> errors = ArrayListMultimap.create();
051            
052            String password = request.getParameter("password");
053            String passwordConfirmation = request.getParameter("password-confirmation");
054            
055            if (StringUtils.isBlank(password))
056            {
057                errors.put("password", new I18nizableText("plugin.web", "PLUGINS_WEB_USER_SIGNUP_ERROR_PASSWORD_EMPTY"));
058            }
059            else if (!password.equals(passwordConfirmation))
060            {
061                errors.put("password", new I18nizableText("plugin.web", "PLUGINS_WEB_USER_SIGNUP_ERROR_PASSWORD_CONFIRMATION_DOESNT_MATCH"));
062            }
063            
064            List<I18nizableText> inputErrors = _userSignupManager.validatePassword(siteName, password, foUser.getLogin(), foUser.getPopulationId());
065            errors.putAll("password", inputErrors);
066            
067            contentHandler.startDocument();
068            
069            AttributesImpl attrs = new AttributesImpl();
070            
071            XMLUtils.startElement(contentHandler, "user-password-errors", attrs);
072            
073            saxErrors(errors);
074            
075            XMLUtils.endElement(contentHandler, "user-password-errors");
076            
077            contentHandler.endDocument();
078        }
079        catch (UserManagementException e)
080        {
081            getLogger().error("An error occurred validating the password.", e);
082            throw new ProcessingException("An error occurred validating the password.", e);
083        }
084    }
085    
086}