User Id creation with an activation link email

thumbnail
Subhasis Roy, modified 12 Years ago. Expert Posts: 275 Join Date: 1/20/12 Recent Posts
Hi All,

I have the following requirement,

Steps;
1.- User enters in the "create account" form and enters all the user data: username, email, password, reminder question, etc..
2.- Liferay creates the account but the user is not active.
3.- Liferay sends an e-mail to the user e-mail account with a portal link to an url that activates the user
4.- When user clicks on the link in the email, his account gets activated

Is it possible in liferay. I am using Liferay 6.0 CE
thumbnail
Prakash Khanchandani, modified 12 Years ago. Expert Posts: 329 Join Date: 2/10/11 Recent Posts
I would think it would be possible. But it would require custom development.

Right now I can think of is the following:
1) using a Listener hook when a User is added to make it inactive after creation.
2) Or using a struts-action hook to intercept the Create account request and doing the needful.
3) The URL generated can be to a custom portlet which might look for a combination of parameters to activate that particular user.

Hope this helps.
Dharmen Panchal, modified 12 Years ago. New Member Posts: 13 Join Date: 2/13/13 Recent Posts
Hi Subhasis Roy,

The functionality that you are looking for is available in Liferay.
The way to achieve is as follows :

Goto control panel > Portal > settings > Authentication.

Now make sure following checkboxes are checked
Allow strangers to create accounts?
Require strangers to verify their email address?

This will enable Liferay to send confirmation email.

Make sure you have configured your outgoing email parameters.

Goto control panel > Server > Server Administration > Mail.

Now apply your smtp settings for outgoing mails.

Hope it helps you.

Thanks.
Dharmen Panchal.
thumbnail
Prakash Khanchandani, modified 12 Years ago. Expert Posts: 329 Join Date: 2/10/11 Recent Posts
Now this is what happens to people like me who don't read the User-Guide and don't do their research properly before replying.

Thanks Dharmen Panchal for the enlightenment.
thumbnail
Muradali Hasan, modified 12 Years ago. Junior Member Posts: 84 Join Date: 7/27/10 Recent Posts
Hi Dharmen Panchal,

Yeah you shared the solution seems to nice.
when we select the configuration as you mention the user account has been created with status of inactive or active?



Thanks-
Murad
thumbnail
Subhasis Roy, modified 12 Years ago. Expert Posts: 275 Join Date: 1/20/12 Recent Posts
Dharmen Panchal:
Hi Subhasis Roy,

The functionality that you are looking for is available in Liferay.
The way to achieve is as follows :

Goto control panel > Portal > settings > Authentication.

Now make sure following checkboxes are checked
Allow strangers to create accounts?
Require strangers to verify their email address?

This will enable Liferay to send confirmation email.

Make sure you have configured your outgoing email parameters.

Goto control panel > Server > Server Administration > Mail.

Now apply your smtp settings for outgoing mails.

Hope it helps you.

Thanks.
Dharmen Panchal.




Hi Dharmen.

This approach will not work as I need an ACTIVATION URL in the email body which on click will activate the user in the portal.

I am attaching the screenshot for Portal Setting and the Email I received after registering in the portal. In the mail content there is no activation link for the user account.

Also I have added the following key to portal-ext.properties file

company.security.strangers.verify=true

Can any one please help me on this.
thumbnail
Vilmos Papp, modified 12 Years ago. Liferay Master Posts: 529 Join Date: 10/21/10 Recent Posts
Hi,

I'm not sure how 6.0 works as I usually work with the latest version but one approach could be:

Create a hook plugin for login portlet to modify the registration process.

Create a friendly-url-mapper to be able to create and handle nice URL for activiation like this: http://www.example.com/c/login/confirm_registration?userId=1234
And process the URL with your custom logic implementation. Of course the URL should be added to the email template as well.
thumbnail
Subhasis Roy, modified 12 Years ago. Expert Posts: 275 Join Date: 1/20/12 Recent Posts
Hi,

I am able to do it in Liferay Portal 6.0.5 CE.

Steps followed:

Step 1. I have one registration.jsp file where user is giving input for registering himself. I added the following code to create one actionURL in that jsp.

<portlet:actionurl var="activationLink">
	<portlet:param name="<%= ActionRequest.ACTION_NAME %>" value="activationLink" />
