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