/** * This file Copyright (c) 2003-2008 Magnolia International * Ltd. (http://www.magnolia.info). All rights reserved. * * * This file is dual-licensed under both the Magnolia * Network Agreement and the GNU General Public License. * You may elect to use one or the other of these licenses. * * This file is distributed in the hope that it will be * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. * Redistribution, except as permitted by whichever of the GPL * or MNA you select, is prohibited. * * 1. For the GPL license (GPL), you can redistribute and/or * modify this file under the terms of the GNU General * Public License, Version 3, as published by the Free Software * Foundation. You should have received a copy of the GNU * General Public License, Version 3 along with this program; * if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * 2. For the Magnolia Network Agreement (MNA), this file * and the accompanying materials are made available under the * terms of the MNA which accompanies this distribution, and * is available at http://www.magnolia.info/mna.html * * Any modifications to this file must keep this entire header * intact. * */ package info.magnolia.module.publicuserregistration.frontend; import info.magnolia.cms.beans.config.ContentRepository; import info.magnolia.cms.core.Content; import info.magnolia.cms.core.HierarchyManager; import info.magnolia.cms.util.ContentUtil; import info.magnolia.cms.util.RequestFormUtil; import info.magnolia.content2bean.Content2BeanException; import info.magnolia.content2bean.Content2BeanUtil; import info.magnolia.context.MgnlContext; import info.magnolia.module.publicuserregistration.PublicUserRegistrationConfig; import javax.jcr.RepositoryException; import org.apache.commons.lang.StringUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author wscheidegger * @version $Revision: $ ($Author: $) */ public class RegisterInContext extends Register { /** * logger */ private static Logger log = LoggerFactory.getLogger(RegisterInContext.class); public RegisterInContext(String name, HttpServletRequest request, HttpServletResponse response) { super(name, request, response); } protected boolean doExecute() { String context = RequestFormUtil.getParameter(this.request, "context"); log.debug("Trying to register user in the context " + context); final PublicUserRegistrationConfig purConfig = getModuleConfig(); PublicUserRegistrationConfig contextConfig = null; // In the config object look for a "contexts" subnode with the same name // as the passed in context. String pathToContext = "/modules/public-user-registration/config/contexts/" + context; // Content contextConfigNode = ContentUtil.getContent("config", pathToContext); // do this in system context - otherwise you would have to give the // anonymous role read permission on this path HierarchyManager hm = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.CONFIG); Content contextConfigNode = null; try { contextConfigNode = hm.getContent(pathToContext); } catch (RepositoryException ex) { log.error("Could not get context configuration at "+pathToContext); } if (contextConfigNode != null) { log.debug("Configuration node for context \"" + context + "\" found at " + pathToContext); try { contextConfig = new PublicUserRegistrationConfig(); Content2BeanUtil.setProperties(contextConfig, contextConfigNode, true); // complete the config with default values if (contextConfig.getDefaultGroups() == null) { contextConfig.setDefaultGroups(purConfig.getDefaultGroups()); log.debug("Using default value for \"defaultGroups\""); } if (contextConfig.getDefaultRoles() == null) { contextConfig.setDefaultRoles(purConfig.getDefaultRoles()); log.debug("Using default value for \"defaultRoles\""); } if (contextConfig.getPasswordRetrievalStrategy() == null) { contextConfig.setPasswordRetrievalStrategy(purConfig.getPasswordRetrievalStrategy()); log.debug("Using default value for \"passwordRetrievalStrategy\""); } if (contextConfig.getRealmName() == null) { contextConfig.setRealmName(purConfig.getRealmName()); log.debug("Using default value for \"realmName\""); } if (contextConfig.getRegistrationStrategy() == null) { contextConfig.setRegistrationStrategy(purConfig.getRegistrationStrategy()); log.debug("Using default value for \"registrationStrategy\""); } if (contextConfig.getUserProfileClass() == null) { contextConfig.setUserProfileClass(purConfig.getUserProfileClass()); log.debug("Using default value for \"userProfileClass\""); } } catch (Content2BeanException ex) { ex.printStackTrace(); } } // If no context was provided or no configuration for the context could // be found, use the default pur configuration. if (contextConfig == null) { contextConfig = purConfig; } errorMessages = userRegistrar.validateForCreation(userProfile, contextConfig); // compare passwords if (!StringUtils.equals(userProfile.getPassword(), passwordConfirmation)) { errorMessages.put("password", "validation.password.donotmatch"); } if (!errorMessages.isEmpty()) { return false; } else { userRegistrar.registerUser(userProfile, contextConfig); return true; } } }