Skip to content
Snippets Groups Projects
Commit 0630a524 authored by ThanKarab's avatar ThanKarab
Browse files

Added DATASET_ALL claim and refactored.

parent 0f4d621b
No related branches found
No related tags found
1 merge request!9Dev dataset authorization
...@@ -17,6 +17,7 @@ import eu.hbp.mip.model.galaxy.GalaxyWorkflowResult; ...@@ -17,6 +17,7 @@ import eu.hbp.mip.model.galaxy.GalaxyWorkflowResult;
import eu.hbp.mip.model.galaxy.PostWorkflowToGalaxyDtoResponse; import eu.hbp.mip.model.galaxy.PostWorkflowToGalaxyDtoResponse;
import eu.hbp.mip.repositories.ExperimentRepository; import eu.hbp.mip.repositories.ExperimentRepository;
import eu.hbp.mip.repositories.ModelRepository; import eu.hbp.mip.repositories.ModelRepository;
import eu.hbp.mip.utils.ClaimUtils;
import eu.hbp.mip.utils.HTTPUtil; import eu.hbp.mip.utils.HTTPUtil;
import eu.hbp.mip.utils.UserActionLogging; import eu.hbp.mip.utils.UserActionLogging;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
...@@ -110,37 +111,39 @@ public class ExperimentApi { ...@@ -110,37 +111,39 @@ public class ExperimentApi {
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Run algorithm", "Running the algorithm..."); UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Run algorithm", "Running the algorithm...");
// --- Validating proper access rights on the datasets --- // --- Validating proper access rights on the datasets ---
List<String> userRoles = Arrays.asList(authentication.getAuthorities().toString().toLowerCase() List<String> userClaims = Arrays.asList(authentication.getAuthorities().toString().toLowerCase()
.replaceAll("[\\s+\\]\\[]","").split(",")); .replaceAll("[\\s+\\]\\[]", "").split(","));
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Authorities", userRoles.toString()); UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "User Claims", userClaims.toString());
// Getting the dataset from the experiment parameters // Don't check for dataset claims if "super" claim exists allowing everything
String experimentDatasets = null; if (!userClaims.contains(ClaimUtils.allDatasetsAllowedClaim())) {
for (AlgorithmExecutionParamDTO parameter : experimentExecutionDTO.getAlgorithms().get(0).getParameters()) { // Getting the dataset from the experiment parameters
if (parameter.getName().equals("dataset")) { String experimentDatasets = null;
experimentDatasets = parameter.getValue(); for (AlgorithmExecutionParamDTO parameter : experimentExecutionDTO.getAlgorithms().get(0).getParameters()) {
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Run algorithm", "Found the dataset parameter!"); if (parameter.getName().equals("dataset")) {
break; experimentDatasets = parameter.getValue();
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Run algorithm", "Found the dataset parameter!");
break;
}
} }
}
if (experimentDatasets == null || experimentDatasets.equals("")) {
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Run algorithm",
"A dataset should be specified when running an algorithm.");
return ResponseEntity.badRequest().body("A dataset should be specified when running an algorithm.");
}
for (String dataset : experimentDatasets.split(",")) { if (experimentDatasets == null || experimentDatasets.equals("")) {
String datasetRole = "role_" + dataset;
if (!userRoles.contains(datasetRole.toLowerCase())) {
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Run algorithm", UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Run algorithm",
"You are not allowed to use dataset: " + dataset); "A dataset should be specified when running an algorithm.");
return ResponseEntity.status(403).body("You are not allowed to use dataset: " + dataset); return ResponseEntity.badRequest().body("A dataset should be specified when running an algorithm.");
} }
}
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Run algorithm",
"User is authorized to use the datasets: " + experimentDatasets);
for (String dataset : experimentDatasets.split(",")) {
String datasetRole = ClaimUtils.getDatasetClaim(dataset);
if (!userClaims.contains(datasetRole.toLowerCase())) {
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Run algorithm",
"You are not allowed to use dataset: " + dataset);
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("You are not allowed to use dataset: " + dataset);
}
}
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Run algorithm",
"User is authorized to use the datasets: " + experimentDatasets);
}
// --- Run the experiment --- // --- Run the experiment ---
......
...@@ -9,6 +9,7 @@ import com.google.gson.reflect.TypeToken; ...@@ -9,6 +9,7 @@ import com.google.gson.reflect.TypeToken;
import eu.hbp.mip.model.PathologyDTO; import eu.hbp.mip.model.PathologyDTO;
import eu.hbp.mip.model.PathologyDTO.PathologyDatasetDTO; import eu.hbp.mip.model.PathologyDTO.PathologyDatasetDTO;
import eu.hbp.mip.model.UserInfo; import eu.hbp.mip.model.UserInfo;
import eu.hbp.mip.utils.ClaimUtils;
import eu.hbp.mip.utils.CustomResourceLoader; import eu.hbp.mip.utils.CustomResourceLoader;
import eu.hbp.mip.utils.UserActionLogging; import eu.hbp.mip.utils.UserActionLogging;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
...@@ -47,27 +48,30 @@ public class PathologiesApi { ...@@ -47,27 +48,30 @@ public class PathologiesApi {
public ResponseEntity<String> getPathologies(Authentication authentication) { public ResponseEntity<String> getPathologies(Authentication authentication) {
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Load all the pathologies", ""); UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Load all the pathologies", "");
// Load pathologies from file
Resource resource = resourceLoader.getResource("file:/opt/portal/api/pathologies.json"); Resource resource = resourceLoader.getResource("file:/opt/portal/api/pathologies.json");
List<PathologyDTO> allPathologies; List<PathologyDTO> allPathologies;
try { try {
allPathologies = gson.fromJson(convertInputStreamToString(resource.getInputStream()), new TypeToken<List<PathologyDTO>>() { allPathologies = gson.fromJson(convertInputStreamToString(resource.getInputStream()), new TypeToken<List<PathologyDTO>>() {
}.getType()); }.getType());
} catch (IOException e) { } catch (IOException e) {
return ResponseEntity.badRequest().body("{\"error\" : \"The pathologies.json file could not be read.\"}"); return ResponseEntity.badRequest().body("The pathologies.json file could not be read.");
} }
// --- Providing only the allowed pathologies/datasets to the user --- // --- Providing only the allowed pathologies/datasets to the user ---
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), UserActionLogging.LogUserAction(userInfo.getUser().getUsername(),
"Load all the pathologies", "Filter out the unauthorised datasets."); "Load all the pathologies", "Filter out the unauthorised datasets.");
List<String> userRoles = Arrays.asList(authentication.getAuthorities().toString().toLowerCase() List<String> userClaims = Arrays.asList(authentication.getAuthorities().toString().toLowerCase()
.replaceAll("[\\s+\\]\\[]","").split(",")); .replaceAll("[\\s+\\]\\[]", "").split(","));
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), UserActionLogging.LogUserAction(userInfo.getUser().getUsername(),
"Load all the pathologies", "Authorities : " + authentication.getAuthorities().toString()); "Load all the pathologies", "User Claims: " + userClaims);
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), // If the "dataset_all" claim exists then return everything
"Load all the pathologies", "Authorities: " + userRoles); if (userClaims.contains(ClaimUtils.allDatasetsAllowedClaim())) {
return ResponseEntity.ok().body(gson.toJson(allPathologies));
}
List<PathologyDTO> userPathologies = new ArrayList<>(); List<PathologyDTO> userPathologies = new ArrayList<>();
for (PathologyDTO curPathology : allPathologies) { for (PathologyDTO curPathology : allPathologies) {
...@@ -76,17 +80,14 @@ public class PathologiesApi { ...@@ -76,17 +80,14 @@ public class PathologiesApi {
List<PathologyDatasetDTO> userPathologyDatasets = new ArrayList<PathologyDatasetDTO>(); List<PathologyDatasetDTO> userPathologyDatasets = new ArrayList<PathologyDatasetDTO>();
for (PathologyDatasetDTO dataset : curPathology.getDatasets()) { for (PathologyDatasetDTO dataset : curPathology.getDatasets()) {
if(userRoles.contains("role_" + dataset.getCode())){ if (userClaims.contains(ClaimUtils.getDatasetClaim(dataset.getCode()))) {
userPathologyDatasets.add(dataset); userPathologyDatasets.add(dataset);
} }
} }
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), if (userPathologyDatasets.size() > 0) {
"Load all the pathologies", "User Pathologies size: " + userPathologyDatasets.size()); UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "Load all the pathologies",
"Added pathology '" + curPathology.getLabel() + " with datasets: '" + userPathologyDatasets + "'");
if(userPathologyDatasets.size() > 0){
UserActionLogging.LogUserAction(userInfo.getUser().getUsername(),
"Load all the pathologies", "Added the pathology");
PathologyDTO userPathology = new PathologyDTO(); PathologyDTO userPathology = new PathologyDTO();
userPathology.setCode(curPathology.getCode()); userPathology.setCode(curPathology.getCode());
......
package eu.hbp.mip.utils;
public class ClaimUtils {
public static String allDatasetsAllowedClaim(){
return "dataset_all";
}
public static String getDatasetClaim(String datasetCode){
return "dataset_" + datasetCode;
}
}
package eu.hbp.mip.utils;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by mirco on 01.07.16.
*/
public class JSONUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtil.class);
private JSONUtil() {
/* Hide implicit public constructor */
throw new IllegalAccessError("JSONUtil class");
}
public static boolean isJSONValid(String test) {
try {
new JsonParser().parse(test);
} catch (JsonParseException jpe)
{
LOGGER.trace("Cannot parse to json", jpe); // This is the normal behavior when the input string is not JSON-ified
return false;
}
return true;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment