diff --git a/pom.xml b/pom.xml index 9b4d23b..b1281e5 100644 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,12 @@ home-library provided + + javax.mail + javax.mail-api + 1.4.5 + provided + com.liferay.portal portal-service diff --git a/src/main/java/org/gcube/portlets/user/gcubeloggedin/server/EmailNotification.java b/src/main/java/org/gcube/portlets/user/gcubeloggedin/server/EmailNotification.java new file mode 100644 index 0000000..8f49f3d --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/gcubeloggedin/server/EmailNotification.java @@ -0,0 +1,95 @@ +package org.gcube.portlets.user.gcubeloggedin.server; + +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.apache.log4j.Logger; + +/** + * A class for sending email + * + * @author Panagiota Koltsida, NKUA + * + */ +public class EmailNotification { + + /** + * 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; + + /** Logger */ + private static Logger logger = Logger.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 = sender; + this.emailrecipients = recipients; + this.emailSubject = subject; + this.emailBody = body; + } + + 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); + 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 (Exception 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 sendUserUnregisteredNotification(String username, String scope, String portalbasicurl, String gatewayName) { + ArrayList adminEmails = getAdministratorsEmails(scope); + + User currUser = null; + try { + currUser = UserLocalServiceUtil.getUserByScreenName(OrganizationsUtil.getCompany().getCompanyId(), username); + } catch (Exception e) { + + } + String name = currUser.getFirstName(); + String lastname = currUser.getLastName(); + + 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(""+name + " " + lastname +" has left to the following environment: "); + body.append("

"); + body.append("" + scope+""); + body.append("
"); + body.append("
"); + body.append("Username: " + username); + body.append("
"); + body.append("e-mail: " + currUser.getEmailAddress()); + 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 + "] - unregistration from VRE", body.toString()); + + mailToAdmin.sendEmail(); + } + + + private String readGatewayName() { + //get the portles to look for from the property file + Properties props = new Properties(); + String gatewayLabel = "gCube Gateway"; + + try { + String propertyfile = System.getenv("CATALINA_HOME")+"/conf/gcube-data.properties"; + File propsFile = new File(propertyfile); + FileInputStream fis = new FileInputStream(propsFile); + props.load( fis); + //set the gateway label in the session + gatewayLabel = props.getProperty("portalinstancename"); + } + //catch exception in case properties file does not exist + catch(IOException e) { + _log.warn("$CATALINA_HOME/conf/gcube-data.properties not found, Returning gateway name: " + gatewayLabel); + } + + return gatewayLabel; + } }