restored the call to shub create workspace without UMA

This commit is contained in:
Massimiliano Assante 2020-12-11 18:00:07 +01:00
parent 61fff62b8a
commit 44db458e1a
1 changed files with 63 additions and 25 deletions

View File

@ -1,38 +1,76 @@
package org.gcube.portal.usersaccount; package org.gcube.portal.usersaccount;
import java.util.ArrayList;
import java.util.List;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import org.gcube.common.portal.PortalContext; import org.gcube.common.portal.PortalContext;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.storagehub.client.dsl.StorageHubClient; import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.portal.oidc.lr62.OIDCUmaUtil; 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;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class WorkspaceCreateAccountThread implements Runnable { public class WorkspaceCreateAccountThread implements Runnable {
private static Logger _log = LoggerFactory.getLogger(WorkspaceCreateAccountThread.class);
public static final String AUTORISED_INFRA_ROLE = "Infrastructure-Manager";
private String newUserUserName;
private static Logger _log = LoggerFactory.getLogger(WorkspaceCreateAccountThread.class); public WorkspaceCreateAccountThread(String newUserUserName, String newUserFullName, String newUserEmailAddress) {
super();
this.newUserUserName = newUserUserName;
_log.info("Calling StorageHub for Workspace creation to new user: " + newUserUserName + " ("+newUserFullName+")");
}
private String newUserUserName; @Override
public void run() {
public WorkspaceCreateAccountThread(String newUserUserName, String newUserFullName, String newUserEmailAddress) { getWS(newUserUserName, "/"+PortalContext.getConfiguration().getInfrastructureName());
super(); }
this.newUserUserName = newUserUserName; public static void getWS(String currentUsername, String context) {
_log.info("Calling SHUB for Workspace creation to new user: " + newUserUserName + " (" + newUserFullName + ")"); String username = currentUsername;
} String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(context, username);
SecurityTokenProvider.instance.set(authorizationToken);
@Override ScopeProvider.instance.set(context);
public void run() { String previousToken = authorizationToken;
String context = "/" + PortalContext.getConfiguration().getInfrastructureName(); try {
String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(context, newUserUserName); //get the super user
SecurityTokenProvider.instance.set(authorizationToken); String infraContext = "/"+PortalContext.getConfiguration().getInfrastructureName();
ScopeProvider.instance.set(context); long groupId = new LiferayGroupManager().getGroupIdFromInfrastructureScope(infraContext);
OIDCUmaUtil.provideConfiguredPortalClientUMATokenInThreadLocal(context); RoleManager rm = new LiferayRoleManager();
try { UserManager uMan = new LiferayUserManager();
StorageHubClient shc = new StorageHubClient(); long roleId = rm.getRoleId(AUTORISED_INFRA_ROLE, groupId);
shc.createUserAccount(newUserUserName); List<GCubeUser> users = uMan.listUsersByGroupAndRole(groupId, roleId);
_log.info("Done Workspace creation for user: " + newUserUserName); if (users.isEmpty()) {
} catch (Exception e) { _log.error("Cannot create this user account on storageHub there is no user having role {} on context {}", AUTORISED_INFRA_ROLE , infraContext);
_log.error("Creating new account on the storage HUB", e); return;
} }
} else {
GCubeUser theAdmin = users.get(0);
String theAdminToken = PortalContext.getConfiguration().getCurrentUserToken(infraContext, theAdmin.getUsername());
List<GCubeRole> theAdminRoles = rm.listRolesByUserAndGroup(theAdmin.getUserId(), groupId);
List<String> rolesString = new ArrayList<String>();
for (GCubeRole gCubeRole : theAdminRoles) {
rolesString.add(gCubeRole.getRoleName());
}
authorizationService().setTokenRoles(theAdminToken, rolesString);
SecurityTokenProvider.instance.set(theAdminToken);
StorageHubClient shc = new StorageHubClient();
shc.createUserAccount(currentUsername);
SecurityTokenProvider.instance.set(previousToken);
}
}
catch (Exception e) {
e.printStackTrace();
SecurityTokenProvider.instance.set(previousToken);
return;
}
}
} }