2017-02-23 18:24:59 +01:00
package org.gcube.portal.plugins ;
2019-10-11 16:47:23 +02:00
import static org.gcube.common.authorization.client.Constants.authorizationService ;
import java.util.ArrayList ;
2017-02-23 18:24:59 +01:00
import java.util.List ;
2019-10-11 16:47:23 +02:00
import org.gcube.common.authorization.library.provider.SecurityTokenProvider ;
2017-02-23 18:24:59 +01:00
import org.gcube.common.portal.PortalContext ;
import org.gcube.common.scope.api.ScopeProvider ;
2020-01-15 19:14:09 +01:00
import org.gcube.common.storagehub.client.dsl.StorageHubClient ;
import org.gcube.common.storagehub.client.dsl.Util ;
import org.gcube.common.storagehub.client.dsl.VREFolderManager ;
2017-02-23 18:24:59 +01:00
import org.gcube.vomanagement.usermanagement.GroupManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager ;
2020-01-15 19:14:09 +01:00
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager ;
2017-02-23 18:24:59 +01:00
import org.gcube.vomanagement.usermanagement.model.GCubeRole ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2019-10-11 16:47:23 +02:00
import com.liferay.portal.kernel.exception.PortalException ;
import com.liferay.portal.kernel.exception.SystemException ;
2017-02-23 18:24:59 +01:00
import com.liferay.portal.model.Role ;
import com.liferay.portal.model.UserGroupRole ;
import com.liferay.portal.service.RoleLocalServiceUtil ;
import com.liferay.portal.service.UserGroupRoleLocalService ;
import com.liferay.portal.service.UserGroupRoleLocalServiceWrapper ;
import com.liferay.portal.service.UserLocalServiceUtil ;
public class GCubeHookSiteRoleLocalService extends UserGroupRoleLocalServiceWrapper {
/ * *
* logger
* /
private static final Logger _log = LoggerFactory . getLogger ( GCubeHookSiteRoleLocalService . class ) ;
private GroupManager gm ;
2020-01-15 19:14:09 +01:00
private LiferayUserManager uMan ;
2017-02-23 18:24:59 +01:00
public GCubeHookSiteRoleLocalService ( UserGroupRoleLocalService userGroupRoleLocalService ) {
super ( userGroupRoleLocalService ) ;
gm = new LiferayGroupManager ( ) ;
2020-01-15 19:14:09 +01:00
uMan = new LiferayUserManager ( ) ;
2017-02-23 18:24:59 +01:00
System . out . println ( " GCubeHookSiteRoleLocalService hook is UP & Listening ... " ) ;
}
2020-01-15 19:14:09 +01:00
//TODO: as soon as Feature https://support.d4science.org/issues/17726 is delivered take care of this also
2017-02-23 18:24:59 +01:00
@Override
2020-01-15 19:14:09 +01:00
public java . util . List < com . liferay . portal . model . UserGroupRole > addUserGroupRoles ( long [ ] userIds , long groupId , long roleId ) throws com . liferay . portal . kernel . exception . SystemException {
List < UserGroupRole > toReturn = super . addUserGroupRoles ( userIds , groupId , roleId ) ;
return toReturn ;
}
@Override
public java . util . List < com . liferay . portal . model . UserGroupRole > addUserGroupRoles ( long userId , long groupId , long [ ] roleIds ) throws com . liferay . portal . kernel . exception . SystemException {
2017-02-23 18:24:59 +01:00
List < UserGroupRole > toReturn = super . addUserGroupRoles ( userId , groupId , roleIds ) ;
try {
2019-10-11 16:47:23 +02:00
String context = gm . getInfrastructureScope ( groupId ) ;
String username = UserLocalServiceUtil . getUser ( userId ) . getScreenName ( ) ;
2020-01-31 11:37:59 +01:00
/* Commenting this part as when the user is just created it fails*/
// String userToken = authorizationService().resolveTokenByUserAndContext(username, context);
// List<String> userRoles = getUserRoles(roleIds);
// authorizationService().setTokenRoles(userToken, userRoles);
2017-02-23 18:24:59 +01:00
_log . debug ( " Check if addUserGroupRoles is done in a VRE " ) ;
if ( gm . isVRE ( groupId ) ) {
_log . debug ( " addUserGroupRoles performed in a VRE, groupId= " + groupId ) ;
boolean vreManagerRolePresent = false ;
for ( int i = 0 ; i < roleIds . length ; i + + ) {
Role role = RoleLocalServiceUtil . getRole ( roleIds [ i ] ) ;
if ( role . getName ( ) . compareTo ( GCubeRole . VRE_MANAGER_LABEL ) = = 0 ) {
_log . info ( " User is being promoted (or was) as VREFolder Administrator, userId= " + userId + " on Site groupId= " + groupId ) ;
vreManagerRolePresent = true ;
break ;
}
}
setVREFolderAdministrator ( userId , groupId , vreManagerRolePresent ) ;
} else {
_log . debug ( " addUserGroupRoles NOT done in a VRE, groupId= " + groupId ) ;
}
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return toReturn ;
}
2019-10-11 16:47:23 +02:00
private List < String > getUserRoles ( long [ ] roleIds ) throws PortalException , SystemException {
List < String > toReturn = new ArrayList < > ( ) ;
for ( int i = 0 ; i < roleIds . length ; i + + ) {
Role role = RoleLocalServiceUtil . getRole ( roleIds [ i ] ) ;
toReturn . add ( role . getName ( ) ) ;
}
return toReturn ;
}
2020-01-15 19:14:09 +01:00
private boolean setVREFolderAdministrator ( long userId , long groupId , boolean enable ) throws Exception {
String context = gm . getInfrastructureScope ( groupId ) ;
ScopeProvider . instance . set ( context ) ;
String vreFolderTitle = Util . getVREGroupFromContext ( context ) ;
2020-01-30 19:16:38 +01:00
_log . info ( " The vreFolderTitle on which the VREFolder role is being {} is {} " , enable , vreFolderTitle ) ;
_log . info ( " Before StorageHubClient shc = new StorageHubClient(); " ) ;
2020-01-15 19:14:09 +01:00
StorageHubClient shc = new StorageHubClient ( ) ;
2020-01-30 19:16:38 +01:00
_log . info ( " Before shc.getVreFolderManager(vreFolderTitle); " ) ;
2020-01-15 19:14:09 +01:00
VREFolderManager vreFolderManager = shc . getVreFolderManager ( vreFolderTitle ) ;
String previousToken = SecurityTokenProvider . instance . get ( ) ;
//get the super user
2020-01-30 19:16:38 +01:00
_log . info ( " //get the super user " ) ;
2020-01-15 19:14:09 +01:00
String infraContext = " / " + PortalContext . getConfiguration ( ) . getInfrastructureName ( ) ;
2020-01-30 19:16:38 +01:00
// long rootgroupId = gm.getGroupIdFromInfrastructureScope(infraContext);
// RoleManager rm = new LiferayRoleManager();
// long roleId = rm.getRoleId(HookConstants.AUTORISED_INFRA_ROLE, rootgroupId);
// List<GCubeUser> users = uMan.listUsersByGroupAndRole(rootgroupId, roleId);
// if (users.isEmpty()) {
// _log.error("Cannot add the user as VRE Folder admin: there is no user having role {} on context: {}", HookConstants.AUTORISED_INFRA_ROLE, infraContext);
// return false;
// }
// else {
//GCubeUser theAdmin = users.get(0);
String adminUsername = " lucio.lelii " ;
_log . info ( " Got the super user: {} " , adminUsername ) ;
String theAdminToken = PortalContext . getConfiguration ( ) . getCurrentUserToken ( infraContext , adminUsername ) ;
2020-01-15 19:14:09 +01:00
List < String > rolesString = new ArrayList < String > ( ) ;
2020-01-30 19:16:38 +01:00
// List<GCubeRole> theAdminRoles = rm.listRolesByUserAndGroup(theAdmin.getUserId(), rootgroupId);
// for (GCubeRole gCubeRole : theAdminRoles) {
// rolesString.add(gCubeRole.getRoleName());
// }
rolesString . add ( " Infrastructure-Manager " ) ;
_log . info ( " authorizationService().setTokenRoles(theAdminToken, rolesString); " , theAdminToken ) ;
2020-01-15 19:14:09 +01:00
authorizationService ( ) . setTokenRoles ( theAdminToken , rolesString ) ;
SecurityTokenProvider . instance . set ( theAdminToken ) ;
String theUserToPromoteOrDeclass = uMan . getUserById ( userId ) . getUsername ( ) ;
_log . info ( " The {} is being promoted? {} " , theUserToPromoteOrDeclass , enable ) ;
if ( enable )
vreFolderManager . setAdmin ( theUserToPromoteOrDeclass ) ;
else
vreFolderManager . removeAdmin ( theUserToPromoteOrDeclass ) ;
SecurityTokenProvider . instance . set ( previousToken ) ;
return true ;
2020-01-30 19:16:38 +01:00
// }
2020-01-15 19:14:09 +01:00
}
2017-02-23 18:24:59 +01:00
}