diff --git a/pom.xml b/pom.xml index df94d45..a5197ec 100644 --- a/pom.xml +++ b/pom.xml @@ -42,7 +42,16 @@ - + + org.gcube.portal + custom-portal-handler + provided + + + org.gcube.common.portal + portal-manager + provided + org.jsoup jsoup @@ -63,6 +72,11 @@ javax.mail provided + + org.gcube.dvos + usermanagement-core + provided + com.liferay.portal portal-service diff --git a/src/main/java/org/gcube/portal/invites/EmailNotification.java b/src/main/java/org/gcube/portal/invites/EmailNotification.java new file mode 100644 index 0000000..7e2dcbb --- /dev/null +++ b/src/main/java/org/gcube/portal/invites/EmailNotification.java @@ -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 getAdministratorsEmails(String scope) { + LiferayUserManager userManager = new LiferayUserManager(); + LiferayGroupManager groupManager = new LiferayGroupManager(); + String groupId = null; + try { + List 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> usersAndRoles = null; + try { + usersAndRoles = userManager.listUsersAndRolesByGroup(groupId); + } catch (Exception e) { + e.printStackTrace(); + } + Set users = usersAndRoles.keySet(); + ArrayList adminEmailsList = new ArrayList(); + for (UserModel usr:users) { + List 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 adminEmails = getAdministratorsEmails(scope); + + StringBuffer body = new StringBuffer(); + body.append("

Dear manager of "+ scope +",
this email message was automatically generated by " + portalbasicurl +" to inform you that "); + body.append("

"); + body.append("

"); + body.append(""+invite.getSenderFullName() + "(" + invite.getSenderUserId() +") has invited " + invite.getInvitedEmail() + " to the following environment:"); + body.append("

"); + body.append("" + scope+""); + body.append("

"); + body.append("

"); + 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("

"); + + 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(); + } + }