Feature #21319 revised and ported to the new auth framework

This commit is contained in:
Massimiliano Assante 2022-02-01 13:58:20 +01:00
parent 6f1988df6e
commit decacda3db
16 changed files with 279 additions and 206 deletions

View File

@ -1,40 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="PerformFISH-Apps-portlet">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="storagehub-client-wrapper-1.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/storagehub-client-wrapper/storagehub-client-wrapper">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="java-output-path" value="/PerformFISH-Apps/target/classes"/>
<property name="context-root" value="PerformFISH-Apps-portlet"/>
</wb-module>
</project-modules>

View File

@ -3,9 +3,14 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v1-5-0] - 2022-02-01
- Feature #21319 revised and ported to the new auth framework
## [v1-4-0] - 2021-06-14
- [Feature #21319](https://support.d4science.org/issues/21319) Removed Home Library dependency and Ported to StorageHub
- [Feature #21319] (https://support.d4science.org/issues/21319) Removed Home Library dependency and Ported to StorageHub
## [v1-3-1] - 2019-10-09

16
pom.xml
View File

@ -14,7 +14,7 @@
<artifactId>PerformFISH-Apps-portlet</artifactId>
<packaging>war</packaging>
<name>PerformFISH-Apps Portlet</name>
<version>1.4.0</version>
<version>1.5.0-SNAPSHOT</version>
<description>
PerformFISH Apps is a set of portlet for the implementation of the Form Submission useful to calculate KPIs
</description>
@ -40,13 +40,23 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>maven-portal-bom</artifactId>
<version>3.6.2</version>
<version>3.6.3-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.portal</groupId>
<artifactId>oidc-library-portal</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>oidc-library</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>storagehub-client-wrapper</artifactId>
@ -96,7 +106,7 @@
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>storagehub-client-library</artifactId>
<!-- <version>1.2.2</version> -->
<!-- <version>1.2.2</version> -->
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>

View File

@ -14,6 +14,7 @@ import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.gcube.common.storagehub.model.items.SharedFolder;
import org.gcube.common.storagehubwrapper.server.tohl.Workspace;
import org.gcube.common.storagehubwrapper.shared.ACLType;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceFolder;
@ -82,9 +83,11 @@ public class AssociateUsersToCompany extends MVCPortlet {
LiferayRoleManager rm = new LiferayRoleManager();
LiferayUserManager um = new LiferayUserManager();
GCubeTeam team = rm.getTeam(teamId);
WorkspaceFolder wsFolder = Utils.getWSCompanyFolder(currentUsername, context, team);
Utils utils = new Utils(currentUsername, context, actionRequest);
SharedFolder theCompanyFolder = utils.getWSCompanyFolder(team, actionRequest);
WorkspaceFolder wsFolder = (WorkspaceFolder) utils.getWS().getItem(theCompanyFolder.getId());
if (wsFolder.isShared()) {
Workspace ws = Utils.getWS(currentUsername, context);
Workspace ws = utils.getWS();
WorkspaceSharedFolder sharedFolder = (WorkspaceSharedFolder) wsFolder;
List<String> usersToShareWith = Arrays.asList(usernamesToAssociateToCompany);
Set<String> usersSetToShareWith = new HashSet<>();

View File

@ -34,6 +34,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.storagehub.client.StreamDescriptor;
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
@ -46,6 +47,8 @@ import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceFolder;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItem;
import org.gcube.common.storagehubwrapper.shared.tohl.impl.WorkspaceFileVersion;
import org.gcube.common.storagehubwrapper.shared.tohl.items.FileItem;
import org.gcube.oidc.rest.JWTToken;
import org.gcube.portal.oidc.lr62.OIDCUmaUtil;
import org.gcube.portlets.user.performfish.bean.PublishAnonymisedJob;
import org.gcube.portlets.user.performfish.bean.SubmittedForm;
import org.gcube.portlets.user.performfish.util.ImportedBatchChecker;
@ -117,13 +120,12 @@ public class CompanyFarmRepository extends MVCPortlet {
String context = Utils.getCurrentContext(request);
String username = Utils.getCurrentUser(request).getUsername();
if (theFarm != null ) {
Utils utils = new Utils(username, context, request);
if (theFarm != null ) {
//check folder exists
WorkspaceFolder sharedFolder = Utils.getWSFarmFolder(
username,
context,
theCompany, theFarm);
Workspace ws = Utils.getWS(username, context);
WorkspaceFolder sharedFolder = utils.getWSFarmFolder(theCompany, theFarm);
Workspace ws = utils.getWS();
List<? extends WorkspaceItem> companyFiles = ws.getChildren(sharedFolder.getId(), true);
Collections.sort(companyFiles, new WSItemComparator());
@ -247,7 +249,8 @@ public class CompanyFarmRepository extends MVCPortlet {
HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(request);
GCubeUser currentUser = Utils.getCurrentUser(httpReq);
String context = Utils.getCurrentContext(httpReq);
Workspace ws = Utils.getWS(currentUser.getUsername(), context) ;
Utils utils = new Utils(currentUser.getUsername(), context, request);
Workspace ws = utils.getWS() ;
WorkspaceItem theItem = ws.getItem(itemId);
if (theItem instanceof FileItem) {
FileItem file = (FileItem) theItem;
@ -293,7 +296,8 @@ public class CompanyFarmRepository extends MVCPortlet {
else if (versionDownloadItemId != null && versionDownloadName != null) { //check if download version
GCubeUser currentUser = Utils.getCurrentUser(resourceRequest);
String context = Utils.getCurrentContext(resourceRequest);
Workspace ws = Utils.getWS(currentUser.getUsername(), context) ;
Utils utils = new Utils(currentUser.getUsername(), context, resourceRequest);
Workspace ws = utils.getWS();
WorkspaceItem theItem;
try {
theItem = ws.getItem(versionDownloadItemId);

View File

@ -70,7 +70,8 @@ public class CompanyPrivateRepository extends MVCPortlet {
GCubeUser currentUser = Utils.getCurrentUser(request);
RoleManager rm = new LiferayRoleManager();
//check folder exists
Utils.getWSCompanyFolder(currentUser.getUsername(), currentContext, theCompany);
Utils utils = new Utils(currentUser.getUsername(), currentContext, request);
utils.getWSCompanyFolder(theCompany, request);
//look for company farms
Connection conn = DatabaseConnection.getInstance(Utils.getCurrentContext(groupId)).getConnection();
List<Farm> companyFarms = DBUtil.listFarmsByCompanyId(conn, theCompany.getTeamId());
@ -78,11 +79,8 @@ public class CompanyPrivateRepository extends MVCPortlet {
for (Farm farm : companyFarms) {
GCubeTeam theFarm = rm.getTeam(farm.getFarmId());
farm.setName(theFarm.getTeamName());
Workspace ws = Utils.getWS(currentUser.getUsername(), currentContext) ;
WorkspaceFolder farmFolder = Utils.getWSFarmFolder(
currentUser.getUsername(),
currentContext,
theCompany, theFarm);
Workspace ws = utils.getWS() ;
WorkspaceFolder farmFolder = utils.getWSFarmFolder(theCompany, theFarm);
List<? extends WorkspaceItem> farmItems = ws.getChildren(farmFolder.getId(), true);
Collections.sort(farmItems, new WSItemComparator());
List<FileItem> farmFiles = new ArrayList<>();
@ -119,7 +117,8 @@ public class CompanyPrivateRepository extends MVCPortlet {
HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(request);
GCubeUser currentUser = Utils.getCurrentUser(httpReq);
String context = Utils.getCurrentContext(httpReq);
Workspace ws = Utils.getWS(currentUser.getUsername(), context) ;
Utils utils = new Utils(currentUser.getUsername(), context, request);
Workspace ws = utils.getWS() ;
WorkspaceItem theItem = ws.getItem(itemId);
if (theItem instanceof FileItem) {
FileItem file = (FileItem) theItem;
@ -160,7 +159,8 @@ public class CompanyPrivateRepository extends MVCPortlet {
else if (versionDownloadItemId != null && versionDownloadName != null) { //check if download version
GCubeUser currentUser = Utils.getCurrentUser(resourceRequest);
String context = Utils.getCurrentContext(resourceRequest);
Workspace ws = Utils.getWS(currentUser.getUsername(), context) ;
Utils utils = new Utils(currentUser.getUsername(), context, resourceRequest);
Workspace ws = utils.getWS() ;
WorkspaceItem theItem;
try {
theItem = ws.getItem(versionDownloadItemId);

View File

@ -89,7 +89,8 @@ public class EmptyFormsDocumentsDisplay extends MVCPortlet {
if (folderId != null && !folderId.equals("")) {
String context = Utils.getCurrentContext(request);
String username = Utils.getCurrentUser(request).getUsername();
Workspace ws = Utils.getWS(username, context) ;
Utils utils = new Utils(username, context, request);
Workspace ws = utils.getWS() ;
String selectedPhase = GetterUtil.getString(portletPreferences.getValue(PFISHConstants.PHASE_PREFERENCE_ATTR_NAME, StringPool.BLANK));
//in theChildren expected to find 3 subfolders: Utils.SHOW_HATCHERY, SHOW_PRE_ONGROWING ..
@ -155,7 +156,8 @@ public class EmptyFormsDocumentsDisplay extends MVCPortlet {
String authorizationToken = Utils.getCurrentUserToken(context, username );
SecurityTokenProvider.instance.set(authorizationToken);
ScopeProvider.instance.set(context);
Workspace workspace = Utils.getWS(username, context) ;
Utils utils = new Utils(username, context, resourceRequest);
Workspace workspace = utils.getWS() ;
String wsItemId = uploadToUserWorkspace(workspace, fileName, contentType, new ByteArrayInputStream(FileUtil.getBytes(multipartFile)));
String toReturn = workspace.getPublicLinkForFile(wsItemId).toString();
@ -172,7 +174,8 @@ public class EmptyFormsDocumentsDisplay extends MVCPortlet {
//if download file
String fileToDownloadId = ParamUtil.getString(resourceRequest, "fileToDownloadId", null);
String context = Utils.getCurrentContext(resourceRequest);
Workspace ws = Utils.getWS("guest", context) ;
Utils utils = new Utils("guest", context, resourceRequest);
Workspace ws = utils.getWS() ;
WorkspaceItem theItem;
try {
theItem = ws.getItem(fileToDownloadId);

View File

@ -41,8 +41,9 @@ public class ManageCompanyAdmins extends MVCPortlet {
request.setAttribute("theTeam", TeamLocalServiceUtil.getTeam(theTeam.getTeamId()));
RoleManager rm = new LiferayRoleManager();
if (theTeam != null && Utils.isCompanyAdmin(Utils.getCurrentUser(request), groupId, theTeam)) { //check is Admin
//check folder exists
Utils.getWSCompanyFolder(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request), theTeam);
// //check folder exists
// Utils utils = new Utils(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request));
// utils.getWSCompanyFolder(theTeam);
List<CompanyMember> companyAdmins = Utils.getCompanyAdminTeamMembers(theTeam.getTeamId(), theTeam.getGroupId());
for (CompanyMember member : companyAdmins) { //anonymising email

View File

@ -13,6 +13,7 @@ import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.gcube.common.storagehub.model.items.SharedFolder;
import org.gcube.common.storagehubwrapper.server.tohl.Workspace;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceFolder;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceSharedFolder;
@ -50,8 +51,9 @@ public class ManageCompanyUsers extends MVCPortlet {
request.setAttribute("theTeam", TeamLocalServiceUtil.getTeam(theTeam.getTeamId()));
RoleManager rm = new LiferayRoleManager();
if (theTeam != null && Utils.isCompanyAdmin(Utils.getCurrentUser(request), groupId, theTeam)) { //check is Admin
//check folder exists
Utils.getWSCompanyFolder(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request), theTeam);
// //check folder exists
// Utils utils = new Utils(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request));
// utils.getWSCompanyFolder(theTeam);
List<CompanyMember> companyMembers = Utils.getRegularCompanyMembers(theTeam.getTeamId(), theTeam.getGroupId());
for (CompanyMember member : companyMembers) { //anonymising email
member.setEmail(member.getUser().getEmail());
@ -85,40 +87,43 @@ public class ManageCompanyUsers extends MVCPortlet {
*/
public void removeCompanUsers(ActionRequest actionRequest,ActionResponse actionResponse) throws Exception {
try {
String[] usernamesToRemoveFromCompany = ParamUtil.getParameterValues(actionRequest, "removeUserIds");
long teamId = ParamUtil.getLong(actionRequest, "teamId");
String currentUsername = ParamUtil.getString(actionRequest, "currentUsername");
long currentGroupId = ParamUtil.getLong(actionRequest, "currentGroupId");
String context = Utils.getCurrentContext(currentGroupId);
_log.info(currentUsername + " has requested the deletion of some user(s) from company having id="+teamId + " in context="+context);
LiferayRoleManager rm = new LiferayRoleManager();
LiferayUserManager um = new LiferayUserManager();
GCubeTeam team = rm.getTeam(teamId);
WorkspaceFolder wsFolder = Utils.getWSCompanyFolder(currentUsername, context, team);
if (wsFolder.isShared()) {
WorkspaceSharedFolder sharedFolder = (WorkspaceSharedFolder) wsFolder;
Workspace ws = Utils.getWS(currentUsername, context);
for (int i = 0; i < usernamesToRemoveFromCompany.length; i++) {
_log.info("Unsharing user =>"+usernamesToRemoveFromCompany[i]);
Set<String> userNamesToUnshare = new HashSet<>();
userNamesToUnshare.add(usernamesToRemoveFromCompany[i]);
ws.unshareFolder(sharedFolder.getId(), userNamesToUnshare);
_log.info("Unsharing OK removing user from team =>"+usernamesToRemoveFromCompany[i]);
long userId = um.getUserByUsername(usernamesToRemoveFromCompany[i]).getUserId();
TeamLocalServiceUtil.deleteUserTeam(userId, team.getTeamId());
List<Farm> thePossibleFarms = Utils.getFarmsByUserId(userId, teamId, currentGroupId);
if (thePossibleFarms != null && !thePossibleFarms.isEmpty()) {
for (Farm farm : thePossibleFarms) {
removeFromFarm(farm.getFarmId(), currentGroupId, userId);
}
}
_log.info("removing user from team Done");
}
}
else {
SessionErrors.add(actionRequest.getPortletSession(), "operation-error");
actionResponse.setRenderParameter("mvcPath", "/html/error_pages/operation-error.jsp");
}
// String[] usernamesToRemoveFromCompany = ParamUtil.getParameterValues(actionRequest, "removeUserIds");
// long teamId = ParamUtil.getLong(actionRequest, "teamId");
// String currentUsername = ParamUtil.getString(actionRequest, "currentUsername");
// long currentGroupId = ParamUtil.getLong(actionRequest, "currentGroupId");
// String context = Utils.getCurrentContext(currentGroupId);
// _log.info(currentUsername + " has requested the deletion of some user(s) from company having id="+teamId + " in context="+context);
// LiferayRoleManager rm = new LiferayRoleManager();
// LiferayUserManager um = new LiferayUserManager();
// GCubeTeam team = rm.getTeam(teamId);
// Utils utils = new Utils(currentUsername, context, actionRequest);
// SharedFolder theCompanyFolder = utils.getWSCompanyFolder(team, actionRequest);
// WorkspaceFolder wsFolder = (WorkspaceFolder) utils.getWS().getItem(theCompanyFolder.getId());
//
// if (wsFolder.isShared()) {
// WorkspaceSharedFolder sharedFolder = (WorkspaceSharedFolder) wsFolder;
// Workspace ws = utils.getWS();
// for (int i = 0; i < usernamesToRemoveFromCompany.length; i++) {
// _log.info("Unsharing user =>"+usernamesToRemoveFromCompany[i]);
// Set<String> userNamesToUnshare = new HashSet<>();
// userNamesToUnshare.add(usernamesToRemoveFromCompany[i]);
// ws.unshareFolder(sharedFolder.getId(), userNamesToUnshare);
// _log.info("Unsharing OK removing user from team =>"+usernamesToRemoveFromCompany[i]);
// long userId = um.getUserByUsername(usernamesToRemoveFromCompany[i]).getUserId();
// TeamLocalServiceUtil.deleteUserTeam(userId, team.getTeamId());
// List<Farm> thePossibleFarms = Utils.getFarmsByUserId(userId, teamId, currentGroupId);
// if (thePossibleFarms != null && !thePossibleFarms.isEmpty()) {
// for (Farm farm : thePossibleFarms) {
// removeFromFarm(farm.getFarmId(), currentGroupId, userId);
// }
// }
// _log.info("removing user from team Done");
// }
// }
// else {
// SessionErrors.add(actionRequest.getPortletSession(), "operation-error");
// actionResponse.setRenderParameter("mvcPath", "/html/error_pages/operation-error.jsp");
// }
}
catch (Exception e) {
SessionErrors.add(actionRequest.getPortletSession(),"operation-error");

View File

@ -72,8 +72,9 @@ public class ManageFarmAdministrators extends MVCPortlet {
if (theCompany != null && Utils.isFarmAdmin(Utils.getCurrentUser(request), groupId, theCompany)) { //check is Admin
Boolean isCompanyAdministrator = Utils.isCompanyAdmin(Utils.getCurrentUser(request), groupId, theCompany);
request.setAttribute("isCompanyAdministrator", isCompanyAdministrator);
//check folder exists
Utils.getWSCompanyFolder(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request), theCompany);
// //check folder exists
// Utils utils = new Utils(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request));
// utils.getWSCompanyFolder(theCompany);
List<GCubeUser> farmAdmins = Utils.getFarmAdminTeamMembers(theCompany, theFarm);
request.setAttribute("farmAdmins", farmAdmins); //pass to the JSP

View File

@ -72,7 +72,8 @@ public class ManageFarmUsers extends MVCPortlet {
if (theFarm != null && Utils.isFarmAdmin(Utils.getCurrentUser(request), groupId, theFarm)) { //check is Admin
request.setAttribute("theFarm", TeamLocalServiceUtil.getTeam(theFarm.getTeamId()));
//check folder exists
Utils.getWSCompanyFolder(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request), theCompany);
Utils utils = new Utils(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request), request);
utils.getWSCompanyFolder(theCompany, request);
List<GCubeUser> farmMembers = Utils.getRegularFarmMembers(theFarm.getTeamId(), theFarm.getGroupId());
for (GCubeUser member : farmMembers) { //anonymising email
int n = member.getEmail().indexOf("@");

View File

@ -99,8 +99,9 @@ public class ManageFarms extends MVCPortlet {
GCubeTeam theFarm = RM.getTeam(farmId);
GCubeTeam theCompany = RM.getTeam(companyId);
_log.info("Trying Delete Farm with name " + theFarm.getTeamName() + " of Company: " + theCompany.getTeamName());
WorkspaceFolder farmFolder = Utils.getWSFarmFolder(username, context, theCompany, theFarm);
Utils.getWS(username, context).deleteItem(farmFolder.getId());
Utils utils = new Utils(username, context, actionRequest);
WorkspaceFolder farmFolder = utils.getWSFarmFolder(theCompany, theFarm);
utils.getWS().deleteItem(farmFolder.getId());
_log.info(username + " Deleted Farm Folder with Id = "+farmId + " deleting farm row DB now ");
boolean resultDeleteFarmDB = DBUtil.deleteFarm(conn, farmId);
if (!resultDeleteFarmDB) {

View File

@ -39,8 +39,9 @@ public class PromoteCompanyAdministrators extends MVCPortlet {
GCubeTeam theTeam = Utils.checkBelongingToOneCompanyOnly(request, response, this);
request.setAttribute("theTeam", TeamLocalServiceUtil.getTeam(theTeam.getTeamId()));
if (theTeam != null && Utils.isCompanyAdmin(Utils.getCurrentUser(request), groupId, theTeam)) { //check is Admin
//check folder exists
Utils.getWSCompanyFolder(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request), theTeam);
// //check folder exists
// Utils utils = new Utils(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request));
// utils.getWSCompanyFolder(theTeam);
List<CompanyMember> companyAdmins = Utils.getCompanyAdminTeamMembers(theTeam.getTeamId(), theTeam.getGroupId());
request.setAttribute("companyAdmins", companyAdmins); //pass to the JSP

View File

@ -51,8 +51,9 @@ public class PromoteFarmAdministrators extends MVCPortlet {
theFarm = (GCubeTeam) request.getPortletSession().getAttribute("theFarm");
}
if (theFarm != null && Utils.isFarmAdmin(Utils.getCurrentUser(request), groupId, theFarm)) { //check is a Farm Admin
//check folder exists
Utils.getWSCompanyFolder(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request), theFarm);
// //check folder exists
// Utils utils = new Utils(Utils.getCurrentUser(request).getUsername(), Utils.getCurrentContext(request));
// utils.getWSCompanyFolder(theFarm);
List<GCubeUser> companyAdmins = Utils.getFarmAdminTeamMembers(theCompany, theFarm);
request.setAttribute("companyAdmins", companyAdmins); //pass to the JSP

View File

@ -140,7 +140,7 @@ public class SubmittedFormValidation extends MVCPortlet {
if (result.isSuccess()) {
try {
String decodedURL = new java.net.URI(inputFileLink).getPath();
uploadToCompanyRepository(username, context, companyId, farmId, fileName, new URL(decodedURL).openConnection().getInputStream());
uploadToCompanyRepository(resourceRequest, username, context, companyId, farmId, fileName, new URL(decodedURL).openConnection().getInputStream());
Connection conn = DatabaseConnection.getInstance(Utils.getCurrentContext(groupId)).getConnection();
java.sql.Date lastActivity = new java.sql.Date(new Date().getTime());
DBUtil.updateFarmLastSubmissionActivity(conn, farmId, lastActivity);
@ -155,11 +155,13 @@ public class SubmittedFormValidation extends MVCPortlet {
resourceResponse.getWriter().println(fileObject);
}
private String uploadToCompanyRepository(String username, String context, long companyId, long farmId, String fileName, InputStream fileData) throws Exception {
private String uploadToCompanyRepository(ResourceRequest resourceRequest, String username, String context, long companyId, long farmId, String fileName, InputStream fileData) throws Exception {
GCubeTeam theCompany = new LiferayRoleManager().getTeam(companyId);
GCubeTeam theFarm = new LiferayRoleManager().getTeam(farmId);
Workspace ws = Utils.getWS(username, context);
WorkspaceFolder companyRepoFolder = Utils.getWSFarmFolder(username, context, theCompany, theFarm);
//check folder exists
Utils utils = new Utils(username, context, resourceRequest);
Workspace ws = utils.getWS();
WorkspaceFolder companyRepoFolder = utils.getWSFarmFolder(theCompany, theFarm);
String toReturn = "";
WorkspaceItem item = ws.uploadFile(companyRepoFolder.getId(), fileData, fileName, "User form submission on " + new Date());
//ExternalFile item = companyRepoFolder.createExternalFileItem(fileName, "User form submission on " + new Date(), XSLX_MIME, fileData);

View File

@ -17,6 +17,7 @@ import java.util.List;
import java.util.Set;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
@ -24,6 +25,7 @@ import javax.portlet.ResourceRequest;
import javax.servlet.http.HttpServletRequest;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.provider.UserInfo;
import org.gcube.common.portal.PortalContext;
@ -31,11 +33,18 @@ import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.GCoreEndpoint.Profile.Endpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.storagehub.client.dsl.FolderContainer;
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.common.storagehub.model.items.Item;
import org.gcube.common.storagehub.model.items.SharedFolder;
import org.gcube.common.storagehubwrapper.server.StorageHubWrapper;
import org.gcube.common.storagehubwrapper.server.tohl.Workspace;
import org.gcube.common.storagehubwrapper.shared.ACLType;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceFolder;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItem;
import org.gcube.oidc.rest.JWTToken;
import org.gcube.portal.oidc.lr62.JWTTokenUtil;
import org.gcube.portal.oidc.lr62.OIDCUmaUtil;
import org.gcube.portlets.user.performfish.bean.Association;
import org.gcube.portlets.user.performfish.bean.Company;
import org.gcube.portlets.user.performfish.bean.CompanyMember;
@ -72,7 +81,7 @@ import com.liferay.util.bridges.mvc.MVCPortlet;
*/
public class Utils {
private static Log _log = LogFactoryUtil.getLog(Utils.class);
private static String SERVICE_ENDPOINT_CATEGORY = "DataAnalysis";
private static String SERVICE_ENDPOINT_NAME = "DataMiner";
public static final String ENCODED_FARM_PARAM = "ZmFybUlk";
@ -94,6 +103,133 @@ public class Utils {
private static UserManager UM = new LiferayUserManager();
private static RoleManager RM = new LiferayRoleManager();
private String currentUsername;
private String context;
private StorageHubWrapper shubWrapper;
private Workspace ws;
private PortletRequest request;
public Utils(String currentUsername, String context, PortletRequest request) {
super();
this.currentUsername = currentUsername;
this.context = context;
this.request = request;
String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(context, currentUsername);
SecurityTokenProvider.instance.set(authorizationToken);
ScopeProvider.instance.set(context);
HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));
JWTToken umaToken = null;
try {
umaToken = OIDCUmaUtil.getUMAToken(httpReq, currentUsername, context);
provideTokensInThreadlocal(umaToken);
} catch (Exception e) {
e.printStackTrace();
}
try {
shubWrapper = new StorageHubWrapper(context, authorizationToken);
ws = shubWrapper.getWorkspace();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void provideTokensInThreadlocal(JWTToken umaToken) {
_log.debug("Setting UMA token with jti in AccessToken provider: " + umaToken.getJti());
AccessTokenProvider.instance.set(JWTTokenUtil.getAccessTokenString(umaToken));
}
public Workspace getWS() {
return ws;
}
/**
* check whether the company folder exists and it creates it when not.
* @param currentUsername
* @param context
* @return
*/
public SharedFolder getWSCompanyFolder(GCubeTeam team, PortletRequest request) {
String username = currentUsername;
String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(context, username);
SecurityTokenProvider.instance.set(authorizationToken);
ScopeProvider.instance.set(context);
HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));
JWTToken umaToken = null;
try {
umaToken = OIDCUmaUtil.getUMAToken(httpReq, currentUsername, context);
provideTokensInThreadlocal(umaToken);
} catch (Exception e) {
e.printStackTrace();
}
SharedFolder folder = null;
String companyName = team.getTeamName();
try {
_log.info(companyName + " company folder exists, returning id and setting hidden?... " + PFISHConstants.HIDE_COMPANY_SHARED_FOLDER);
String name2Look = companyName+PFISHConstants.COMPANY_WS_FOLDER_SUFFIX;
StorageHubClient shClient = new StorageHubClient();
FolderContainer rootFolder = shClient.getWSRoot();
List<? extends Item> items = rootFolder.list().includeHidden().getItems();
for (Item item : items) {
if (item instanceof SharedFolder) {
System.out.println("folder name:" + item.getTitle());
if (item.getTitle().compareTo(name2Look) == 0) {
return (SharedFolder) item;
}
}
}
}
catch (Exception e) {
e.printStackTrace();
return null;
}
return folder;
}
/**
* check whether the farm folder exists and it creates it when not.
* @param currentUsername
* @param context
* @return
*/
public WorkspaceFolder getWSFarmFolder(GCubeTeam company, GCubeTeam farm) {
String username = currentUsername;
String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(context, username);
SecurityTokenProvider.instance.set(authorizationToken);
ScopeProvider.instance.set(context);
WorkspaceFolder folder = null;
String companyName = company.getTeamName();
try {
SharedFolder theCompanyFolder = getWSCompanyFolder(company, request);
String destinationFolderId = theCompanyFolder.getId();
String name = Long.toString(farm.getTeamId()); //the farm folder is the unique id of the farm
String description = new StringBuilder("Forms submitted by farm ")
.append(farm.getTeamName())
.append(" belonging to company ").append(companyName).toString();
if (!ws.exists(name, destinationFolderId)) {
_log.info(companyName + " farm folder does not exists, triggering creation ... ");
folder = ws.createFolder(name, description, destinationFolderId);
ws.setFolderAsHidden(folder.getId(), PFISHConstants.HIDE_COMPANY_SHARED_FOLDER);
_log.info("Farm folder created succesfully for " + companyName);
}
else {
_log.info(farm.getTeamName() + " farm folder exists, returning id ... ");
SecurityTokenProvider.instance.set(authorizationToken);
ScopeProvider.instance.set(context);
List<WorkspaceItem> folders = ws.find(name, destinationFolderId);
folder = (WorkspaceFolder) folders.get(0);
ws.setFolderAsHidden(folder.getId(), PFISHConstants.HIDE_COMPANY_SHARED_FOLDER);
_log.info(farm.getTeamName() + " farm folder id = " + folder.getId());
}
}
catch (Exception e) {
e.printStackTrace();
return null;
}
return folder;
}
/**
* For being a Company admin in PerformFISHKPIs VRE you should have the Site Role defined in COMPANY_ADMIN_SITE_ROLE constant.
@ -221,7 +357,7 @@ public class Utils {
return null;
} else
return associationChecker.get(0);
}
/**
@ -298,7 +434,7 @@ public class Utils {
return false;
}
public static List<GCubeTeam> getUserFarms(RenderRequest request, RenderResponse response, MVCPortlet instance) throws PortletException, IOException {
GCubeUser currentUser = getCurrentUser(request);
@ -686,113 +822,6 @@ public class Utils {
return false;
}
public static Workspace getWS(String currentUsername, String context) {
String username = currentUsername;
String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(context, username);
SecurityTokenProvider.instance.set(authorizationToken);
ScopeProvider.instance.set(context);
try {
StorageHubWrapper wrap = new StorageHubWrapper(context, authorizationToken);
return wrap.getWorkspace();
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* check whether the company folder exists and it creates it when not.
* @param currentUsername
* @param context
* @return
*/
public static synchronized WorkspaceFolder getWSCompanyFolder(String currentUsername, String context, GCubeTeam team) {
String username = currentUsername;
String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(context, username);
SecurityTokenProvider.instance.set(authorizationToken);
ScopeProvider.instance.set(context);
Workspace ws = null;
WorkspaceFolder folder = null;
String companyName = team.getTeamName();
try {
ws = getWS(username, context);
String destinationFolderId = ws.getRoot().getId();
String name = companyName+PFISHConstants.COMPANY_WS_FOLDER_SUFFIX;
String description = "Data belonging to " + companyName;
if (!ws.exists(name, destinationFolderId)) {
_log.info(companyName + " company folder does not exists, triggering creation ... ");
folder = ws.createFolder(name, description, destinationFolderId);
String folderId = folder.getId();
ws.setFolderAsHidden(folderId, PFISHConstants.HIDE_COMPANY_SHARED_FOLDER);
_log.info("Company folder created succesfully for " + companyName + " adding other company users (if any)");
Set<String> users = new HashSet<>();
for (GCubeUser member : UM.listUsersByTeam(team.getTeamId())) {
users.add(member.getUsername());
}
ws.shareFolder(folderId, users, ACLType.WRITE_ALL);
_log.info("company folder created ...");
}
else {
_log.info(companyName + " company folder exists, returning id and setting hidden?... " + PFISHConstants.HIDE_COMPANY_SHARED_FOLDER);
List<WorkspaceItem> folders = ws.find(name, destinationFolderId);
folder = (WorkspaceFolder) folders.get(0);
ws.setFolderAsHidden(folder.getId(), PFISHConstants.HIDE_COMPANY_SHARED_FOLDER);
}
}
catch (Exception e) {
e.printStackTrace();
return null;
}
return folder;
}
/**
* check whether the farm folder exists and it creates it when not.
* @param currentUsername
* @param context
* @return
*/
public static synchronized WorkspaceFolder getWSFarmFolder(String currentUsername, String context, GCubeTeam company, GCubeTeam farm) {
String username = currentUsername;
String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(context, username);
SecurityTokenProvider.instance.set(authorizationToken);
ScopeProvider.instance.set(context);
Workspace ws = null;
WorkspaceFolder folder = null;
String companyName = company.getTeamName();
try {
WorkspaceFolder companyFolder = getWSCompanyFolder(currentUsername, context, company);
ws = getWS(currentUsername, context);
String destinationFolderId = companyFolder.getId();
String name = Long.toString(farm.getTeamId()); //the farm folder is the unique id of the farm
String description = new StringBuilder("Forms submitted by farm ")
.append(farm.getTeamName())
.append(" belonging to company ").append(companyName).toString();
if (!ws.exists(name, destinationFolderId)) {
_log.info(companyName + " farm folder does not exists, triggering creation ... ");
folder = ws.createFolder(name, description, destinationFolderId);
ws.setFolderAsHidden(folder.getId(), PFISHConstants.HIDE_COMPANY_SHARED_FOLDER);
_log.info("Farm folder created succesfully for " + companyName);
}
else {
_log.info(farm.getTeamName() + " farm folder exists, returning id ... ");
SecurityTokenProvider.instance.set(authorizationToken);
ScopeProvider.instance.set(context);
List<WorkspaceItem> folders = ws.find(name, destinationFolderId);
folder = (WorkspaceFolder) folders.get(0);
ws.setFolderAsHidden(folder.getId(), PFISHConstants.HIDE_COMPANY_SHARED_FOLDER);
_log.info(farm.getTeamName() + " farm folder id = " + folder.getId());
}
}
catch (Exception e) {
e.printStackTrace();
return null;
}
return folder;
}
public static GCubeUser getCurrentUser(RenderRequest request) {
long userId;
try {
@ -917,7 +946,7 @@ public class Utils {
return userToken;
}
public static String maskId(long idToMask) {
String toMask = ""+idToMask;
try {
@ -927,7 +956,7 @@ public class Utils {
}
return null;
}
public static String maskId(String toMask) {
try {
return URLEncoder.encode(Base64.getEncoder().encodeToString(toMask.getBytes("utf-8")), "UTF-8");
@ -936,13 +965,13 @@ public class Utils {
}
return null;
}
public static long unmaskId(String idToUnmask) {
String unmasked = new String(Base64.getDecoder().decode(idToUnmask));
return Long.parseLong(unmasked);
}
public static List<GCoreEndpoint> getAnalyticalToolkitServiceInstance(String context) throws Exception {
String currScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(context);
@ -954,7 +983,7 @@ public class Utils {
ScopeProvider.instance.set(currScope);
return toReturn;
}
public static String getAnalyticalToolkitEndpoint(String context) {
List<GCoreEndpoint> analyticalServices = null;
try {
@ -980,7 +1009,7 @@ public class Utils {
}
return null;
}
public static List<ServiceEndpoint> getDataMinerInstance(String scope) throws Exception {
String currScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
@ -1022,7 +1051,7 @@ public class Utils {
else
return PFISHConstants.SHOW_GROW_OUT;
}
public static String getBatchTypeName(String phase, String fileName) {
switch (phase) {
case PFISHConstants.SHOW_PRE_ONGROWING: