join-vre/src/main/java/org/gcube/portlets/user/joinvre/server/LoginServiceUtil.java

236 lines
9.5 KiB
Java

package org.gcube.portlets.user.joinvre.server;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gcube.common.portal.PortalContext;
import org.gcube.portal.databook.shared.Invite;
import org.gcube.portlets.user.joinvre.shared.VO;
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.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.GCubeRole;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Massimiliano Assante ISTI-CNR
*
*/
public class LoginServiceUtil {
/**
*
*/
public static final String ROOT_ORG = "rootorganization";
/**
*
*/
public static final String PUBLIC_LAYOUT_NAME = " Data e-Infrastructure gateway";
/**
*
*/
public static final String GUEST_COMMUNITY_NAME = "Guest";
private static Logger _log = LoggerFactory.getLogger(LoginServiceUtil.class);
protected static ArrayList<String> getAdministratorsEmails(String scope) {
LiferayUserManager userManager = new LiferayUserManager();
LiferayGroupManager groupManager = new LiferayGroupManager();
long groupId = -1;
try {
List<GCubeGroup> allGroups = groupManager.listGroups();
_log.debug("Number of groups retrieved: " + allGroups.size());
for (int i = 0; i < allGroups.size(); i++) {
long grId = allGroups.get(i).getGroupId();
String groupScope = groupManager.getInfrastructureScope(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();
}
Map<GCubeUser, List<GCubeRole>> usersAndRoles = null;
try {
usersAndRoles = userManager.listUsersAndRolesByGroup(groupId);
} catch (Exception e) {
e.printStackTrace();
}
Set<GCubeUser> users = usersAndRoles.keySet();
ArrayList<String> adminEmailsList = new ArrayList<String>();
for (GCubeUser usr:users) {
List<GCubeRole> 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 addMembershipRequest(String username, String scope, String optionalMessage,String portalbasicurl, String gatewayName) throws Exception{
_log.info("gatewayName = " + gatewayName + " Message=" + optionalMessage);
ArrayList<String> adminEmails = LoginServiceUtil.getAdministratorsEmails(scope);
UserManager um = new LiferayUserManager();
GCubeUser currUser = um.getUserByUsername(username);
String name = currUser.getFirstName();
String lastname = currUser.getLastName();
String selectedVRE = scope.substring(scope.lastIndexOf("/")+1, scope.length());
_log.info("Requested MEMBERSHIP for: " + selectedVRE + " scope: " + scope);
GroupManager gm = new LiferayGroupManager();
um.requestMembership(currUser.getUserId(), gm.getGroupIdFromInfrastructureScope(scope), optionalMessage);
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 requested access 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.getEmail());
body.append("</p>");
body.append("<p>");
body.append("The request is annotated with the following text: " + optionalMessage);
body.append("</p>");
body.append("<p>");
body.append("You are kindly asked to manage such request by either approving or rejecting it through the user management " +
"facilities available at ");
body.append("<br />" + portalbasicurl);
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(PortalContext.getConfiguration().getSenderEmail(), allMails , "[" + gatewayName + "] - Join Request", body.toString());
mailToAdmin.sendEmail();
}
/**
*
* @param scope .
* @param optionalMessage .
*/
public static void notifyUserSelfRegistration(String username, VO rootVO, String scope, String portalbasicurl, String gatewayName) throws Exception {
ArrayList<String> adminEmails = LoginServiceUtil.getAdministratorsEmails(scope);
LiferayUserManager um = new LiferayUserManager();
GCubeUser currUser = um.getUserByUsername(username);
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 self registered 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.getEmail());
body.append("</p>");
body.append("<p>");
body.append("<br />" + portalbasicurl);
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 + "] - Self Registration", body.toString());
mailToAdmin.sendEmail();
}
/**
*
* @param scope .
* @param optionalMessage .
*/
public static void notifyUserAcceptedInvite(String username, VO rootVO, String scope, String portalbasicurl, String gatewayName, Invite invite) throws Exception {
ArrayList<String> adminEmails = LoginServiceUtil.getAdministratorsEmails(scope);
LiferayUserManager um = new LiferayUserManager();
GCubeUser currUser = um.getUserByUsername(username);
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 accepted an invitation 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.getEmail());
body.append("</p>");
body.append("<p>");
body.append("<b>The invitation was sent by " + invite.getSenderFullName() +" (" + invite.getSenderUserId()+") on " + invite.getTime()+"</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 + "] - Accepted Invitation", body.toString());
mailToAdmin.sendEmail();
}
}