001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 */ 018package org.nuxeo.ecm.webengine.invite; 019 020import java.io.Serializable; 021import java.util.HashMap; 022import java.util.Map; 023 024import javax.ws.rs.GET; 025import javax.ws.rs.POST; 026import javax.ws.rs.Path; 027import javax.ws.rs.PathParam; 028import javax.ws.rs.Produces; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 033import org.nuxeo.ecm.user.invite.AlreadyProcessedRegistrationException; 034import org.nuxeo.ecm.user.invite.DefaultInvitationUserFactory; 035import org.nuxeo.ecm.user.invite.UserInvitationService; 036import org.nuxeo.ecm.user.invite.UserRegistrationException; 037import org.nuxeo.ecm.webengine.forms.FormData; 038import org.nuxeo.ecm.webengine.model.Template; 039import org.nuxeo.ecm.webengine.model.WebObject; 040import org.nuxeo.ecm.webengine.model.impl.ModuleRoot; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a> 045 */ 046@Path("/userInvitation") 047@Produces("text/html;charset=UTF-8") 048@WebObject(type = "userRegistration") 049public class UserInvitationObject extends ModuleRoot { 050 private static final Log log = LogFactory.getLog(UserInvitationObject.class); 051 052 @POST 053 @Path("validate") 054 public Object validateTrialForm() { 055 UserInvitationService usr = fetchService(); 056 057 FormData formData = getContext().getForm(); 058 String requestId = formData.getString("RequestId"); 059 String configurationName = formData.getString("ConfigurationName"); 060 String password = formData.getString("Password"); 061 String passwordConfirmation = formData.getString("PasswordConfirmation"); 062 063 // Check if the requestId is an existing one 064 try { 065 usr.checkRequestId(requestId); 066 } catch (AlreadyProcessedRegistrationException ape) { 067 return getView("ValidationErrorTemplate").arg("exceptionMsg", 068 ctx.getMessage("label.error.requestAlreadyProcessed")); 069 } catch (UserRegistrationException ue) { 070 return getView("ValidationErrorTemplate").arg("exceptionMsg", 071 ctx.getMessage("label.error.requestNotExisting", requestId)); 072 } 073 074 // Check if both entered passwords are correct 075 if (password == null || "".equals(password.trim())) { 076 return redisplayFormWithErrorMessage("EnterPassword", 077 ctx.getMessage("label.registerForm.validation.password"), formData); 078 } 079 if (passwordConfirmation == null || "".equals(passwordConfirmation.trim())) { 080 return redisplayFormWithErrorMessage("EnterPassword", 081 ctx.getMessage("label.registerForm.validation.passwordconfirmation"), formData); 082 } 083 password = password.trim(); 084 passwordConfirmation = passwordConfirmation.trim(); 085 if (!password.equals(passwordConfirmation)) { 086 return redisplayFormWithErrorMessage("EnterPassword", 087 ctx.getMessage("label.registerForm.validation.passwordvalidation"), formData); 088 } 089 Map<String, Serializable> registrationData = new HashMap<String, Serializable>(); 090 try { 091 Map<String, Serializable> additionalInfo = buildAdditionalInfos(); 092 093 // Add the entered password to the document model 094 additionalInfo.put(DefaultInvitationUserFactory.PASSWORD_KEY, password); 095 // Validate the creation of the user 096 registrationData = usr.validateRegistration(requestId, additionalInfo); 097 098 } catch (AlreadyProcessedRegistrationException ape) { 099 log.info("Try to validate an already processed registration"); 100 return getView("ValidationErrorTemplate").arg("exceptionMsg", 101 ctx.getMessage("label.error.requestAlreadyProcessed")); 102 } catch (UserRegistrationException ue) { 103 log.warn("Unable to validate registration request", ue); 104 return getView("ValidationErrorTemplate").arg("exceptionMsg", 105 ctx.getMessage("label.errror.requestNotAccepted")); 106 } 107 // User redirected to the logout page after validating the password 108 String webappName = VirtualHostHelper.getWebAppName(getContext().getRequest()); 109 String logoutUrl = "/" + webappName + "/logout"; 110 return getView("UserCreated").arg("data", registrationData).arg("logout", logoutUrl); 111 } 112 113 protected UserInvitationService fetchService() { 114 UserInvitationService usr = Framework.getService(UserInvitationService.class); 115 return usr; 116 } 117 118 @GET 119 @Path("enterpassword/{configurationName}/{requestId}") 120 public Object validatePasswordForm(@PathParam("requestId") String requestId, 121 @PathParam("configurationName") String configurationName) { 122 123 UserInvitationService usr = fetchService(); 124 try { 125 usr.checkRequestId(requestId); 126 } catch (AlreadyProcessedRegistrationException ape) { 127 return getView("ValidationErrorTemplate").arg("exceptionMsg", 128 ctx.getMessage("label.error.requestAlreadyProcessed")); 129 } catch (UserRegistrationException ue) { 130 return getView("ValidationErrorTemplate").arg("exceptionMsg", 131 ctx.getMessage("label.error.requestNotExisting", requestId)); 132 } 133 134 Map<String, String> data = new HashMap<String, String>(); 135 data.put("RequestId", requestId); 136 data.put("ConfigurationName", configurationName); 137 String webappName = VirtualHostHelper.getWebAppName(getContext().getRequest()); 138 String validationRelUrl = usr.getConfiguration(configurationName).getValidationRelUrl(); 139 String valUrl = "/" + webappName + "/" + validationRelUrl; 140 data.put("ValidationUrl", valUrl); 141 return getView("EnterPassword").arg("data", data); 142 } 143 144 protected Map<String, Serializable> buildAdditionalInfos() { 145 return new HashMap<String, Serializable>(); 146 } 147 148 protected Template redisplayFormWithMessage(String messageType, String formName, String message, FormData data) { 149 Map<String, String> savedData = new HashMap<String, String>(); 150 for (String key : data.getKeys()) { 151 savedData.put(key, data.getString(key)); 152 } 153 return getView(formName).arg("data", savedData).arg(messageType, message); 154 } 155 156 protected Template redisplayFormWithInfoMessage(String formName, String message, FormData data) { 157 return redisplayFormWithMessage("info", formName, message, data); 158 } 159 160 protected Template redisplayFormWithErrorMessage(String formName, String message, FormData data) { 161 return redisplayFormWithMessage("err", formName, message, data); 162 } 163 164}