2013-04-10 18:32:02 +02:00
package org.gcube.portlets.user.gcubeloggedin.server ;
2014-09-16 18:33:11 +02:00
import java.util.ArrayList ;
import java.util.List ;
2016-03-11 16:36:08 +01:00
import java.util.Map ;
2014-09-16 18:33:11 +02:00
import java.util.Set ;
2013-04-10 18:32:02 +02:00
import org.gcube.application.framework.core.session.ASLSession ;
import org.gcube.application.framework.core.session.SessionManager ;
2016-03-11 16:36:08 +01:00
import org.gcube.common.portal.PortalContext ;
2016-04-26 16:26:45 +02:00
import org.gcube.common.portal.mailing.EmailNotification ;
2013-04-10 18:32:02 +02:00
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper ;
import org.gcube.portlets.user.gcubeloggedin.client.LoggedinService ;
import org.gcube.portlets.user.gcubeloggedin.shared.VObject ;
import org.gcube.portlets.user.gcubeloggedin.shared.VObject.UserBelongingClient ;
import org.gcube.portlets.user.gcubeloggedin.shared.VREClient ;
2014-12-11 18:38:17 +01:00
import org.gcube.portlets.user.gcubewidgets.server.ScopeServiceImpl ;
2014-09-15 16:45:05 +02:00
import org.gcube.vomanagement.usermanagement.GroupManager ;
import org.gcube.vomanagement.usermanagement.UserManager ;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException ;
2016-03-11 16:36:08 +01:00
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 ;
2013-09-26 16:32:37 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2013-04-10 18:32:02 +02:00
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
2014-09-15 19:02:39 +02:00
import com.liferay.portal.service.UserLocalServiceUtil ;
2013-04-10 18:32:02 +02:00
/ * *
* The server side implementation of the RPC service .
* /
@SuppressWarnings ( " serial " )
public class LoggedinServiceImpl extends RemoteServiceServlet implements LoggedinService {
2013-09-26 16:32:37 +02:00
private static final Logger _log = LoggerFactory . getLogger ( LoggedinServiceImpl . class ) ;
2014-09-15 19:02:39 +02:00
2013-04-10 18:32:02 +02:00
/ * *
* the current ASLSession
* @return .
* /
private ASLSession getASLSession ( ) {
String sessionID = this . getThreadLocalRequest ( ) . getSession ( ) . getId ( ) ;
String user = ( String ) this . getThreadLocalRequest ( ) . getSession ( ) . getAttribute ( ScopeHelper . USERNAME_ATTRIBUTE ) ;
if ( user = = null ) {
_log . warn ( " LOGGEDIN PORTLET: USER IS NULL \ n \ n SESSION ID READ: " + sessionID ) ;
user = " test.user " ;
_log . warn ( " session ID= * " + sessionID + " * user= * " + user + " * " ) ;
2014-12-11 18:38:17 +01:00
2013-04-10 18:32:02 +02:00
}
return SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) ;
}
2014-09-15 19:02:39 +02:00
/ * *
*
* @return true if you ' re running into the portal , false if in development
* /
private boolean isWithinPortal ( ) {
try {
UserLocalServiceUtil . getService ( ) ;
return true ;
}
catch ( com . liferay . portal . kernel . bean . BeanLocatorException ex ) {
_log . trace ( " Development Mode ON " ) ;
return false ;
}
}
2013-04-10 18:32:02 +02:00
/ * *
* return the current selected VRE
* /
2014-12-11 18:38:17 +01:00
public VObject getSelectedRE ( String portalURL ) {
2013-04-10 18:32:02 +02:00
ASLSession aslSession = getASLSession ( ) ;
2014-12-11 18:38:17 +01:00
String friendlyURL = ScopeServiceImpl . extractOrgFriendlyURL ( portalURL ) ;
if ( friendlyURL = = null ) { //the URL is not a portal URL, we are in devmode.
2016-03-23 16:23:47 +01:00
return new VREClient ( " Test VRE Name " , " " , " " +
" Fishery and Aquaculture Resources Management (FARM) Virtual Organisation</b> The FARM Virtual Organisation is the <b><i>dynamic group of individuals</i></b> and/or <b><i>institutions</i></b> defined around a set of <b><i>sharing rules</i></b> in which <b><i>resource providers</i></b> and <b><i>consumers</i></b> specify clearly and carefully just what is shared, who is allowed to share, and the conditions under which sharing occurs to serve the needs of the <b><i>Fisheries and Aquaculture Resources Management</i></b>. "
+ " This VO is conceived to support various application scenarios arising in the FARM Community including the production of Fisheries and Aquaculture Country Profiles, "
+ " the management of catch statistics including harmonisation, the dynamic generation of biodiversity maps and species distribution maps. "
+ " This Virtual Organisation currently consists of:<ul> <li> approximately <b><i>13 gCube nodes</i></b>, "
+ " i.e. machines dedicated to run the gCube system;</li> "
+ " <li> approximately <b><i>89 running instances</i></b>, i.e. running gCube services supporting the operation of the infrastructure;</li> "
+ " <li> approximately <b><i>25 collections</i></b>, i.e. set of D4Science Information Objects including Earth images, AquaMaps, Graphs on catch statistics;</li> "
+ " <li> approximately <b><i>66 metadata collections</i></b>, i.e. set of Metadata Objects describing the Information Objects through various features and schemas;</li> "
+ " <li> approximately <b><i>58 other resources</i></b> including transformation programs, index types, etc.</li></ul></div> "
, " http://placehold.it/300x200 " , " " , UserBelongingClient . BELONGING , false , true ) ;
2013-04-10 18:32:02 +02:00
}
_log . trace ( " getting Selected Research Environment " ) ;
2016-03-11 16:36:08 +01:00
GroupManager gm = new LiferayGroupManager ( ) ;
GCubeGroup currSite = null ;
try {
List < GCubeGroup > groups = gm . listGroups ( ) ;
for ( GCubeGroup g : groups ) {
if ( g . getFriendlyURL ( ) . compareTo ( friendlyURL ) = = 0 ) {
long groupId = g . getGroupId ( ) ;
String scopeToSet = gm . getInfrastructureScope ( groupId ) ;
getASLSession ( ) . setScope ( scopeToSet ) ;
_log . info ( " GOT Selected Research Environment: " + scopeToSet ) ;
currSite = g ;
}
2014-12-11 18:38:17 +01:00
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2013-04-10 18:32:02 +02:00
/ * *
* set the current ORG bean in session
* /
2016-03-11 16:36:08 +01:00
aslSession . setAttribute ( ScopeHelper . CURR_ORG , currSite ) ;
2013-04-10 18:32:02 +02:00
2016-03-11 16:36:08 +01:00
aslSession . setGroupModelInfos ( currSite . getGroupName ( ) , currSite . getGroupId ( ) ) ;
2013-04-10 18:32:02 +02:00
2016-03-11 16:36:08 +01:00
_log . trace ( " CURRENT ORG SET IN SESSION: " + currSite . getGroupName ( ) ) ;
2013-04-10 18:32:02 +02:00
2016-03-11 16:36:08 +01:00
String name = currSite . getGroupName ( ) ;
String logoURL = " /image/layout_set_logo?img_id= " + currSite . getLogoId ( ) ;
2013-04-10 18:32:02 +02:00
String desc = " " ;
//set the description for the vre
2016-03-11 16:36:08 +01:00
if ( currSite . getDescription ( ) ! = null )
desc = currSite . getDescription ( ) ;
2016-03-23 12:44:50 +01:00
2016-04-26 16:26:45 +02:00
System . out . println ( " **** debuggaaa " ) ;
Boolean isRequestBasedGroup = false ;
2016-03-23 12:44:50 +01:00
Boolean isMandatory = false ;
2014-09-15 19:02:39 +02:00
try {
2016-04-13 18:09:55 +02:00
isMandatory = ( Boolean ) gm . readCustomAttr ( currSite . getGroupId ( ) , org . gcube . vomanagement . usermanagement . model . CustomAttributeKeys . MANDATORY . getKeyName ( ) ) ;
2016-04-26 16:26:45 +02:00
isRequestBasedGroup = currSite . isRequestBasedGroup ( ) ;
2016-03-23 12:44:50 +01:00
} catch ( Exception e ) {
2016-04-26 16:26:45 +02:00
isMandatory = false ;
isRequestBasedGroup = false ;
return new VREClient ( name , " " , desc , logoURL , " " , UserBelongingClient . BELONGING , isMandatory , isRequestBasedGroup ) ;
2014-09-15 19:02:39 +02:00
}
2016-04-26 16:26:45 +02:00
VREClient vre = new VREClient ( name , " " , desc , logoURL , " " , UserBelongingClient . BELONGING , isMandatory , isRequestBasedGroup ) ;
2016-03-23 12:44:50 +01:00
return vre ;
2014-09-15 19:02:39 +02:00
}
2014-09-15 16:45:05 +02:00
/ * *
* @return the redirect url if everything goes ok , null otherwise
* /
@Override
public String removeUserFromVRE ( ) {
String username = getASLSession ( ) . getUsername ( ) ;
if ( username . compareTo ( " test.user " ) = = 0 )
return null ;
_log . debug ( " Going to remove user from the current Group: " + getCurrentGroupID ( ) + " . Username is: " + username ) ;
UserManager userM = new LiferayUserManager ( ) ;
try {
userM . dismissUserFromGroup ( getCurrentGroupID ( ) , userM . getUserId ( username ) ) ;
2016-03-29 15:07:50 +02:00
sendUserUnregisteredNotification ( username , getASLSession ( ) . getScope ( ) , PortalContext . getConfiguration ( ) . getGatewayURL ( getThreadLocalRequest ( ) ) , PortalContext . getConfiguration ( ) . getGatewayName ( ) ) ;
2016-03-11 16:36:08 +01:00
return " / " ;
2014-09-15 16:45:05 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return null ;
}
}
2014-12-11 18:38:17 +01:00
2014-09-15 16:45:05 +02:00
/ * *
* Get the current group ID
*
* @return the current group ID or null if an exception is thrown
* @throws Exception
* /
2016-03-11 16:36:08 +01:00
private long getCurrentGroupID ( ) {
2014-09-15 16:45:05 +02:00
ASLSession session = getASLSession ( ) ;
_log . debug ( " The current group NAME is --> " + session . getGroupName ( ) ) ;
try {
try {
GroupManager groupM = new LiferayGroupManager ( ) ;
return groupM . getGroupId ( session . getGroupName ( ) ) ;
} catch ( UserManagementSystemException e ) {
throw new Exception ( e . getMessage ( ) , e . getCause ( ) ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2016-03-11 16:36:08 +01:00
return - 1 ;
2014-09-15 16:45:05 +02:00
}
2014-12-11 18:38:17 +01:00
2016-03-23 12:44:50 +01:00
2014-12-11 18:38:17 +01:00
2014-09-16 18:33:11 +02:00
protected static ArrayList < String > getAdministratorsEmails ( String scope ) {
LiferayUserManager userManager = new LiferayUserManager ( ) ;
LiferayGroupManager groupManager = new LiferayGroupManager ( ) ;
2016-03-11 16:36:08 +01:00
long groupId = - 1 ;
2014-09-16 18:33:11 +02:00
try {
2016-03-11 16:36:08 +01:00
List < GCubeGroup > allGroups = groupManager . listGroups ( ) ;
2014-09-16 18:33:11 +02:00
_log . debug ( " Number of groups retrieved: " + allGroups . size ( ) ) ;
for ( int i = 0 ; i < allGroups . size ( ) ; i + + ) {
2016-03-11 16:36:08 +01:00
long grId = allGroups . get ( i ) . getGroupId ( ) ;
2014-09-16 18:33:11 +02:00
String groupScope = groupManager . getScope ( grId ) ;
2016-03-11 16:36:08 +01:00
_log . debug ( " Comparing: " + groupScope + " " + scope ) ;
2014-09-16 18:33:11 +02:00
if ( groupScope . equals ( scope ) ) {
groupId = allGroups . get ( i ) . getGroupId ( ) ;
break ;
}
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2016-03-11 16:36:08 +01:00
Map < GCubeUser , List < GCubeRole > > usersAndRoles = null ;
2014-09-16 18:33:11 +02:00
try {
usersAndRoles = userManager . listUsersAndRolesByGroup ( groupId ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2016-03-11 16:36:08 +01:00
Set < GCubeUser > users = usersAndRoles . keySet ( ) ;
2014-09-16 18:33:11 +02:00
ArrayList < String > adminEmailsList = new ArrayList < String > ( ) ;
2016-03-11 16:36:08 +01:00
for ( GCubeUser usr : users ) {
List < GCubeRole > roles = usersAndRoles . get ( usr ) ;
2014-09-16 18:33:11 +02:00
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 ;
}
2014-12-11 18:38:17 +01:00
2014-09-16 18:33:11 +02:00
/ * *
*
* @param scope .
* @param optionalMessage .
* /
2016-04-26 16:26:45 +02:00
public void sendUserUnregisteredNotification ( String username , String scope , String portalbasicurl , String gatewayName ) {
2014-09-16 18:33:11 +02:00
ArrayList < String > adminEmails = getAdministratorsEmails ( scope ) ;
2016-03-11 16:36:08 +01:00
UserManager um = new LiferayUserManager ( ) ;
GCubeUser currUser = null ;
2014-09-16 18:33:11 +02:00
try {
2016-03-11 16:36:08 +01:00
currUser = um . getUserByUsername ( username ) ;
2014-09-16 18:33:11 +02:00
} 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> " ) ;
2016-04-26 17:07:04 +02:00
body . append ( " <b> " + name + " " + lastname + " </b> has left the following environment: " ) ;
2014-09-16 18:33:11 +02:00
body . append ( " <br /><br /> " ) ;
body . append ( " <b> " + scope + " </b> " ) ;
body . append ( " <br /> " ) ;
body . append ( " <br /> " ) ;
body . append ( " <b>Username: </b> " + username ) ;
body . append ( " <br /> " ) ;
2016-03-11 16:36:08 +01:00
body . append ( " <b>e-mail: </b> " + currUser . getEmail ( ) ) ;
2014-09-16 18:33:11 +02:00
body . append ( " </p> " ) ;
String [ ] allMails = new String [ adminEmails . size ( ) ] ;
adminEmails . toArray ( allMails ) ;
2016-04-26 17:07:04 +02:00
EmailNotification mailToAdmin = new EmailNotification ( allMails , " Unregistration from VRE " , body . toString ( ) , getThreadLocalRequest ( ) ) ;
2014-09-16 18:33:11 +02:00
mailToAdmin . sendEmail ( ) ;
}
2014-12-11 18:38:17 +01:00
2014-09-16 18:33:11 +02:00
2013-04-10 18:32:02 +02:00
}