accept-invite-portlet/src/main/java/org/gcube/portlets/user/acceptinvite/PortletViewController.java

214 lines
8.2 KiB
Java

/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library 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 library 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.
*/
package org.gcube.portlets.user.acceptinvite;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.codec.binary.Base64;
import org.gcube.common.portal.PortalContext;
import org.gcube.social_networking.social_networking_client_library.LibClient;
import org.gcube.social_networking.socialnetworking.model.shared.Invite;
import org.gcube.social_networking.socialnetworking.model.shared.InviteStatus;
import org.gcube.social_networking.socialnetworking.model.shared.exceptions.InviteIDNotFoundException;
import org.gcube.social_networking.socialnetworking.model.shared.exceptions.InviteStatusNotFoundException;
import org.gcube.portal.invites.InvitesManager;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.model.Company;
import com.liferay.portal.model.Group;
import com.liferay.portal.service.CompanyLocalServiceUtil;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
@Controller(value = "PortletViewController")
@RequestMapping("VIEW")
public class PortletViewController {
private static Log _log = LogFactoryUtil.getLog(PortletViewController.class);
public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
public static final String DEFAULT_COMPANY_WEB_ID = "liferay.com";
private static String PAGE_NOT_AUTHORIZED = "not-authorized";
private static String PAGE_INVITE_NOTFOUND = "invite-notfound";
private static String PAGE_VRE_NOTFOUND = "vre-notfound";
private static String PAGE_INVITE_EXPIRED = "invite-expired";
private static String PAGE_INVITE_PROCESS = "view";
public static String INVITE_INSTANCE = "inviteInstance";
private static String MODEL_ATTR = "theModel";
private static LibClient libClient;
/**
*
* @return the unique instance of the store
*/
public static synchronized LibClient getClient() {
if (libClient == null) {
try {
libClient = new LibClient();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return libClient;
}
@RenderMapping
public String handleRenderRequest(RenderRequest request,RenderResponse response, Model model) {
HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));
final String INVITE_ID_ENCODED = new String(Base64.encodeBase64(InvitesManager.INVITEID_ATTR.getBytes()));
final String SITE_ID_ENCODED = new String(Base64.encodeBase64(InvitesManager.SITEID_ATTR.getBytes()));
if (httpReq.getParameter(INVITE_ID_ENCODED) == null || SITE_ID_ENCODED == null)
return PAGE_NOT_AUTHORIZED;
String inviteIdEncoded = (String) httpReq.getParameter(INVITE_ID_ENCODED);
String siteIdEncoded = (String) httpReq.getParameter(SITE_ID_ENCODED);
String inviteId= new String(Base64.decodeBase64(inviteIdEncoded));
String groupId = new String(Base64.decodeBase64(siteIdEncoded));
_log.info("GOT inviteId=" + inviteId);
_log.info("siteId=" + groupId);
Group site = null;
try {
site = GroupLocalServiceUtil.getGroup(Long.parseLong(groupId));
} catch (Exception e1) {
e1.printStackTrace();
return PAGE_VRE_NOTFOUND;
}
Invite invite = null;
invite = getClient().readInviteLib(inviteId);
if (invite.getStatus() == InviteStatus.ACCEPTED)
return PAGE_INVITE_EXPIRED;
GCubeUser invitedUser = null;
try {
invitedUser = new LiferayUserManager().getUserByEmail(invite.getInvitedEmail());
model.addAttribute("invitedUser", invitedUser);
} catch (UserManagementSystemException | UserRetrievalFault e) {
_log.info("No user account exist with this email: " + invite.getInvitedEmail());
}
//we set the invite instance retrieved in the model
model.addAttribute(INVITE_INSTANCE, invite);
model.addAttribute("vreName", site.getName());
model.addAttribute("vreFriendlyURL", site.getFriendlyURL());
model.addAttribute("groupId", site.getGroupId());
model.addAttribute("landingPage", PortalContext.getConfiguration().getSiteLandingPagePath(httpReq));
HttpSession session = httpReq.getSession();
session.setAttribute(MODEL_ATTR, model);
return PAGE_INVITE_PROCESS;
}
@ResourceMapping(value="createAccount")
public void createAccountForUser(ResourceRequest request, ResourceResponse response) throws Exception {
String email = ParamUtil.getString(request, "email");
String firstName = ParamUtil.getString(request, "firstname");
String lastName = ParamUtil.getString(request, "lastname");
//
_log.info("firstName=" + firstName);
_log.info("lastName=" + lastName);
_log.info("email=" + email);
HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));
Model model = (Model) httpReq.getSession().getAttribute(MODEL_ATTR);
Invite invite = (Invite) model.asMap().get(INVITE_INSTANCE);
if (invite.getInvitedEmail().compareTo(email) != 0) {
response.getWriter().println("The email address invited does not match or is empty.");
return;
}
//check the fields before creating account
if (firstName == null
|| firstName.equals("")
|| lastName == null
|| lastName.equals("")
|| email.equals("")) {
response.getWriter().println("Not all the required fields have been filled.");
return;
}
if (!validate(email)) {
response.getWriter().println("The email address invited does not look like a valid email address.");
return;
}
_log.debug("trying to send the event to create the account for this user to the orchestrator");
InvitationAcceptedEvent toSend = new InvitationAcceptedEvent(firstName, lastName, email, invite.getControlCode());
new InvitationAcceptEventPublisher().publish(toSend);
_log.info("Done send event " + toSend.getName() + " for " + toSend.getUser());
Thread.sleep(1000);
response.getWriter().println("OK");
return;
}
private static boolean validate(String emailStr) {
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(emailStr);
return matcher.find();
}
public static Company getCompany() throws PortalException, SystemException {
return CompanyLocalServiceUtil.getCompanyByWebId(getDefaultCompanyWebId());
}
/**
*
* @return the default company web-id (e.g. iMarine.eu)
*/
public static String getDefaultCompanyWebId() {
String defaultWebId = "";
try {
defaultWebId = GetterUtil.getString(PropsUtil.get("company.default.web.id"));
}
catch (NullPointerException e) {
_log.error("Cound not find property company.default.web.id in portal.ext file returning default web id: " + DEFAULT_COMPANY_WEB_ID);
return DEFAULT_COMPANY_WEB_ID;
}
return defaultWebId;
}
}