-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
packages/admin-panel/src/pages/resources/DataTablesPage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2021 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { ResourcePage } from './ResourcePage'; | ||
|
||
const DATA_TABLES_ENDPOINT = 'dataTables'; | ||
|
||
const FIELDS = [ | ||
{ | ||
Header: 'Code', | ||
source: 'code', | ||
type: 'tooltip', | ||
}, | ||
{ | ||
Header: 'Description', | ||
source: 'description', | ||
}, | ||
{ | ||
Header: 'Type', | ||
source: 'type', | ||
}, | ||
{ | ||
Header: 'Config', | ||
source: 'config', | ||
type: 'jsonTooltip', | ||
editConfig: { type: 'jsonEditor' }, | ||
}, | ||
{ | ||
Header: 'Permission groups', | ||
source: 'permission_groups', | ||
type: 'tooltip', | ||
editConfig: { | ||
type: 'jsonArray', | ||
}, | ||
}, | ||
]; | ||
|
||
const COLUMNS = [...FIELDS]; | ||
|
||
export const DataTablesPage = ({ getHeaderEl }) => ( | ||
<ResourcePage | ||
title="Data-Tables" | ||
endpoint={DATA_TABLES_ENDPOINT} | ||
columns={COLUMNS} | ||
getHeaderEl={getHeaderEl} | ||
/> | ||
); | ||
|
||
DataTablesPage.propTypes = { | ||
getHeaderEl: PropTypes.func.isRequired, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/central-server/src/apiV2/dataTables/GETDataTables.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { GETHandler } from '../GETHandler'; | ||
import { | ||
assertDataTableGETPermissions, | ||
createDataTableDBFilter, | ||
} from './assertDataTablePermissions'; | ||
import { assertAnyPermissions, assertBESAdminAccess } from '../../permissions'; | ||
|
||
export class GETDataTables extends GETHandler { | ||
permissionsFilteredInternally = true; | ||
|
||
async findSingleRecord(dataTableId, options) { | ||
const dataGroupPermissionChecker = accessPolicy => | ||
assertDataTableGETPermissions(accessPolicy, this.models, dataTableId); | ||
await this.assertPermissions( | ||
assertAnyPermissions([assertBESAdminAccess, dataGroupPermissionChecker]), | ||
); | ||
|
||
return super.findSingleRecord(dataTableId, options); | ||
} | ||
|
||
async getPermissionsFilter(criteria, options) { | ||
const dbConditions = await createDataTableDBFilter(this.accessPolicy, this.models, criteria); | ||
return { dbConditions, dbOptions: options }; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/central-server/src/apiV2/dataTables/assertDataTablePermissions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2021 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { hasBESAdminAccess } from '../../permissions'; | ||
|
||
const getPermissionListWithWildcard = async accessPolicy => { | ||
const userPermissionGroups = accessPolicy.getPermissionGroups(); | ||
return ['*', ...userPermissionGroups]; | ||
}; | ||
|
||
export const assertDataTableGETPermissions = async (accessPolicy, models, dataTableId) => { | ||
// User requires access to any permission group | ||
if (await assertDataTablePermissions(accessPolicy, models, dataTableId, 'some')) { | ||
return true; | ||
} | ||
throw new Error('You do not have permission to view this data-table'); | ||
}; | ||
|
||
export const assertDataTableEditPermissions = async (accessPolicy, models, dataTableId) => { | ||
// User requires access to all permission groups | ||
if (await assertDataTablePermissions(accessPolicy, models, dataTableId, 'every')) { | ||
return true; | ||
} | ||
throw new Error( | ||
'You require access to all of a data-tables permission groups to perform this action', | ||
); | ||
}; | ||
|
||
const assertDataTablePermissions = async (accessPolicy, models, dataTableId, test) => { | ||
const dataTable = await models.dataTable.findById(dataTableId); | ||
if (!dataTable) { | ||
throw new Error(`No data-table exists with id ${dataTableId}`); | ||
} | ||
const userPermissions = await getPermissionListWithWildcard(accessPolicy); | ||
// Test if user has access to any or all permission groups against the data-table | ||
if ( | ||
dataTable.permission_groups.length <= 0 || | ||
dataTable.permission_groups[test](code => userPermissions.includes(code)) | ||
) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
export const createDataTableDBFilter = async (accessPolicy, models, criteria) => { | ||
if (hasBESAdminAccess(accessPolicy)) { | ||
return criteria; | ||
} | ||
const dbConditions = { ...criteria }; | ||
const userPermissions = await getPermissionListWithWildcard(accessPolicy); | ||
|
||
// Permission groups on the data-table overlap with our permission groups | ||
// Wildcard is added to our list so it will be included | ||
dbConditions.permission_groups = { | ||
comparator: '&&', // Checks two array have any elements in common | ||
comparisonValue: userPermissions, | ||
}; | ||
return dbConditions; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2022 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
export { GETDataTables } from './GETDataTables'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters