handle legacy operators in permissions builder (fix #268) (#270)

This commit is contained in:
Rikin Kachhia 2018-08-08 11:41:00 +05:30 committed by Shahidh K Muhammed
parent 8ecb80d2da
commit 816b480df3
4 changed files with 61 additions and 14 deletions

View File

@ -52,6 +52,22 @@ class PermissionBuilder extends React.Component {
return _tableSchemas;
};
const isNotOperator = value => {
return value === boolOperators.not;
};
const isAndOrOperator = value => {
return value === boolOperators.or || value === boolOperators.and;
};
const isArrayColumnOperator = value => {
return arrayColumnOperators.indexOf(value) !== -1;
};
const isColumnOperator = value => {
return columnOperators.indexOf(value) !== -1;
};
const getFilter = (conditions, prefix, value = '') => {
const _where = {};
@ -59,7 +75,7 @@ class PermissionBuilder extends React.Component {
const operation = prefixSplit[0];
if (prefixSplit.length !== 1) {
if (operation === boolOperators.or || operation === boolOperators.and) {
if (isAndOrOperator(operation)) {
const position = parseInt(prefixSplit[1], 10);
_where[operation] = conditions[operation];
_where[operation][position] = getFilter(
@ -70,13 +86,13 @@ class PermissionBuilder extends React.Component {
if (Object.keys(_where[operation][position]).length === 0) {
_where[operation].splice(position, 1);
}
} else if (operation === boolOperators.not) {
} else if (isNotOperator(operation)) {
_where[operation] = getFilter(
conditions[operation],
prefixSplit.slice(1).join('.'),
value
);
} else if (arrayColumnOperators.indexOf(operation) !== -1) {
} else if (isArrayColumnOperator(operation)) {
const position = parseInt(prefixSplit[1], 10);
_where[operation] = conditions[operation] || [];
if (value) {
@ -95,16 +111,13 @@ class PermissionBuilder extends React.Component {
} else {
if (operation === '--') {
// blank where
} else if (
operation === boolOperators.or ||
operation === boolOperators.and
) {
} else if (isAndOrOperator(operation)) {
_where[operation] = [];
} else if (operation === boolOperators.not) {
} else if (isNotOperator(operation)) {
_where[operation] = {};
} else if (arrayColumnOperators.indexOf(operation) !== -1) {
} else if (isArrayColumnOperator(operation)) {
_where[operation] = value || [];
} else if (columnOperators.indexOf(operation) !== -1) {
} else if (isColumnOperator(operation)) {
_where[operation] = value;
// if (operation === '$eq') {
// _where = value
@ -261,7 +274,7 @@ class PermissionBuilder extends React.Component {
let valueInput = '';
if (operator) {
if (arrayColumnOperators.indexOf(operator) !== -1) {
if (isArrayColumnOperator(operator)) {
valueInput = renderInputArray(
dispatchFunc,
operationValue,
@ -402,7 +415,7 @@ class PermissionBuilder extends React.Component {
let boolExpValue = null;
if (operation) {
const newPrefix = addToPrefix(prefix, operation);
if (operation === boolOperators.or || operation === boolOperators.and) {
if (isAndOrOperator(operation)) {
boolExpValue = renderBoolExpArray(
dispatchFunc,
condition[operation],
@ -410,7 +423,7 @@ class PermissionBuilder extends React.Component {
tableSchemas,
newPrefix
);
} else if (operation === boolOperators.not) {
} else if (isNotOperator(operation)) {
boolExpValue = renderBoolExp(
dispatchFunc,
condition[operation],

View File

@ -21,6 +21,24 @@ export const columnOperators = [
export const arrayColumnOperators = ['_in', '_nin'];
export const legacyOperatorsMap = {
$and: '_and',
$or: '_or',
$not: '_not',
$eq: '_eq',
$ne: '_ne',
$in: '_in',
$nin: '_nin',
$gt: '_gt',
$lt: '_lt',
$gte: '_gte',
$lte: '_lte',
$like: '_like',
$nlike: '_nlike',
$similar: '_similar',
$nsimilar: '_nsimilar',
};
export function addToPrefix(prefix, value) {
let _newPrefix;
if (prefix !== null && prefix.toString()) {

View File

@ -28,7 +28,8 @@ import PermissionBuilder from './PermissionBuilder/PermissionBuilder';
import TableHeader from '../TableCommon/TableHeader';
import ViewHeader from '../TableBrowseRows/ViewHeader';
import { setTable } from '../DataActions';
import { getIngForm } from '../utils';
import { getIngForm, escapeRegExp } from '../utils';
import { legacyOperatorsMap } from './PermissionBuilder/utils';
class Permissions extends Component {
componentDidMount() {
@ -670,6 +671,16 @@ class Permissions extends Component {
filterString = JSON.stringify(permsState[query][filterKey]);
}
// replace legacy operator values
Object.keys(legacyOperatorsMap).forEach(legacyOperator => {
const legacyString = '"' + legacyOperator + '"';
const currentString = '"' + legacyOperatorsMap[legacyOperator] + '"';
filterString = filterString.replace(
new RegExp(escapeRegExp(legacyString), 'g'),
currentString
);
});
return (
<div className={styles.editPermissionsSection}>
Allow {getIngForm(permsState.query)} <b>rows</b> for role '

View File

@ -118,10 +118,15 @@ const getEdForm = string => {
);
};
const escapeRegExp = string => {
return string.replace(/([.*+?^${}()|[\]\\])/g, '\\$1');
};
export {
ordinalColSort,
findTableFromRel,
findAllFromRel,
getEdForm,
getIngForm,
escapeRegExp,
};