-
Notifications
You must be signed in to change notification settings - Fork 7
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
RN-632: Show Data-Tables in the Admin-Panel #4175
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
}; |
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 }; | ||
} | ||
} |
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]; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code isn't the DRY-est... it's copy pasted from dataElements permissions stuff. It's a bit awkward to consolidate, but I could make more of an effort if the reviewer feels it's worth it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure no DRY is fine here |
||
|
||
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; | ||
}; |
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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we like this naming style? With the
-
? I kinda like it as it makes clear thatData-Tables
is a single concept, but it's not particularly convention with Tupaia to use-
...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get it sorry 😆
But it's just a label, - or no -, it's a + to me