001/*
002 *  Copyright 2016 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.Collection;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.ProcessingException;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.apache.commons.lang.StringUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.user.directory.ModifiableUserDirectory;
034import org.ametys.core.user.population.UserPopulationDAO;
035import org.ametys.runtime.i18n.I18nizableText;
036import org.ametys.runtime.parameter.Errors;
037import org.ametys.runtime.parameter.Parameter;
038import org.ametys.runtime.parameter.ParameterHelper.ParameterType;
039import org.ametys.web.repository.page.ZoneItem;
040
041import com.google.common.collect.ArrayListMultimap;
042import com.google.common.collect.Multimap;
043
044/**
045 * Validate user input on the sign-up service form and generate errors, if any.
046 */
047public class UserSignupValidateGenerator extends UserSignupGenerator
048{
049    /** The DAO for user population */
050    private UserPopulationDAO _userPopulationDAO;
051
052    @Override
053    public void service(ServiceManager serviceManager) throws ServiceException
054    {
055        super.service(serviceManager);
056        _userPopulationDAO = (UserPopulationDAO) serviceManager.lookup(UserPopulationDAO.ROLE);
057    }
058    
059    @Override
060    public void generate() throws IOException, SAXException, ProcessingException
061    {
062        Request request = ObjectModelHelper.getRequest(objectModel);
063        String siteName = (String) request.getAttribute("site");
064        
065        String firstName = request.getParameter("firstname");
066        String lastName = request.getParameter("lastname");
067        String email = request.getParameter("email");
068        String captchaValue = request.getParameter("captcha");
069        String captchaKey = request.getParameter("captcha-key");
070        String tos = request.getParameter("tos");
071        String tosPageId = request.getParameter("tos-page-id");
072        
073        try
074        {
075            Multimap<String, I18nizableText> errors = ArrayListMultimap.create();
076            
077            // Validate the input
078            if (StringUtils.isBlank(captchaKey) || StringUtils.isBlank(captchaValue))
079            {
080                errors.put("captcha", new I18nizableText("plugin.web", "PLUGINS_WEB_USER_SIGNUP_FORM_ERROR_INVALID_CAPTCHA"));
081            }
082            if (StringUtils.isNotEmpty(tosPageId) && !"true".equals(tos))
083            {
084                errors.put("tos", new I18nizableText("plugin.web", "PLUGINS_WEB_USER_SIGNUP_FORM_ERROR_TOS_NOT_CHECKED"));
085            }
086            
087            ZoneItem zoneItem = (ZoneItem) request.getAttribute(ZoneItem.class.getName());
088            String[] populationAndDirectory = zoneItem.getServiceParameters().getString("userdirectory").split("#", 2);
089            String populationId = populationAndDirectory[0];
090            String userDirectoryId = populationAndDirectory[1];
091            if (_userSignupManager.userExists(email, populationId))
092            {
093                errors.put("global", new I18nizableText("user-email-already-exists"));
094            }
095            
096            Map<String, String> additionalFields = getAdditionalValues(request, populationId, userDirectoryId);
097            
098            Map<String, Errors> inputErrors = _userSignupManager.validate(siteName, email, firstName, lastName, additionalFields);
099            
100            for (String field : inputErrors.keySet())
101            {
102                errors.putAll(field, inputErrors.get(field).getErrors());
103            }
104            
105            contentHandler.startDocument();
106            
107            AttributesImpl attrs = new AttributesImpl();
108            
109            XMLUtils.startElement(contentHandler, "user-signup-errors", attrs);
110            
111            saxErrors(errors);
112            
113            XMLUtils.endElement(contentHandler, "user-signup-errors");
114            
115            contentHandler.endDocument();
116        }
117        catch (UserManagementException e)
118        {
119            getLogger().error("An error occurred validating the sign-up information.", e);
120            throw new ProcessingException("An error occurred validating the sign-up information.", e);
121        }
122    }
123    
124    /**
125     * Get FO user manager's custom field values.
126     * @param request the request.
127     * @param population The population id
128     * @param userDirectoryId The user directory id 
129     * @return the custom field values.
130     * @throws UserManagementException if an error occurs.
131     */
132    protected Map<String, String> getAdditionalValues(Request request, String population, String userDirectoryId) throws UserManagementException
133    {
134        Map<String, String> additionalFields = new HashMap<>();
135        
136        ModifiableUserDirectory userDirectory = (ModifiableUserDirectory) _userPopulationDAO.getUserPopulation(population).getUserDirectory(userDirectoryId);
137        Collection<? extends Parameter<ParameterType>> fields = userDirectory.getModel();
138        
139        for (Parameter<ParameterType> field : fields)
140        {
141            String fieldId = field.getId();
142            if (!UserSignupAction.STANDARD_FIELDS.contains(fieldId))
143            {
144                String value = request.getParameter(fieldId);
145                
146                additionalFields.put(fieldId, value);
147            }
148        }
149        
150        return additionalFields;
151    }
152    
153}