added email notification to vre managers when a user leaves a VRE

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/gcube-loggedin@99955 82a268e6-3cf1-43bd-a215-b396298e98cf
master
Massimiliano Assante 10 years ago
parent f455fc70f4
commit 0061735c5a

@ -93,6 +93,12 @@
<artifactId>home-library</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.4.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>portal-service</artifactId>

@ -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<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");
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
logger.error("Failed to send the email message.", e);
}
}
}

@ -3,8 +3,14 @@ package org.gcube.portlets.user.gcubeloggedin.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@ -21,9 +27,13 @@ import org.gcube.portlets.user.gcubeloggedin.shared.VObject.UserBelongingClient;
import org.gcube.portlets.user.gcubeloggedin.shared.VREClient;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -283,6 +293,7 @@ public class LoggedinServiceImpl extends RemoteServiceServlet implements Loggedi
try {
userM.dismissUserFromGroup(getCurrentGroupID(), userM.getUserId(username));
removeUserFromHLGroup(username, getASLSession().getScope());
sendUserUnregisteredNotification(username, getASLSession().getScope(), getPortalBasicUrl(), readGatewayName());
return getDefaultCommunityURL();
} catch (Exception e) {
e.printStackTrace();
@ -323,4 +334,110 @@ public class LoggedinServiceImpl extends RemoteServiceServlet implements Loggedi
}
}
protected 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 (Exception 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 sendUserUnregisteredNotification(String username, String scope, String portalbasicurl, String gatewayName) {
ArrayList<String> 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("<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>"+name + " " + lastname +"</b> has left to the following environment: ");
body.append("<br /><br />");
body.append("<b>" + scope+"</b>");
body.append("<br />");
body.append("<br />");
body.append("<b>Username: </b>" + username);
body.append("<br />");
body.append("<b>e-mail: </b>" + currUser.getEmailAddress());
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 + "] - 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;
}
}

Loading…
Cancel
Save