Skip to content

Commit

Permalink
fix: [PROD-12954] update dataset parts security when scenario access …
Browse files Browse the repository at this point in the history
…is changed
  • Loading branch information
csm-thu committed Jan 19, 2024
1 parent d2bb5df commit 5273c0c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/state/commons/DatasetConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
export const DATASET_ACTIONS_KEY = {
GET_ALL_DATASETS: 'GET_ALL_DATASETS',
SET_ALL_DATASETS: 'SET_ALL_DATASETS',
SET_DATASET_SECURITY: 'SET_DATASET_SECURITY',
ADD_DATASET: 'ADD_DATASET',
};
16 changes: 16 additions & 0 deletions src/state/reducers/dataset/DatasetReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DATASET_ACTIONS_KEY } from '../../commons/DatasetConstants';
import { STATUSES } from '../../commons/Constants';
import { createReducer } from '@reduxjs/toolkit';
import { combineReducers } from 'redux';
import { DatasetsUtils } from '../../../utils';

export const datasetListInitialState = {
data: [],
Expand All @@ -23,6 +24,21 @@ export const datasetListReducer = createReducer(datasetListInitialState, (builde
.addCase(DATASET_ACTIONS_KEY.ADD_DATASET, (state, action) => {
delete action.type;
state.data.push(action);
})
.addCase(DATASET_ACTIONS_KEY.SET_DATASET_SECURITY, (state, action) => {
state.data = state.data?.map((datasetData) => {
if (datasetData.id === action.datasetId) {
const datasetWithNewSecurity = { ...datasetData, security: action.security };
DatasetsUtils.patchDatasetWithCurrentUserPermissions(
datasetWithNewSecurity,
action.userEmail,
action.userId,
action.permissionsMapping
);
return { ...datasetData, security: datasetWithNewSecurity.security };
}
return datasetData;
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,40 @@
// Licensed under the MIT licence.

import { takeEvery, select, call, put } from 'redux-saga/effects';
import { DATASET_ACTIONS_KEY } from '../../../commons/DatasetConstants';
import { SCENARIO_ACTIONS_KEY } from '../../../commons/ScenarioConstants';
import { t } from 'i18next';
import { DATASET_ID_VARTYPE } from '../../../../services/config/ApiConstants';
import { dispatchSetApplicationErrorMessage } from '../../../dispatchers/app/ApplicationDispatcher';
import DatasetService from '../../../../services/dataset/DatasetService';
import ScenarioService from '../../../../services/scenario/ScenarioService';
import { SecurityUtils } from '../../../../utils';

// TODO: replace by data from redux when dataset roles-permissions mapping is added in back-end /permissions endpoint
const DATASET_PERMISSIONS_MAPPING = {
viewer: ['read', 'read_security'],
editor: ['read', 'read_security', 'write'],
admin: ['read', 'read_security', 'write', 'write_security', 'delete'],
};

const getUserEmail = (state) => state.auth.userEmail;
const getUserId = (state) => state.auth.userId;
const getScenariosPermissionsMapping = (state) => state.application.permissionsMapping.scenario;
const getOrganizationId = (state) => state.organization.current.data.id;
const getWorkspaceId = (state) => state.workspace.current.data.id;
const getCurrentScenarioSecurity = (state) => state.scenario.current?.data?.security;
const getCurrentScenario = (state) => state.scenario.current?.data;
const getDatasets = (state) => state.dataset.list?.data;
const getSolutionParameters = (state) => state?.solution?.current?.data?.parameters ?? [];

export function* applyScenarioSharingChanges(action) {
try {
const organizationId = yield select(getOrganizationId);
const workspaceId = yield select(getWorkspaceId);
const currentScenarioSecurity = yield select(getCurrentScenarioSecurity);
const datasets = yield select(getDatasets);
const userEmail = yield select(getUserEmail);
const userId = yield select(getUserId);
const currentScenario = yield select(getCurrentScenario);
const currentScenarioSecurity = currentScenario?.security;
const { scenarioId, newScenarioSecurity } = action;

const mustUpdateSecurity = yield call(
Expand All @@ -30,9 +47,37 @@ export function* applyScenarioSharingChanges(action) {
newScenarioSecurity
);

const solutionParameters = yield select(getSolutionParameters);
const defaultDatasetsIds = solutionParameters
.filter((parameter) => parameter.varType === DATASET_ID_VARTYPE && parameter.defaultValue != null)
.map((parameter) => parameter.defaultValue);

const scenarioDatasetsIds = (currentScenario.parametersValues ?? [])
.filter((value) => value.varType === DATASET_ID_VARTYPE)
.map((dataset) => dataset.value);
for (const datasetId of scenarioDatasetsIds) {
if (defaultDatasetsIds.includes(datasetId)) continue; // Do not update access to common "default datasets"

const dataset = datasets.find((el) => el.id === datasetId);
if (!dataset) {
console.warn(`Unable to find dataset part ${datasetId}, you may lack "read" permission on this dataset.`);
continue;
}

const newDatasetSecurity = SecurityUtils.forgeDatasetSecurityFromScenarioSecurity(newScenarioSecurity);
yield call(DatasetService.updateSecurity, organizationId, datasetId, dataset.security, newDatasetSecurity);

yield put({
type: DATASET_ACTIONS_KEY.SET_DATASET_SECURITY,
datasetId,
security: newDatasetSecurity,
userEmail,
userId,
permissionsMapping: DATASET_PERMISSIONS_MAPPING,
});
}

if (mustUpdateSecurity) {
const userEmail = yield select(getUserEmail);
const userId = yield select(getUserId);
const scenariosPermissionsMapping = yield select(getScenariosPermissionsMapping);
yield put({
type: SCENARIO_ACTIONS_KEY.SET_SCENARIO_SECURITY,
Expand Down

0 comments on commit 5273c0c

Please sign in to comment.