chore: add intersect method from backend to fontend for upload extensions (#18811)

* chore: add intersect method from be to font end

* fix lint

* add suggestion

* fix python test

* run precommit

* fix pytlint

* update changes from masters
This commit is contained in:
Phillip Kelley-Dotson 2022-02-24 17:42:55 -08:00 committed by GitHub
parent 420a63f343
commit 8d38675cbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 15 deletions

View File

@ -23,6 +23,7 @@ import {
getPasswordsNeeded,
getAlreadyExists,
hasTerminalValidation,
checkUploadExtensions,
} from 'src/views/CRUD/utils';
const terminalErrors = {
@ -186,3 +187,19 @@ test('successfully modified rison to encode correctly', () => {
expect(rison.decode(actualEncoding)).toEqual(testObject);
});
});
test('checkUploadExtenssions should return valid upload extensions', () => {
const uploadExtensionTest = ['a', 'b', 'c'];
const randomExtension = ['a', 'c'];
const randomExtensionTwo = ['c'];
const randomExtensionThree: Array<any> = [];
expect(
checkUploadExtensions(randomExtension, uploadExtensionTest),
).toBeTruthy();
expect(
checkUploadExtensions(randomExtensionTwo, uploadExtensionTest),
).toBeTruthy();
expect(
checkUploadExtensions(randomExtensionThree, uploadExtensionTest),
).toBeFalsy();
});

View File

@ -27,6 +27,7 @@ import {
css,
} from '@superset-ui/core';
import Chart from 'src/types/Chart';
import { intersection } from 'lodash';
import rison from 'rison';
import { getClientErrorObject } from 'src/utils/getClientErrorObject';
import { FetchDataConfig } from 'src/components/ListView';
@ -409,3 +410,14 @@ export const hasTerminalValidation = (errors: Record<string, any>[]) =>
isNeedsPassword(payload) || isAlreadyExists(payload),
),
);
export const checkUploadExtensions = (
perm: Array<any> | string | undefined | boolean,
cons: Array<any>,
) => {
if (perm !== undefined) {
if (typeof perm === 'boolean') return perm;
return intersection(perm, cons).length;
}
return false;
};

View File

@ -79,7 +79,7 @@ interface MenuObjectChildProps {
index?: number;
url?: string;
isFrontendRoute?: boolean;
perm?: string | boolean;
perm?: string | Array<any> | boolean;
view?: string;
}

View File

@ -26,6 +26,7 @@ import { useSelector } from 'react-redux';
import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
import LanguagePicker from './LanguagePicker';
import DatabaseModal from '../CRUD/data/database/DatabaseModal';
import { checkUploadExtensions } from '../CRUD/utils';
import {
ExtentionConfigs,
GlobalMenuDataOptions,
@ -75,6 +76,7 @@ const RightMenu = ({
CSV_EXTENSIONS,
COLUMNAR_EXTENSIONS,
EXCEL_EXTENSIONS,
ALLOWED_EXTENSIONS,
HAS_GSHEETS_INSTALLED,
} = useSelector<any, ExtentionConfigs>(state => state.common.conf);
@ -188,7 +190,9 @@ const RightMenu = ({
title={menuIconAndLabel(menu)}
>
{menu.childs.map((item, idx) =>
typeof item !== 'string' && item.name && item.perm ? (
typeof item !== 'string' &&
item.name &&
checkUploadExtensions(item.perm, ALLOWED_EXTENSIONS) ? (
<>
{idx === 2 && <Menu.Divider />}
<Menu.Item key={item.name}>

View File

@ -20,9 +20,10 @@
import { NavBarProps, MenuObjectProps } from './Menu';
export interface ExtentionConfigs {
CSV_EXTENSIONS: boolean;
COLUMNAR_EXTENSIONS: boolean;
EXCEL_EXTENSIONS: boolean;
ALLOWED_EXTENSIONS: Array<any>;
CSV_EXTENSIONS: Array<any>;
COLUMNAR_EXTENSIONS: Array<any>;
EXCEL_EXTENSIONS: Array<any>;
HAS_GSHEETS_INSTALLED: boolean;
}
export interface RightMenuProps {

View File

@ -104,6 +104,10 @@ FRONTEND_CONF_KEYS = (
"GLOBAL_ASYNC_QUERIES_WEBSOCKET_URL",
"DASHBOARD_AUTO_REFRESH_MODE",
"SCHEDULED_QUERIES",
"EXCEL_EXTENSIONS",
"CSV_EXTENSIONS",
"COLUMNAR_EXTENSIONS",
"ALLOWED_EXTENSIONS",
)
logger = logging.getLogger(__name__)
@ -347,16 +351,10 @@ def common_bootstrap_payload() -> Dict[str, Any]:
locale = str(get_locale())
# should not expose API TOKEN to frontend
frontend_config = {k: conf.get(k) for k in FRONTEND_CONF_KEYS}
frontend_config["EXCEL_EXTENSIONS"] = bool(
bool(conf["EXCEL_EXTENSIONS"].intersection(conf["ALLOWED_EXTENSIONS"])),
)
frontend_config["CSV_EXTENSIONS"] = bool(
bool(conf["CSV_EXTENSIONS"].intersection(conf["ALLOWED_EXTENSIONS"])),
)
frontend_config["COLUMNAR_EXTENSIONS"] = bool(
bool(conf["COLUMNAR_EXTENSIONS"].intersection(conf["ALLOWED_EXTENSIONS"])),
)
frontend_config = {
k: (list(conf.get(k)) if isinstance(conf.get(k), set) else conf.get(k))
for k in FRONTEND_CONF_KEYS
}
if conf.get("SLACK_API_TOKEN"):
frontend_config["ALERT_REPORTS_NOTIFICATION_METHODS"] = [