Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: right managment for dataset and distribution #1070

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/packages/components/advanced-search/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { NumberResults } from '../number-results';

type AdvancedSearchListTypes = {
title: string;
data: any;
initializeState?: any;
redirect?: any;
data: unknown[];
initializeState?: unknown;
redirect?: JSX.Element;
};
export const AdvancedSearchList = ({
title,
Expand Down
2 changes: 1 addition & 1 deletion src/packages/model/Dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type Dataset = {
export type CatalogRecord = {
created: string;
updated: string;
contributor: string;
contributor: string | string[];
catalogRecord: CatalogRecord;
validationState: ValidationState;
};
Expand Down
4 changes: 2 additions & 2 deletions src/packages/modules-datasets/datasets/view/menu.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Dataset View Menu', () => {
it('an Gestionnaire_jeu_donnees_RMESGNCS can goBack, publish and update a dataset if the stamp is correct and validationState is unpublished', () => {
const dataset = {
validationState: 'Published',
catalogRecord: { contributor: 'INSEE' },
catalogRecord: { contributor: ['INSEE'] },
};
render(
<RBACMock roles={[DATASET_CONTRIBUTOR]} stamp="INSEE">
Expand All @@ -70,7 +70,7 @@ describe('Dataset View Menu', () => {
it('an Gestionnaire_jeu_donnees_RMESGNCS can only goBack if the stamp not is correct', () => {
const dataset = {
validationState: 'Published',
catalogRecord: { contributor: 'XXXXXX' },
catalogRecord: { contributor: ['XXXXXX'] },
};
render(
<RBACMock roles={[DATASET_CONTRIBUTOR]} stamp="INSEE">
Expand Down
11 changes: 7 additions & 4 deletions src/packages/modules-datasets/datasets/view/menu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ADMIN, DATASET_CONTRIBUTOR } from '../../../auth/roles';
import { ADMIN } from '../../../auth/roles';
import { ValidationButton } from '../../../components';
import { ActionToolbar } from '../../../components/action-toolbar';
import {
Expand All @@ -10,6 +10,7 @@ import { Dataset } from '../../../model/Dataset';
import { UNPUBLISHED } from '../../../model/ValidationState';
import { usePermission } from '../../../redux/hooks/usePermission';
import { useGoBack } from '../../../utils/hooks/useGoBack';
import { checkIfContributorContainsUserStamp } from '../../utils/check-stamp-with-contributor';

type ViewMenuTypes = {
dataset: Dataset;
Expand All @@ -25,9 +26,11 @@ export const ViewMenu = ({

const permission = usePermission();

const hasDatasetRightsBasedOnStamp =
permission?.stamp === dataset?.catalogRecord?.contributor &&
permission?.roles?.includes(DATASET_CONTRIBUTOR);
const hasDatasetRightsBasedOnStamp = checkIfContributorContainsUserStamp(
dataset,
permission,
);

const isAdmin = permission?.roles?.includes(ADMIN);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ describe('Distribution View Menu', () => {
it('an Gestionnaire_jeu_donnees_RMESGNCS can goBack, publish, delete and update a distribution if the stamp is correct and validationState is unpublished', () => {
const dataset = {
validationState: UNPUBLISHED,
catalogRecord: { contributor: 'INSEE' },
} as Dataset;
catalogRecord: { contributor: ['INSEE'] },
} as unknown as Dataset;
const distribution = {} as Distribution;

render(
Expand All @@ -74,7 +74,7 @@ describe('Distribution View Menu', () => {
it('an Gestionnaire_jeu_donnees_RMESGNCS can goBack, publish and update a distribution if the stamp is correct and validationState is published', () => {
const dataset = {
validationState: 'Published',
catalogRecord: { contributor: 'INSEE' },
catalogRecord: { contributor: ['INSEE'] },
} as unknown as Dataset;
const distribution = {} as Distribution;

Expand All @@ -98,7 +98,7 @@ describe('Distribution View Menu', () => {
it('an Gestionnaire_jeu_donnees_RMESGNCS can only goBack if the stamp not is correct', () => {
const dataset = {
validationState: 'Published',
catalogRecord: { contributor: 'XXXXXX' },
catalogRecord: { contributor: ['XXXXXX'] },
} as unknown as Dataset;
const distribution = {} as Distribution;

Expand Down
11 changes: 7 additions & 4 deletions src/packages/modules-datasets/distributions/view/menu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ADMIN, DATASET_CONTRIBUTOR } from '../../../auth/roles';
import { ADMIN } from '../../../auth/roles';
import { ValidationButton } from '../../../components';
import { ActionToolbar } from '../../../components/action-toolbar';
import {
Expand All @@ -10,6 +10,7 @@ import { Dataset, Distribution } from '../../../model/Dataset';
import { UNPUBLISHED } from '../../../model/ValidationState';
import { usePermission } from '../../../redux/hooks/usePermission';
import { useGoBack } from '../../../utils/hooks/useGoBack';
import { checkIfContributorContainsUserStamp } from '../../utils/check-stamp-with-contributor';

type ViewMenuTypes = {
distribution: Distribution;
Expand All @@ -28,9 +29,11 @@ export const ViewMenu = ({

const permission = usePermission();

const hasDatasetRightsBasedOnStamp =
permission?.stamp === dataset?.catalogRecord?.contributor &&
permission?.roles?.includes(DATASET_CONTRIBUTOR);
const hasDatasetRightsBasedOnStamp = checkIfContributorContainsUserStamp(
dataset,
permission,
);

const isAdmin = permission?.roles?.includes(ADMIN);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { describe, it, expect } from 'vitest';
import { checkIfContributorContainsUserStamp } from './check-stamp-with-contributor';
import { DATASET_CONTRIBUTOR } from '../../auth/roles';
import { Dataset } from '../../model/Dataset';
import { Permission } from '../../redux/selectors';

describe('checkIfContributorContainsUserStamp', () => {
it('should return true if the user stamp is in contributors and the role is correct', () => {
const dataset: Dataset = {
catalogRecord: {
contributor: ['userStamp1', 'userStamp2'],
},
} as Dataset;
const permission: Permission = {
stamp: 'userStamp1',
roles: [DATASET_CONTRIBUTOR],
} as Permission;

expect(checkIfContributorContainsUserStamp(dataset, permission)).toBe(true);
});

it('should return false if the user stamp is not in contributors', () => {
const dataset: Dataset = {
catalogRecord: {
contributor: ['userStamp2', 'userStamp3'],
},
} as Dataset;
const permission: Permission = {
stamp: 'userStamp1',
roles: [DATASET_CONTRIBUTOR],
} as Permission;

expect(checkIfContributorContainsUserStamp(dataset, permission)).toBe(
false,
);
});

it('should return false if the user role does not include DATASET_CONTRIBUTOR', () => {
const dataset: Dataset = {
catalogRecord: {
contributor: ['userStamp1', 'userStamp2'],
},
} as Dataset;
const permission: Permission = {
stamp: 'userStamp1',
roles: ['ANOTHER_ROLE'],
} as Permission;

expect(checkIfContributorContainsUserStamp(dataset, permission)).toBe(
false,
);
});

it('should correctly handle a single non-array contributor', () => {
const dataset: Dataset = {
catalogRecord: {
contributor: 'userStamp1',
},
} as Dataset;
const permission: Permission = {
stamp: 'userStamp1',
roles: [DATASET_CONTRIBUTOR],
} as Permission;

expect(checkIfContributorContainsUserStamp(dataset, permission)).toBe(true);
});

it('should return false if the dataset or permission is undefined', () => {
expect(
checkIfContributorContainsUserStamp(
undefined as unknown as Dataset,
undefined as unknown as Permission,
),
).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { DATASET_CONTRIBUTOR } from '../../auth/roles';
import { Dataset } from '../../model/Dataset';
import { Permission } from '../../redux/selectors';

export const checkIfContributorContainsUserStamp = (
dataset: Dataset,
permission: Permission,
) => {
const contributors = Array.isArray(dataset?.catalogRecord?.contributor)
? dataset?.catalogRecord?.contributor
: [dataset?.catalogRecord?.contributor];

return !!(
contributors.find((c) => c === permission?.stamp) &&
permission?.roles?.includes(DATASET_CONTRIBUTOR)
);
};
5 changes: 5 additions & 0 deletions src/packages/redux/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ReduxModel } from './model';

export type Permission = {
authType: string;
roles: string[];
stamp: string;
}
export const getPermission = (state: ReduxModel) => {
const {
type: authType,
Expand Down
Loading