Skip to content

Commit

Permalink
chore: add intersect method from backend to fontend for upload extens…
Browse files Browse the repository at this point in the history
…ions (#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

(cherry picked from commit 8d38675)
  • Loading branch information
pkdotson authored and villebro committed Apr 3, 2022
1 parent 420a63f commit e20788c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
17 changes: 17 additions & 0 deletions superset-frontend/src/views/CRUD/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getPasswordsNeeded,
getAlreadyExists,
hasTerminalValidation,
checkUploadExtensions,
} from 'src/views/CRUD/utils';

const terminalErrors = {
Expand Down Expand Up @@ -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();
});
12 changes: 12 additions & 0 deletions superset-frontend/src/views/CRUD/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
};
2 changes: 1 addition & 1 deletion superset-frontend/src/views/components/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ interface MenuObjectChildProps {
index?: number;
url?: string;
isFrontendRoute?: boolean;
perm?: string | boolean;
perm?: string | Array<any> | boolean;
view?: string;
}

Expand Down
6 changes: 5 additions & 1 deletion superset-frontend/src/views/components/MenuRight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -75,6 +76,7 @@ const RightMenu = ({
CSV_EXTENSIONS,
COLUMNAR_EXTENSIONS,
EXCEL_EXTENSIONS,
ALLOWED_EXTENSIONS,
HAS_GSHEETS_INSTALLED,
} = useSelector<any, ExtentionConfigs>(state => state.common.conf);

Expand Down Expand Up @@ -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}>
Expand Down
7 changes: 4 additions & 3 deletions superset-frontend/src/views/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 8 additions & 10 deletions superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
"GLOBAL_ASYNC_QUERIES_WEBSOCKET_URL",
"DASHBOARD_AUTO_REFRESH_MODE",
"SCHEDULED_QUERIES",
"EXCEL_EXTENSIONS",
"CSV_EXTENSIONS",
"COLUMNAR_EXTENSIONS",
"ALLOWED_EXTENSIONS",
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -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"] = [
Expand Down

0 comments on commit e20788c

Please sign in to comment.