2018-11-30 12:14:48 +01:00
package org.gcube.portal.usersaccount ;
2020-01-17 10:55:17 +01:00
import java.util.ArrayList ;
import java.util.List ;
2018-11-30 12:14:48 +01:00
import org.gcube.common.authorization.library.provider.SecurityTokenProvider ;
2020-01-17 10:55:17 +01:00
import static org.gcube.common.authorization.client.Constants.authorizationService ;
2018-11-30 12:14:48 +01:00
import org.gcube.common.portal.PortalContext ;
import org.gcube.common.scope.api.ScopeProvider ;
2020-01-17 10:55:17 +01:00
import org.gcube.common.storagehub.client.dsl.StorageHubClient ;
import org.gcube.vomanagement.usermanagement.RoleManager ;
import org.gcube.vomanagement.usermanagement.UserManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager ;
import org.gcube.vomanagement.usermanagement.model.GCubeRole ;
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2018-11-30 12:14:48 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
public class WorkspaceCreateAccountThread implements Runnable {
private static Logger _log = LoggerFactory . getLogger ( WorkspaceCreateAccountThread . class ) ;
2020-01-17 10:55:17 +01:00
public static final String AUTORISED_INFRA_ROLE = " Infrastructure-Manager " ;
2018-11-30 12:14:48 +01:00
private String newUserUserName ;
public WorkspaceCreateAccountThread ( String newUserUserName , String newUserFullName , String newUserEmailAddress ) {
super ( ) ;
this . newUserUserName = newUserUserName ;
2020-01-17 10:55:17 +01:00
_log . info ( " Calling StorageHub for Workspace creation to new user: " + newUserUserName + " ( " + newUserFullName + " ) " ) ;
2018-11-30 12:14:48 +01:00
}
@Override
public void run ( ) {
getWS ( newUserUserName , " / " + PortalContext . getConfiguration ( ) . getInfrastructureName ( ) ) ;
}
2020-01-17 10:55:17 +01:00
public static void getWS ( String currentUsername , String context ) {
2018-11-30 12:14:48 +01:00
String username = currentUsername ;
String authorizationToken = PortalContext . getConfiguration ( ) . getCurrentUserToken ( context , username ) ;
SecurityTokenProvider . instance . set ( authorizationToken ) ;
ScopeProvider . instance . set ( context ) ;
2020-01-17 10:55:17 +01:00
String previousToken = authorizationToken ;
try {
//get the super user
String infraContext = " / " + PortalContext . getConfiguration ( ) . getInfrastructureName ( ) ;
2020-01-30 19:21:12 +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-17 10:55:17 +01:00
List < String > rolesString = new ArrayList < String > ( ) ;
2020-01-30 19:21:12 +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-17 10:55:17 +01:00
authorizationService ( ) . setTokenRoles ( theAdminToken , rolesString ) ;
SecurityTokenProvider . instance . set ( theAdminToken ) ;
StorageHubClient shc = new StorageHubClient ( ) ;
shc . createUserAccount ( currentUsername ) ;
SecurityTokenProvider . instance . set ( previousToken ) ;
2020-01-30 19:21:12 +01:00
// }
2018-11-30 12:14:48 +01:00
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
2020-01-17 10:55:17 +01:00
SecurityTokenProvider . instance . set ( previousToken ) ;
return ;
2018-11-30 12:14:48 +01:00
}
}
}