Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
GTNPORTAL-1805 Ability to send email after registration of new user
Browse files Browse the repository at this point in the history
  • Loading branch information
mposolda committed Aug 8, 2012
1 parent e9953c3 commit 9202633
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 3 deletions.
@@ -0,0 +1,105 @@
/******************************************************************************
* JBoss, a division of Red Hat *
* Copyright 2010, Red Hat Middleware, LLC, and individual *
* contributors as indicated by the @authors tag. See the *
* copyright.txt in the distribution for a full listing of *
* individual contributors. *
* *
* This is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 2.1 of *
* the License, or (at your option) any later version. *
* *
* This software is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this software; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
******************************************************************************/
package org.exoplatform.portal.registration;

import org.exoplatform.container.xml.InitParams;
import org.exoplatform.services.mail.MailService;
import org.exoplatform.services.organization.User;
import org.gatein.common.logging.Logger;
import org.gatein.common.logging.LoggerFactory;

/**
* This service contains actions, which should be performed after successful registration of new user (Sending mail,
* Activating of user, which is disabled by default etc.)<br>
*
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
* @version $Revision$
*/
public class PostRegistrationService
{
private final String mailFrom;
private final String mailTo;
private final String mailSubject;
private final String mailMessage;
private final MailService mailService;
private final Boolean sendMailEnabled; // If false, then mails won't be send.
private static final Logger log = LoggerFactory.getLogger(PostRegistrationService.class);


public PostRegistrationService(InitParams params, MailService mailService)
{
this.mailService = mailService;
this.sendMailEnabled = Boolean.valueOf(params.getValueParam("sendMailAfterRegistration").getValue());
this.mailFrom = params.getValueParam("mailFrom").getValue();
this.mailTo = params.getValueParam("mailTo").getValue();
this.mailSubject = params.getValueParam("mailSubject").getValue();
this.mailMessage = params.getValueParam("mailMessage").getValue();
}

/**
* This method can be used to send mail to administrator after successful registration of new user.
*
* @param user which just register himself to portal.
*/
public void sendMailAfterSuccessfulRegistration(User user)
{
// return if sending mails disabled in configuration.
if (!sendMailEnabled)
{
log.debug("Sending of mails disabled. Mail won't be send about creating of user " + user.getUserName());
return;
}

try
{
String subject = replaceTokens(mailSubject, user);
String message = replaceTokens(mailMessage, user);

log.debug("Sending mail about the creating of user " + user.getUserName());
mailService.sendMessage(mailFrom, mailTo, subject, message);
}
catch (Exception e)
{
log.error("Error when sending mail to admin after registration of user " + user.getUserName(), e);
}
}

/**
* Replace tokens in message with real values of user. This can be used to inform administrator
* about attributes of concrete user.
*
* @param param
* @param user
* @return
*/
private String replaceTokens(String param, User user)
{
String result = param.replaceAll("\\$\\{user.userName\\}", user.getUserName());
result = result.replaceAll("\\$\\{user.firstName\\}", user.getFirstName());
result = result.replaceAll("\\$\\{user.lastName\\}", user.getLastName());
result = result.replaceAll("\\$\\{user.email\\}", user.getEmail());
return result;
}

}

Expand Up @@ -18,13 +18,15 @@
*/

package org.exoplatform.account.webui.component;


import java.util.ArrayList;
import java.util.List;

import nl.captcha.Captcha;

import org.exoplatform.portal.registration.PostRegistrationService;
import org.exoplatform.services.organization.OrganizationService;
import org.exoplatform.services.organization.User;
import org.exoplatform.services.organization.UserHandler;
import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.webui.application.WebuiRequestContext;
Expand Down Expand Up @@ -58,6 +60,8 @@ public class UIRegisterForm extends UIForm
{

private final static String[] ACTIONS = {"Subscribe", "Reset"};

static final String ATTR_USER = "UIRegisterForm$User";

public UIRegisterForm() throws Exception
{
Expand Down Expand Up @@ -111,9 +115,14 @@ public void execute(Event<UIRegisterForm> event) throws Exception

if (registerInput.save(userHandler, context))
{
//TODO: Send email and add Account Activating feature
//TODO: Add Account Activating feature
UIApplication uiApp = context.getUIApplication();
uiApp.addMessage(new ApplicationMessage("UIRegisterForm.registerWithSuccess.message", null));
uiApp.addMessage(new ApplicationMessage("UIRegisterForm.registerWithSuccess.message", null));

// Send mail to administrator after successful registration of user
PostRegistrationService postRegistrationService = uiApp.getApplicationComponent(PostRegistrationService.class);
User user = (User)context.getAttribute(ATTR_USER);
postRegistrationService.sendMailAfterSuccessfulRegistration(user);
}

//Invalidate the capcha
Expand Down
Expand Up @@ -184,6 +184,9 @@ public boolean save(UserHandler userHandler, WebuiRequestContext context) throws

userHandler.createUser(user, true);//Broadcast user creaton event
reset();//Reset the input form

// save user as attribute to WebuiRequestContext for later use
context.setAttribute(UIRegisterForm.ATTR_USER, user);
return true;
}
}
Expand Up @@ -20,6 +20,53 @@
xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_2.xsd http://www.exoplaform.org/xml/ns/kernel_1_2.xsd"
xmlns="http://www.exoplaform.org/xml/ns/kernel_1_2.xsd">

<component>
<key>org.exoplatform.portal.registration.PostRegistrationService</key>
<type>org.exoplatform.portal.registration.PostRegistrationService</type>
<init-params>
<value-param>
<name>sendMailAfterRegistration</name>
<description>Switch this parameter to value 'true' if you want
to be notified about registration of new user.</description>
<value>false</value>
</value-param>
<value-param>
<name>mailFrom</name>
<description>This will be used as from header in admin mail</description>
<value>gatein-portal@example.com</value>
</value-param>
<value-param>
<name>mailTo</name>
<description>This should be admin mail address, where email
about registration of new user will be send.</description>
<value>portal-admin@example.com</value>
</value-param>
<value-param>
<name>mailSubject</name>
<description>Subject of mail. Tokens like ${user.userName}
will be replaced with real attributes of registered user in final message.</description>
<value>Registration of user ${user.userName}</value>
</value-param>
<value-param>
<name>mailMessage</name>
<description>Content of mail. Tokens like ${user.userName}
will be replaced with real attributes of registered user in final message.</description>
<value>Hi admin,

User ${user.userName} just register himself into your portal. Full data about user:

Username: ${user.userName}
First name: ${user.firstName}
Last name: ${user.lastName}
E-mail: ${user.email}

----------------------
This message has been generated automatically as notification about registration of new user into portal.
</value>
</value-param>
</init-params>
</component>

<component>
<key>org.exoplatform.web.security.errorlogin.InvalidLoginAttemptsService</key>
<type>org.exoplatform.web.security.errorlogin.InvalidLoginAttemptsService</type>
Expand Down

0 comments on commit 9202633

Please sign in to comment.