added VRE Manager to receive notification via email when users get invitations #268

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/invites-common-library@115732 82a268e6-3cf1-43bd-a215-b396298e98cf
master
Massimiliano Assante 9 years ago
parent 65a95e666b
commit 785fcc9b05

@ -42,7 +42,16 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.portal</groupId>
<artifactId>custom-portal-handler</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.common.portal</groupId>
<artifactId>portal-manager</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
@ -63,6 +72,11 @@
<artifactId>javax.mail</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.dvos</groupId>
<artifactId>usermanagement-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>portal-service</artifactId>

@ -0,0 +1,156 @@
package org.gcube.portal.invites;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.gcube.portal.custom.communitymanager.OrganizationsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A class for sending email
*
* @author M. Assante
*
*/
public class EmailNotification {
private static final String SENDER_EMAIL = "notificationSenderEmail";
private static final String GATEWAY_NAME = "portalinstancename";
/**
* The sender of the email
*/
private String emailSender;
/**
* The recipients of the email
*/
private String emailrecipients[];
/**
* Email's subject
*/
private String emailSubject;
/**
* Email's body message
*/
private String emailBody;
private String portalName;
/** Logger */
private static Logger _log = LoggerFactory.getLogger(EmailNotification.class);
/**
* Class's constructor
*
* @param sender
* @param recipients
* @param subject
* @param body
*/
public EmailNotification(String sender, String recipients[], String subject, String body) {
this.emailSender = getNotificationSenderEmail();
this.emailrecipients = recipients;
this.emailSubject = subject;
this.emailBody = body;
this.portalName = getPortalInstanceName();
}
public void sendEmail() {
Properties props = System.getProperties();
String mailServiceHost = "localhost";
props.put("mail.smtp.host", mailServiceHost);
String mailServicePort = "25";
props.put("mail.smtp.port", mailServicePort);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Message mimeMessage = new MimeMessage(session);
try {
// EMAIL SENDER
Address from = new InternetAddress(emailSender, portalName);
mimeMessage.setHeader("Content-Type", "text/html; charset=UTF-8");
mimeMessage.setFrom(from);
// EMAIL RECIPIENTS
for (int i=0; i<emailrecipients.length; i++) {
Address address = new InternetAddress(emailrecipients[i]);
mimeMessage.addRecipient(Message.RecipientType.TO, address);
}
mimeMessage.setSubject(emailSubject);
// mimeMessage.setText(emailBody);
mimeMessage.setContent(emailBody, "text/html; charset=UTF-8");
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
_log.error("Failed to send the email message.", e);
}
}
/**
* read the portal instance name from a property file and returns it
*/
protected static String getPortalInstanceName() {
//get the portles to look for from the property file
Properties props = new Properties();
String toReturn = "";
try {
String propertyfile = OrganizationsUtil.getTomcatFolder()+"conf/gcube-data.properties";
File propsFile = new File(propertyfile);
FileInputStream fis = new FileInputStream(propsFile);
props.load( fis);
toReturn = props.getProperty(GATEWAY_NAME);
}
//catch exception in case properties file does not exist
catch(IOException e) {
toReturn = "Gateway support";
_log.error("gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default Portal Name " + toReturn);
return toReturn;
}
_log.debug("Returning Gateway Name: " + toReturn );
return toReturn;
}
/**
* read the sender email for notifications name from a property file and returns it
*/
private static String getNotificationSenderEmail() {
//get the portles to look for from the property file
Properties props = new Properties();
String toReturn = "";
try {
String propertyfile = OrganizationsUtil.getTomcatFolder()+"conf/gcube-data.properties";
File propsFile = new File(propertyfile);
FileInputStream fis = new FileInputStream(propsFile);
props.load( fis);
toReturn = props.getProperty(SENDER_EMAIL);
}
//catch exception in case properties file does not exist
catch(IOException e) {
toReturn = "do-not-reply@d4science.org";
_log.error("gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default Email" + toReturn);
return toReturn;
}
_log.debug("Returning SENDER_EMAIL: " + toReturn );
return toReturn;
}
}

