From 12cd60454ffe9e6f0ea0d641ffc94027f0ff7110 Mon Sep 17 00:00:00 2001
From: Massimiliano Assante
Date: Tue, 5 Jul 2016 14:58:28 +0000
Subject: [PATCH] added support for new user account and new user site
registration emails
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/notifications-common-library@129944 82a268e6-3cf1-43bd-a215-b396298e98cf
---
pom.xml | 6 +
.../NewUserAccountNotificationThread.java | 109 ++++++++++++++++++
...serSiteRegistrationNotificationThread.java | 106 +++++++++++++++++
3 files changed, 221 insertions(+)
create mode 100644 src/main/java/org/gcube/portal/notifications/thread/NewUserAccountNotificationThread.java
create mode 100644 src/main/java/org/gcube/portal/notifications/thread/NewUserSiteRegistrationNotificationThread.java
diff --git a/pom.xml b/pom.xml
index 84f7a36..add7913 100644
--- a/pom.xml
+++ b/pom.xml
@@ -100,6 +100,12 @@
portlet-api
provided
+
+ javax.servlet
+ servlet-api
+ 2.4
+ provided
+
org.slf4j
slf4j-log4j12
diff --git a/src/main/java/org/gcube/portal/notifications/thread/NewUserAccountNotificationThread.java b/src/main/java/org/gcube/portal/notifications/thread/NewUserAccountNotificationThread.java
new file mode 100644
index 0000000..0b7213f
--- /dev/null
+++ b/src/main/java/org/gcube/portal/notifications/thread/NewUserAccountNotificationThread.java
@@ -0,0 +1,109 @@
+package org.gcube.portal.notifications.thread;
+
+import java.util.List;
+
+import org.gcube.common.portal.PortalContext;
+import org.gcube.common.portal.mailing.EmailNotification;
+import org.gcube.vomanagement.usermanagement.GroupManager;
+import org.gcube.vomanagement.usermanagement.RoleManager;
+import org.gcube.vomanagement.usermanagement.UserManager;
+import org.gcube.vomanagement.usermanagement.exception.RoleRetrievalFault;
+import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
+import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
+import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
+import org.gcube.vomanagement.usermanagement.model.GCubeUser;
+import org.gcube.vomanagement.usermanagement.model.GatewayRolesNames;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.liferay.portal.util.PortalUtil;
+
+/**
+ *
+ * @author Massimiliano Assante ISTI-CNR
+ *
+ */
+public class NewUserAccountNotificationThread implements Runnable {
+ private static Logger _log = LoggerFactory.getLogger(NewUserAccountNotificationThread.class);
+
+ final String SUBJECT = "New user account notification";
+
+ private String newUserUserName;
+ private String newUserFullName;
+ private String newUserEmailAddress;
+
+
+ public NewUserAccountNotificationThread(String newUserUserName, String newUserFullName, String newUserEmailAddress) {
+ super();
+ this.newUserUserName = newUserUserName;
+ this.newUserFullName = newUserFullName;
+ this.newUserEmailAddress = newUserEmailAddress;
+ }
+
+ @Override
+ public void run() {
+ handleUserRegistration(newUserUserName, newUserFullName, newUserEmailAddress);
+ }
+
+ private void handleUserRegistration(String newUserUserName, String newUserFullName, String newUserEmailAddress) {
+ UserManager um = new LiferayUserManager();
+ GroupManager gm = new LiferayGroupManager();
+ RoleManager rm = new LiferayRoleManager();
+ try {
+ System.out.println("addUser hook ON");
+ String rootVoName = PortalContext.getConfiguration().getInfrastructureName();
+ long groupId = gm.getGroupIdFromInfrastructureScope("/"+rootVoName);
+ long infraManagerRoleId = -1;
+ try {
+ infraManagerRoleId = rm.getRoleIdByName(GatewayRolesNames.INFRASTRUCTURE_MANAGER.getRoleName());
+ }
+ catch (RoleRetrievalFault e) {
+ _log.warn("There is no (Site) Role " + infraManagerRoleId + " in this portal. Will not notify about newly user accounts.");
+ return;
+ }
+ _log.trace("Root is: " + rootVoName + " Scanning roles ....");
+
+ List managers = um.listUsersByGroupAndRole(groupId, infraManagerRoleId);
+ if (managers == null || managers.isEmpty()) {
+ _log.warn("There are no users with (Site) Role " + infraManagerRoleId + " on " + rootVoName + " in this portal. Will not notify about newly user accounts.");
+ }
+ else {
+ for (GCubeUser manager : managers) {
+ sendNotification(manager, newUserUserName, newUserFullName, newUserEmailAddress);
+ _log.info("sent email to manager: " + manager.getEmail());
+ }
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void sendNotification(GCubeUser manager, String newUserUserName, String newUserFullName, String newUserEmailAddress) {
+ EmailNotification toSend = new EmailNotification(manager.getEmail(), SUBJECT,
+ getHTMLEmail(manager.getFirstName(), newUserUserName, newUserFullName, newUserEmailAddress), null);
+ toSend.sendEmail();
+ }
+
+ private static String getHTMLEmail(String userFirstName, String newUserUserName, String newUserFullName, String newUserEmailAddress) {
+ String sender = newUserFullName + " ("+newUserUserName+") ";
+
+ StringBuilder body = new StringBuilder();
+
+ body.append("
")
+ .append("")
+ .append("Dear ").append(userFirstName).append(",") //dear
+ .append("").append(sender).append(" ").append("registered to the portal with the following email: ") // has done something
+ .append(newUserEmailAddress)
+ .append("
")
+ .append("You received this email because you are an Infrastructure Manager in this portal
")
+ .append("
")
+ .append("");
+
+ return body.toString();
+
+ }
+
+
+
+}
diff --git a/src/main/java/org/gcube/portal/notifications/thread/NewUserSiteRegistrationNotificationThread.java b/src/main/java/org/gcube/portal/notifications/thread/NewUserSiteRegistrationNotificationThread.java
new file mode 100644
index 0000000..81fc415
--- /dev/null
+++ b/src/main/java/org/gcube/portal/notifications/thread/NewUserSiteRegistrationNotificationThread.java
@@ -0,0 +1,106 @@
+package org.gcube.portal.notifications.thread;
+
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.gcube.common.portal.PortalContext;
+import org.gcube.common.portal.mailing.EmailNotification;
+import org.gcube.portal.custom.communitymanager.SiteManagerUtil;
+import org.gcube.vomanagement.usermanagement.RoleManager;
+import org.gcube.vomanagement.usermanagement.UserManager;
+import org.gcube.vomanagement.usermanagement.exception.RoleRetrievalFault;
+import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
+import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
+import org.gcube.vomanagement.usermanagement.model.GCubeUser;
+import org.gcube.vomanagement.usermanagement.model.GatewayRolesNames;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.liferay.portal.model.Group;
+import com.liferay.portal.model.User;
+import com.liferay.portal.util.PortalUtil;
+import com.liferay.portlet.sites.util.SitesUtil;
+
+/**
+ *
+ * @author Massimiliano Assante ISTI-CNR
+ *
+ */
+public class NewUserSiteRegistrationNotificationThread implements Runnable {
+ private static Logger _log = LoggerFactory.getLogger(NewUserSiteRegistrationNotificationThread.class);
+
+ final String SUBJECT = "New User registration to site notification";
+
+ private User user;
+ private Group site;
+ private String siteURL;
+
+ public NewUserSiteRegistrationNotificationThread(User user, Group site, String siteURL) {
+ super();
+ this.user = user;
+ this.site = site;
+ this.siteURL = siteURL;
+ }
+
+ @Override
+ public void run() {
+ handleUserToSiteRegistration(user, site, siteURL);
+ }
+
+ private void handleUserToSiteRegistration(final User user, final Group site, String siteURL) {
+ UserManager um = new LiferayUserManager();
+ RoleManager rm = new LiferayRoleManager();
+ try {
+ long groupId = site.getGroupId();
+ long infraManagerRoleId = -1;
+ try {
+ infraManagerRoleId = rm.getRoleIdByName(GatewayRolesNames.INFRASTRUCTURE_MANAGER.getRoleName());
+ }
+ catch (RoleRetrievalFault e) {
+ _log.warn("There is no (Site) Role " + GatewayRolesNames.INFRASTRUCTURE_MANAGER.getRoleName() + " in this Portal. Will not notify about newly user accounts.");
+ return;
+ }
+ List managers = um.listUsersByGroupAndRole(groupId, infraManagerRoleId);
+ if (managers == null || managers.isEmpty()) {
+ _log.warn("There are no users with (Site) Role " + GatewayRolesNames.INFRASTRUCTURE_MANAGER.getRoleName() + " on Site " + site.getName() + ". Will not notify about newly user accounts.");
+ }
+ else {
+ for (GCubeUser manager : managers) {
+ sendNotification(manager, user, site, siteURL);
+ _log.info("sent email to manager: " + manager.getEmail());
+ }
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void sendNotification(GCubeUser manager, User user, Group site, String siteURL) {
+ EmailNotification toSend = new EmailNotification(manager.getEmail(), SUBJECT,
+ getHTMLEmail(manager.getFirstName(), user.getScreenName(), user.getFullName(), user.getEmailAddress(), site, siteURL), null);
+ toSend.sendEmail();
+ }
+
+ private static String getHTMLEmail(String userFirstName, String newUserUserName, String newUserFullName, String newUserEmailAddress, Group site, String siteURL) {
+ String sender = newUserFullName + " ("+newUserUserName+") ";
+
+ StringBuilder body = new StringBuilder();
+
+ body.append("
")
+ .append("")
+ .append("Dear ").append(userFirstName).append(",") //dear
+ .append("").append(sender).append(" ").append("registered to the site " + site.getName() + " with the following email: ") // has done something
+ .append(newUserEmailAddress)
+ .append("
You received this email because you are a Manager in this Site: ").append(siteURL).append("
")
+ .append(" ")
+ .append("");
+
+ return body.toString();
+
+ }
+
+
+
+}