</portlet:actionurl>



Step2: Then I added one hidden field in the aui:form to pass that action url into processAction method.

<aui:form action="<%= registerUser %>" method="post" name="registerUserForm">
         <aui:input name="activationLink" id="activationLink" type="hidden" value="<%=activationLink %>"></aui:input>
</aui:form>


Step 3: Retrieved the activationLink in the processAction method in the UserRegistration.java

String activationLink = ParamUtil.getString(actionRequest, "activationLink");


Step 4: Created the user and set the user status to deactive in my UserRegistration.java

ThemeDisplay themeDisplay = (ThemeDisplay)map.get(Constant.THEME_DISPLAY);
Company company = themeDisplay.getCompany();
ServiceContext serviceContext = (ServiceContext)map.get(Constant.SERVICE_CONTEXT);

try {
		user = UserServiceUtil.addUser(
				 company.getCompanyId(), autoPassword, password1, password2,autoScreenName,screenName,
				 emailAddress, facebookId, openId, themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,suffixId, male,                    
                                 birthdayMonth, birthdayDay, birthdayYear, jobTitle,groupIds, organizationIds, roleIds, userGroupIds, sendEmail,serviceContext);
} catch (Exception e) {
	         e.printStackTrace();
} 

if(user !=null ){
     //Setting user status to deactive
     UserLocalServiceUtil.updateActive(userId, false);
}


Step 5: After creating user I am sending activation link to users. This Activation link is nothing but the action url which we have generated previously (in the jsp). I only appended user email address in that action url. So that when user will click that activation url in the mail, we will get the user email from the parameter.

InternetAddress from = new InternetAddress("sender@gmail.com");
InternetAddress to = new InternetAddress(eMail);
String subject = "User successfully created";

StringBuffer sb = new StringBuffer();
sb.append(activationLink);
sb.append("&amp;eMail=");
sb.append(eMail);

String activateLink = sb.toString();
String mailBody = "<a href="+activateLink+">Click on this link to activate your account</a>";
MailEngine.send(from, to , subject , mailBody);
System.out.println("Email sent");


Sample Activation link in the user mail:
<a href=http://test.mysite.com/registration?p_auth=WLKMjft8&p_p_id=userregistration_WAR_UserRegistrationportlet_INSTANCE_ln7L&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=3&_userregistration_WAR_UserRegistrationportlet_INSTANCE_ln7L_javax.portlet.action=activationLink&eMail=receiver@gmail.com>Click on this link to activate your account</a>


Step 6: Now we have to create one "activationLink" method in the UserRegistration.java . When user will click the activation url in the email. Then this activationLink method will get executed. In this method I have activated the user based on the email id received as parameter from the actionURL.


public void activationLink(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PasswordException {
	System.out.println("in activation link method.....");
	
	String userEmail = (String)actionRequest.getParameter("eMail");
	User userObject=null;
	ThemeDisplay themeDisplayObj = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
	try {
		userObject= UserLocalServiceUtil.getUserByEmailAddress(themeDisplayObj .getCompanyId(), userEmail);
	} catch (PortalException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SystemException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	//Activating user
	if(objUser != null) {			

		try {
			UserLocalServiceUtil.updateActive(userObject.getUserId(), true);
		} catch (PortalException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SystemException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	//Code for taking user to welcome page after activation
	byte[] encoded = Base64.encodeBase64(userEmail.getBytes());
	String encodedEmail = new String(encoded);
	String redirectURL="/web/guest/welcome?x="+encodedEmail;	
	actionResponse.sendRedirect(redirectURL);
}



Some Reference URL:
Reference1
Reference2
Reference3
thumbnail
Vilmos Papp, modified 12 Years ago. Liferay Master Posts: 529 Join Date: 10/21/10 Recent Posts
Hi,

Does it mean that you managed to solve your problem?
thumbnail
Subhasis Roy, modified 12 Years ago. Expert Posts: 275 Join Date: 1/20/12 Recent Posts
Yes Vil... emoticon
thumbnail
Vilmos Papp, modified 12 Years ago. Liferay Master Posts: 529 Join Date: 10/21/10 Recent Posts
Great emoticon I'm happy to hear that!