@ -1,7 +1,11 @@
package org.gcube.portal.invites;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import javax.mail.Message;
@ -17,6 +21,12 @@ import org.gcube.portal.databook.server.DatabookStore;
import org.gcube.portal.databook.shared.Invite;
import org.gcube.portal.databook.shared.InviteOperationResult;
import org.gcube.portal.databook.shared.InviteStatus;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.RoleModel;
import org.gcube.vomanagement.usermanagement.model.UserModel;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
@ -75,6 +85,7 @@ public class InvitesManager {
try {
result = store.saveInvite(invite);
emailResult = sendInviteEmail(aslSession, portalSenderEmail, portalURL, name, lastName, email, vreDescription);
notifyInviteSent(username, aslSession.getScopeName(), portalURL, EmailNotification.getPortalInstanceName(), invite);
} catch (AddressException e) {
_log.error("Email not valid " + e.getMessage());
@ -222,4 +233,79 @@ public class InvitesManager {
return buffer;
}
private static ArrayList<String> getAdministratorsEmails(String scope) {
LiferayUserManager userManager = new LiferayUserManager();
LiferayGroupManager groupManager = new LiferayGroupManager();
String groupId = null;
try {
List<org.gcube.vomanagement.usermanagement.model.GroupModel> allGroups = groupManager.listGroups();
_log.debug("Number of groups retrieved: " + allGroups.size());
for (int i = 0; i < allGroups.size(); i++) {
String grId = allGroups.get(i).getGroupId();
String groupScope = groupManager.getScope(grId);
System.out.println("Comparing: " + groupScope + " " + scope);
if (groupScope.equals(scope)) {
groupId = allGroups.get(i).getGroupId();
break;
}
}
} catch (UserManagementSystemException e) {
e.printStackTrace();
} catch (GroupRetrievalFault e) {
e.printStackTrace();
}
HashMap<UserModel, List<RoleModel>> usersAndRoles = null;
try {
usersAndRoles = userManager.listUsersAndRolesByGroup(groupId);
} catch (Exception e) {
e.printStackTrace();
}
Set<UserModel> users = usersAndRoles.keySet();
ArrayList<String> adminEmailsList = new ArrayList<String>();
for (UserModel usr:users) {
List<RoleModel> roles = usersAndRoles.get(usr);
for (int i = 0; i < roles.size(); i++) {
if (roles.get(i).getRoleName().equals("VO-Admin") || roles.get(i).getRoleName().equals("VRE-Manager")) {
adminEmailsList.add(usr.getEmail());
_log.debug("Admin: " + usr.getFullname());
break;
}
}
}
return adminEmailsList;
}
/**
*
* @param scope .
* @param optionalMessage .
*/
public static void notifyInviteSent(String username, String scope, String portalbasicurl, String gatewayName, Invite invite) {
ArrayList<String> adminEmails = getAdministratorsEmails(scope);
StringBuffer body = new StringBuffer();
body.append("<p>Dear manager of "+ scope +",<br />this email message was automatically generated by " + portalbasicurl +" to inform you that ");
body.append("</p>");
body.append("<p>");
body.append("<b>"+invite.getSenderFullName() + "(" + invite.getSenderUserId() +")</b> has invited " + invite.getInvitedEmail() + " to the following environment:");
body.append("<br /><br />");
body.append("<b>" + scope+"</b>");
body.append("</p>");
body.append("<p>");
body.append("WARNING / LEGAL TEXT: This message is intended only for the use of the individual or entity to which it is addressed and may contain"+
" information which is privileged, confidential, proprietary, or exempt from disclosure under applicable law. " +
"If you are not the intended recipient or the person responsible for delivering the message to the intended recipient, you are strictly prohibited from disclosing, distributing, copying, or in any way using this message.");
body.append("</p>");
String[] allMails = new String[adminEmails.size()];
adminEmails.toArray(allMails);
EmailNotification mailToAdmin = new EmailNotification("no-reply@d4science.org", allMails , "[" + gatewayName + "] - Sent Invitation", body.toString());
mailToAdmin.sendEmail();
}
}

Loading…
Cancel
Save