2014-04-02 12:19:10 +02:00
package org.gcube.portlets.admin.manageusers.server ;
import java.util.ArrayList ;
2017-01-22 12:11:03 +01:00
import java.util.Date ;
2014-04-02 12:19:10 +02:00
import java.util.List ;
2016-09-15 14:27:15 +02:00
import javax.servlet.http.HttpServletRequest ;
2014-04-02 12:19:10 +02:00
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager ;
import org.gcube.applicationsupportlayer.social.NotificationsManager ;
2016-05-26 17:49:42 +02:00
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite ;
2016-03-16 18:46:20 +01:00
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser ;
2016-09-15 14:27:15 +02:00
import org.gcube.common.portal.PortalContext ;
2015-12-21 12:36:27 +01:00
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.common.scope.impl.ScopeBean.Type ;
2021-10-27 16:17:32 +02:00
import org.gcube.common.storagehub.client.plugins.AbstractPlugin ;
import org.gcube.common.storagehub.client.proxies.MessageManagerClient ;
2014-04-02 12:19:10 +02:00
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper ;
2015-10-15 12:35:40 +02:00
import org.gcube.portal.notifications.bean.GenericItemBean ;
2015-10-13 12:07:44 +02:00
import org.gcube.portal.notifications.thread.MessageNotificationsThread ;
2014-04-02 12:19:10 +02:00
import org.gcube.portlets.admin.manageusers.client.ManageUsersService ;
import org.gcube.portlets.admin.manageusers.shared.PortalUserDTO ;
import org.gcube.vomanagement.usermanagement.UserManager ;
2016-03-14 18:30:09 +01:00
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager ;
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2014-04-02 12:19:10 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
2016-12-02 12:40:21 +01:00
import com.liferay.portal.kernel.cache.CacheRegistryUtil ;
import com.liferay.portal.kernel.util.GetterUtil ;
import com.liferay.portal.kernel.util.PropsUtil ;
2016-09-15 14:27:15 +02:00
import com.liferay.portal.model.Group ;
2014-04-02 12:19:10 +02:00
import com.liferay.portal.model.User ;
2016-09-15 14:27:15 +02:00
import com.liferay.portal.model.VirtualHost ;
2014-04-02 12:19:10 +02:00
import com.liferay.portal.security.permission.PermissionChecker ;
import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil ;
import com.liferay.portal.security.permission.PermissionThreadLocal ;
2016-12-02 12:40:21 +01:00
import com.liferay.portal.service.CompanyLocalServiceUtil ;
2016-09-15 14:27:15 +02:00
import com.liferay.portal.service.LayoutSetLocalServiceUtil ;
2014-04-02 12:19:10 +02:00
import com.liferay.portal.service.UserLocalServiceUtil ;
2016-09-15 14:27:15 +02:00
import com.liferay.portal.service.VirtualHostLocalServiceUtil ;
2014-04-02 12:19:10 +02:00
@SuppressWarnings ( " serial " )
2016-12-02 12:40:21 +01:00
public class ManageUsersServiceImpl extends RemoteServiceServlet implements ManageUsersService {
2014-04-02 12:19:10 +02:00
private static final Logger _log = LoggerFactory . getLogger ( ManageUsersServiceImpl . class ) ;
2016-12-02 12:40:21 +01:00
public static final String DEFAULT_COMPANY_WEB_ID = " liferay.com " ;
2014-04-02 12:19:10 +02:00
private final static String POSITION = " POSITION " ;
private final static String LABS = " LABS " ;
2016-03-16 18:46:20 +01: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 ;
}
}
2014-04-02 12:19:10 +02:00
@Override
public ArrayList < PortalUserDTO > getAvailableUsers ( ) {
2016-12-02 12:40:21 +01:00
2014-04-02 12:19:10 +02:00
ArrayList < PortalUserDTO > toReturn = new ArrayList < PortalUserDTO > ( ) ;
2016-03-16 18:46:20 +01:00
if ( isWithinPortal ( ) ) {
2014-04-02 12:19:10 +02:00
return getNonBelongingUsersForVRE ( ) ;
}
else {
for ( int i = 0 ; i < 20 ; i + + ) {
String role = i % 2 = = 0 ? " Research Staff " : " Graduate Fellow " ;
String lastName = " TheLastName " + i ;
String firstLetter = lastName . substring ( 0 , 1 ) ;
PortalUserDTO toAdd = new PortalUserDTO ( " " + i , firstLetter , " aName " + i , lastName , i + " email@isti.cnr.it " , role , " HPC " ) ;
toReturn . add ( toAdd ) ;
}
PortalUserDTO toAdd = new PortalUserDTO ( " " + 1 , " A " , " Massimiliano " , " Assante " , " assante@isti.cnr.it " , " Research Staff " , " NeMIS " ) ;
toReturn . add ( toAdd ) ;
toAdd = new PortalUserDTO ( " " + 1 , " M " , " Francesco " , " Mangiacrapa " , " mangi@isti.cnr.it " , " Graduate Fellow " , " NeMIS " ) ;
toReturn . add ( toAdd ) ;
toAdd = new PortalUserDTO ( " " + 1 , " N " , " Salvatore " , " Neri " , " neri@isti.cnr.it " , " Graduate Fellow " , " KDD " ) ;
toReturn . add ( toAdd ) ;
toAdd = new PortalUserDTO ( " " + 1 , " P " , " Giorgio " , " Pini " , " pini@isti.cnr.it " , " Graduate Fellow " , " SI " ) ;
toReturn . add ( toAdd ) ;
toAdd = new PortalUserDTO ( " " + 1 , " F " , " Dario " , " Faggiu " , " faggiu@isti.cnr.it " , " Research Staff " , " VC " ) ;
toReturn . add ( toAdd ) ;
return toReturn ;
}
}
2016-09-15 14:27:15 +02:00
/ * *
*
* @param request
* @return the current Group instance based on the request
* /
private Group getSiteFromServletRequest ( final HttpServletRequest request ) throws Exception {
String serverName = request . getServerName ( ) ;
Group site = null ;
List < VirtualHost > vHosts = VirtualHostLocalServiceUtil . getVirtualHosts ( 0 , VirtualHostLocalServiceUtil . getVirtualHostsCount ( ) ) ;
for ( VirtualHost virtualHost : vHosts ) {
if ( virtualHost . getHostname ( ) . compareTo ( " localhost " ) ! = 0 & &
virtualHost . getLayoutSetId ( ) ! = 0 & &
virtualHost . getHostname ( ) . compareTo ( serverName ) = = 0 ) {
long layoutSetId = virtualHost . getLayoutSetId ( ) ;
site = LayoutSetLocalServiceUtil . getLayoutSet ( layoutSetId ) . getGroup ( ) ;
return site ;
}
}
_log . warn ( " serverName is " + serverName + " but i could not find any virtualHost associated to it " ) ;
return null ;
}
2014-04-02 12:19:10 +02:00
/ * *
* Retrieves all the users that are registered to portal but are not registered to the current VO
*
* @return A list with the username of the unregistered users
* /
public ArrayList < PortalUserDTO > getNonBelongingUsersForVRE ( ) {
2016-12-02 12:40:21 +01:00
CacheRegistryUtil . clear ( ) ;
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String scope = pContext . getCurrentScope ( getThreadLocalRequest ( ) ) ;
GCubeUser currentUser = pContext . getCurrentUser ( getThreadLocalRequest ( ) ) ;
2014-04-02 12:19:10 +02:00
try {
2016-12-02 12:40:21 +01:00
long companyId = CompanyLocalServiceUtil . getCompanyByWebId ( getDefaultCompanyWebId ( ) ) . getCompanyId ( ) ;
2014-04-02 12:19:10 +02:00
_log . trace ( " Setting Thread Permission " ) ;
2016-03-16 18:46:20 +01:00
User userAdmin = UserLocalServiceUtil . getUserByScreenName ( companyId , ScopeHelper . getAdministratorUsername ( ) ) ;
_log . trace ( " Setting Thread Permission for admin= " + userAdmin . getScreenName ( ) ) ;
PermissionChecker permissionChecker = PermissionCheckerFactoryUtil . create ( userAdmin ) ;
2014-04-02 12:19:10 +02:00
PermissionThreadLocal . setPermissionChecker ( permissionChecker ) ;
_log . trace ( " Setting Permission ok! " ) ;
ArrayList < PortalUserDTO > unregisteredUsers = new ArrayList < PortalUserDTO > ( ) ;
2016-09-15 14:27:15 +02:00
//List<GCubeUser> notBelongingUsers = userM.listUnregisteredUsersByGroup(getCurrentGroupID());
long currSiteGroupId = getSiteFromServletRequest ( getThreadLocalRequest ( ) ) . getGroupId ( ) ;
2016-12-02 12:40:21 +01:00
UserManager userM = new LiferayUserManager ( ) ;
2016-12-29 17:10:41 +01:00
List < GCubeUser > nonBelongingUsers = userM . listUsersByGroup ( currSiteGroupId , false ) ; //the users of i-marine or services etc
2017-01-22 12:11:03 +01:00
nonBelongingUsers . removeAll ( userM . listUsersByGroup ( pContext . getCurrentGroupId ( getThreadLocalRequest ( ) ) , false ) ) ;
2016-09-15 14:27:15 +02:00
for ( GCubeUser u : nonBelongingUsers ) {
2016-03-16 18:46:20 +01:00
String id = u . getUsername ( ) ;
User user = UserLocalServiceUtil . getUserByScreenName ( companyId , id ) ;
String position = " " ;
String labs = " " ;
if ( user . getExpandoBridge ( ) ! = null & & user . getExpandoBridge ( ) . hasAttribute ( POSITION ) ) {
position = ( user . getExpandoBridge ( ) . getAttribute ( POSITION ) ! = null ) ? user . getExpandoBridge ( ) . getAttribute ( POSITION ) . toString ( ) : " " ;
labs = ( user . getExpandoBridge ( ) . getAttribute ( LABS ) ! = null ) ? user . getExpandoBridge ( ) . getAttribute ( LABS ) . toString ( ) : " " ;
}
2016-03-14 18:30:09 +01:00
if ( ( u . getLastName ( ) ! = null & & u . getLastName ( ) . compareTo ( " " ) ! = 0 ) ) {
String firstLetter = u . getLastName ( ) . substring ( 0 , 1 ) ;
2016-12-02 12:40:21 +01:00
2015-12-21 12:36:27 +01:00
ScopeBean validator = new ScopeBean ( scope ) ;
String email = u . getEmail ( ) ;
String username = id ;
if ( validator . is ( Type . VRE ) ) {
2016-03-16 18:46:20 +01:00
email = " ********@ " + email . split ( " @ " ) [ 1 ] ;
2015-12-21 12:36:27 +01:00
}
2016-03-14 18:30:09 +01:00
PortalUserDTO myUser = new PortalUserDTO ( username , firstLetter , u . getFirstName ( ) , u . getLastName ( ) , email , position , labs ) ;
2014-04-02 12:19:10 +02:00
unregisteredUsers . add ( myUser ) ;
}
}
_log . trace ( " Setting Thread Permission back to regular " ) ;
2016-12-02 12:40:21 +01:00
User user = UserLocalServiceUtil . getUserByScreenName ( companyId , currentUser . getUsername ( ) ) ;
2016-03-16 18:46:20 +01:00
permissionChecker = PermissionCheckerFactoryUtil . create ( user ) ;
2014-04-02 12:19:10 +02:00
PermissionThreadLocal . setPermissionChecker ( permissionChecker ) ;
2016-03-16 18:46:20 +01:00
2014-04-02 12:19:10 +02:00
_log . trace ( " Setting Permission ok! " ) ;
2016-03-16 18:46:20 +01:00
2014-04-02 12:19:10 +02:00
return unregisteredUsers ;
}
catch ( Exception e ) {
_log . error ( " Failed to retrieve the unregistered users. An exception was thrown " , e ) ;
}
return null ;
}
/ * *
* register the user to the VRE and in the HL Group , plus send notifications to the users
2017-01-22 12:11:03 +01:00
* it is needed to issue a fake membership request otherwise if the user is loggedIn it is not able to access the VRE unless she logout and login
2014-04-02 12:19:10 +02:00
* /
@Override
public boolean registerUsers ( List < PortalUserDTO > users2Register ) {
2016-12-02 12:40:21 +01:00
long vreGroupId = PortalContext . getConfiguration ( ) . getCurrentGroupId ( getThreadLocalRequest ( ) ) ;
UserManager userM = new LiferayUserManager ( ) ;
2014-04-02 12:19:10 +02:00
for ( PortalUserDTO user : users2Register ) {
try {
2017-01-22 12:11:03 +01:00
GCubeUser currUser = userM . getUserByUsername ( user . getId ( ) ) ;
_log . debug ( " registerUser " + currUser . getUsername ( ) + " to groupId: " + vreGroupId ) ;
userM . requestMembership ( currUser . getUserId ( ) , vreGroupId , " Automatic Request at " + new Date ( ) ) ;
_log . debug ( " fakeRequest sent " ) ;
String replierUsername = LiferayUserManager . getAdmin ( ) . getScreenName ( ) ;
_log . trace ( " Sleep 1 second ... " ) ;
Thread . sleep ( 1000 ) ;
userM . acceptMembershipRequest ( currUser . getUserId ( ) , vreGroupId , true , replierUsername , " Automatic acceptance request at " + new Date ( ) ) ;
_log . info ( " fakeRequest accepted " ) ;
2014-04-02 12:19:10 +02:00
//add the user to the VRE
2017-01-22 12:11:03 +01:00
userM . assignUserToGroup ( vreGroupId , currUser . getUserId ( ) ) ;
2014-04-02 12:19:10 +02:00
//send notification
2016-09-15 14:27:15 +02:00
sendNotificationToUser ( vreGroupId , user ) ;
2014-04-02 12:19:10 +02:00
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
return false ;
}
/ * *
*
* @param addressee
* @return
* @throws Exception
* /
2016-09-15 14:27:15 +02:00
private boolean sendNotificationToUser ( long groupId , PortalUserDTO addressee ) throws Exception {
2016-12-02 12:40:21 +01:00
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String scope = pContext . getCurrentScope ( getThreadLocalRequest ( ) ) ;
GCubeUser currentUser = pContext . getCurrentUser ( getThreadLocalRequest ( ) ) ;
2021-10-27 16:17:32 +02:00
MessageManagerClient client = AbstractPlugin . messages ( ) . build ( ) ;
2016-12-02 12:40:21 +01:00
2015-10-13 12:07:44 +02:00
List < String > recipientIds = new ArrayList < String > ( ) ;
recipientIds . add ( addressee . getId ( ) ) ;
2016-03-16 18:46:20 +01:00
2015-10-15 12:35:40 +02:00
List < GenericItemBean > recipients = new ArrayList < GenericItemBean > ( ) ;
recipients . add ( new GenericItemBean ( addressee . getId ( ) , addressee . getId ( ) , addressee . getName ( ) + " " + addressee . getLastName ( ) , " " ) ) ;
2016-03-16 18:46:20 +01:00
2016-09-15 14:27:15 +02:00
String gatewayName = PortalContext . getConfiguration ( ) . getGatewayName ( getThreadLocalRequest ( ) ) ;
String gatewayURL = PortalContext . getConfiguration ( ) . getGatewayURL ( getThreadLocalRequest ( ) ) ;
String vreURL = gatewayURL + " /group " + new LiferayGroupManager ( ) . getGroup ( groupId ) . getFriendlyURL ( ) ;
String subject = " Registration to VRE notification " ;
2016-12-02 12:40:21 +01:00
String body = " Dear " + addressee . getName ( ) + " , \ n \ n I just registered you to the VRE " + pContext . getCurrentGroupName ( getThreadLocalRequest ( ) ) + " on the " + gatewayName + " . \ n \ n " ;
2016-09-15 14:27:15 +02:00
body + = " Please, click here to access the VRE: " + vreURL ;
2021-10-27 16:17:32 +02:00
String messageId = client . sendMessage ( recipientIds , subject , body , new ArrayList < String > ( ) ) ;
2016-03-16 18:46:20 +01:00
2015-10-13 12:07:44 +02:00
_log . debug ( " Sending message notification to: " + recipientIds . toString ( ) ) ;
2016-12-02 12:40:21 +01:00
NotificationsManager nm = new ApplicationNotificationsManager ( new SocialNetworkingSite ( getThreadLocalRequest ( ) ) , scope , new SocialNetworkingUser (
currentUser . getUsername ( ) , currentUser . getEmail ( ) , currentUser . getFullname ( ) , currentUser . getUserAvatarURL ( ) ) ) ;
2015-10-15 12:35:40 +02:00
Thread thread = new Thread ( new MessageNotificationsThread ( recipients , messageId , subject , body , nm ) ) ;
2015-10-13 12:07:44 +02:00
thread . start ( ) ;
return ( messageId ! = null ) ;
2014-04-02 12:19:10 +02:00
}
2016-12-02 12:40:21 +01:00
2014-04-02 12:19:10 +02:00
/ * *
*
2016-12-02 12:40:21 +01:00
* @return the default company web - id ( e . g . iMarine . eu )
2014-04-02 12:19:10 +02:00
* /
2016-12-02 12:40:21 +01:00
public static String getDefaultCompanyWebId ( ) {
String defaultWebId = " " ;
2014-04-02 12:19:10 +02:00
try {
2016-12-02 12:40:21 +01:00
defaultWebId = GetterUtil . getString ( PropsUtil . get ( " company.default.web.id " ) ) ;
}
catch ( NullPointerException e ) {
_log . info ( " Cound not find property company.default.web.id in portal.ext file returning default web id: " + DEFAULT_COMPANY_WEB_ID ) ;
return DEFAULT_COMPANY_WEB_ID ;
2014-04-02 12:19:10 +02:00
}
2016-12-02 12:40:21 +01:00
return defaultWebId ;
2014-04-02 12:19:10 +02:00
}
}