Skip to content

Commit

Permalink
[data view management] Fix set default data view permissions check (e…
Browse files Browse the repository at this point in the history
…lastic#124897)

* fix set default data view permissions

* fix fields table

* fix scripted field add button

* fix jest tests

* lint fixes

* fix test

* updte snapshot
  • Loading branch information
mattkime authored Feb 9, 2022
1 parent 67cd496 commit ded0478
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ export const CreateEditField = withRouter(
if (spec) {
return (
<>
<IndexHeader indexPattern={indexPattern} defaultIndex={uiSettings.get('defaultIndex')} />
<IndexHeader
indexPattern={indexPattern}
defaultIndex={uiSettings.get('defaultIndex')}
canSave={dataViews.getCanSaveSync()}
/>
<EuiSpacer size={'l'} />
<FieldEditor
indexPattern={indexPattern}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const securitySolution = 'security-solution';

export const EditIndexPattern = withRouter(
({ indexPattern, history, location }: EditIndexPatternProps) => {
const { application, uiSettings, overlays, chrome, dataViews } =
const { uiSettings, overlays, chrome, dataViews } =
useKibana<IndexPatternManagmentContext>().services;
const [fields, setFields] = useState<DataViewField[]>(indexPattern.getNonScriptedFields());
const [conflictedFields, setConflictedFields] = useState<DataViewField[]>(
Expand Down Expand Up @@ -143,15 +143,16 @@ export const EditIndexPattern = withRouter(
const showTagsSection = Boolean(indexPattern.timeFieldName || (tags && tags.length > 0));
const kibana = useKibana();
const docsUrl = kibana.services.docLinks!.links.elasticsearch.mapping;
const userEditPermission = !!application?.capabilities?.indexPatterns?.save;
const userEditPermission = dataViews.getCanSaveSync();

return (
<div data-test-subj="editIndexPattern" role="region" aria-label={headingAriaLabel}>
<IndexHeader
indexPattern={indexPattern}
setDefault={setDefaultPattern}
{...(userEditPermission ? { deleteIndexPatternClick: removePattern } : {})}
deleteIndexPatternClick={removePattern}
defaultIndex={defaultIndex}
canSave={userEditPermission}
>
{showTagsSection && (
<EuiFlexGroup wrap gutterSize="s">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface IndexHeaderProps {
defaultIndex?: string;
setDefault?: () => void;
deleteIndexPatternClick?: () => void;
canSave: boolean;
}

const setDefaultAriaLabel = i18n.translate('indexPatternManagement.editDataView.setDefaultAria', {
Expand All @@ -40,12 +41,13 @@ export const IndexHeader: React.FC<IndexHeaderProps> = ({
setDefault,
deleteIndexPatternClick,
children,
canSave,
}) => {
return (
<EuiPageHeader
pageTitle={<span data-test-subj="indexPatternTitle">{indexPattern.title}</span>}
rightSideItems={[
defaultIndex !== indexPattern.id && setDefault && (
defaultIndex !== indexPattern.id && setDefault && canSave && (
<EuiToolTip content={setDefaultTooltip}>
<EuiButtonIcon
color="text"
Expand All @@ -56,7 +58,7 @@ export const IndexHeader: React.FC<IndexHeaderProps> = ({
/>
</EuiToolTip>
),
deleteIndexPatternClick && (
canSave && (
<EuiToolTip content={removeTooltip}>
<EuiButtonIcon
color="danger"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ const fields = [
},
].map(mockFieldToIndexPatternField);

const mockedServices = {
userEditPermission: false,
openModal: () => ({ onClose: new Promise<void>(() => {}), close: async () => {} }),
theme: {} as any,
};

describe('IndexedFieldsTable', () => {
test('should render normally', async () => {
const component: ShallowWrapper<any, Readonly<{}>, React.Component<{}, {}, any>> = shallow(
Expand All @@ -110,8 +116,9 @@ describe('IndexedFieldsTable', () => {
indexedFieldTypeFilter={[]}
schemaFieldTypeFilter={[]}
fieldFilter=""
{...mockedServices}
/>
).dive();
);

await new Promise((resolve) => process.nextTick(resolve));
component.update();
Expand All @@ -131,8 +138,9 @@ describe('IndexedFieldsTable', () => {
indexedFieldTypeFilter={[]}
schemaFieldTypeFilter={[]}
fieldFilter=""
{...mockedServices}
/>
).dive();
);

await new Promise((resolve) => process.nextTick(resolve));
component.setProps({ fieldFilter: 'Elast' });
Expand All @@ -153,8 +161,9 @@ describe('IndexedFieldsTable', () => {
indexedFieldTypeFilter={[]}
schemaFieldTypeFilter={[]}
fieldFilter=""
{...mockedServices}
/>
).dive();
);

await new Promise((resolve) => process.nextTick(resolve));
component.setProps({ indexedFieldTypeFilter: ['date'] });
Expand All @@ -175,8 +184,9 @@ describe('IndexedFieldsTable', () => {
indexedFieldTypeFilter={[]}
schemaFieldTypeFilter={[]}
fieldFilter=""
{...mockedServices}
/>
).dive();
);

await new Promise((resolve) => process.nextTick(resolve));
component.setProps({ schemaFieldTypeFilter: ['runtime'] });
Expand All @@ -198,8 +208,9 @@ describe('IndexedFieldsTable', () => {
indexedFieldTypeFilter={[]}
schemaFieldTypeFilter={[]}
fieldFilter=""
{...mockedServices}
/>
).dive();
);

await new Promise((resolve) => process.nextTick(resolve));
component.update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import React, { Component } from 'react';
import { createSelector } from 'reselect';
import { OverlayStart, ThemeServiceStart } from 'src/core/public';
import { DataViewField, DataView } from '../../../../../../plugins/data_views/public';
import { useKibana } from '../../../../../../plugins/kibana_react/public';
import { Table } from './components/table';
import { IndexedFieldItem } from './types';
import { IndexPatternManagmentContext } from '../../../types';

interface IndexedFieldsTableProps {
fields: DataViewField[];
Expand All @@ -36,16 +34,10 @@ interface IndexedFieldsTableState {
fields: IndexedFieldItem[];
}

const withHooks = (Comp: typeof Component) => {
return (props: any) => {
const { application } = useKibana<IndexPatternManagmentContext>().services;
const userEditPermission = !!application?.capabilities?.indexPatterns?.save;

return <Comp userEditPermission={userEditPermission} {...props} />;
};
};

class IndexedFields extends Component<IndexedFieldsTableProps, IndexedFieldsTableState> {
export class IndexedFieldsTable extends Component<
IndexedFieldsTableProps,
IndexedFieldsTableState
> {
constructor(props: IndexedFieldsTableProps) {
super(props);

Expand Down Expand Up @@ -158,5 +150,3 @@ class IndexedFields extends Component<IndexedFieldsTableProps, IndexedFieldsTabl
);
}
}

export const IndexedFieldsTable = withHooks(IndexedFields);
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ interface HeaderProps extends RouteComponentProps {
}

export const Header = withRouter(({ indexPatternId, history }: HeaderProps) => {
const { application, docLinks } = useKibana<IndexPatternManagmentContext>().services;
const { dataViews, docLinks } = useKibana<IndexPatternManagmentContext>().services;
const links = docLinks?.links;
const userEditPermission = !!application?.capabilities?.indexPatterns?.save;
const userEditPermission = dataViews.getCanSaveSync();
return (
<EuiFlexGroup alignItems="center">
<EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ describe('ScriptedFieldsTable', () => {
helpers={helpers}
painlessDocLink={'painlessDoc'}
saveIndexPattern={async () => {}}
userEditPermission={false}
scriptedFieldLanguageFilter={[]}
/>
).dive();
);

// Allow the componentWillMount code to execute
// https://github.com/airbnb/enzyme/issues/450
Expand All @@ -87,9 +88,10 @@ describe('ScriptedFieldsTable', () => {
helpers={helpers}
painlessDocLink={'painlessDoc'}
saveIndexPattern={async () => {}}
userEditPermission={false}
scriptedFieldLanguageFilter={[]}
/>
).dive();
);

// Allow the componentWillMount code to execute
// https://github.com/airbnb/enzyme/issues/450
Expand Down Expand Up @@ -119,9 +121,10 @@ describe('ScriptedFieldsTable', () => {
painlessDocLink={'painlessDoc'}
helpers={helpers}
saveIndexPattern={async () => {}}
userEditPermission={false}
scriptedFieldLanguageFilter={[]}
/>
).dive();
);

// Allow the componentWillMount code to execute
// https://github.com/airbnb/enzyme/issues/450
Expand All @@ -145,9 +148,10 @@ describe('ScriptedFieldsTable', () => {
painlessDocLink={'painlessDoc'}
helpers={helpers}
saveIndexPattern={async () => {}}
userEditPermission={false}
scriptedFieldLanguageFilter={[]}
/>
).dive();
);

// Allow the componentWillMount code to execute
// https://github.com/airbnb/enzyme/issues/450
Expand All @@ -166,9 +170,10 @@ describe('ScriptedFieldsTable', () => {
helpers={helpers}
painlessDocLink={'painlessDoc'}
saveIndexPattern={async () => {}}
userEditPermission={false}
scriptedFieldLanguageFilter={[]}
/>
).dive();
);

await component.update(); // Fire `componentWillMount()`
// @ts-expect-error lang is not valid
Expand All @@ -194,9 +199,10 @@ describe('ScriptedFieldsTable', () => {
helpers={helpers}
painlessDocLink={'painlessDoc'}
saveIndexPattern={async () => {}}
userEditPermission={false}
scriptedFieldLanguageFilter={[]}
/>
).dive();
);

await component.update(); // Fire `componentWillMount()`
// @ts-expect-error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import {

import { Table, Header, CallOuts, DeleteScritpedFieldConfirmationModal } from './components';
import { ScriptedFieldItem } from './types';
import { IndexPatternManagmentContext } from '../../../types';

import { DataView, DataViewsPublicPluginStart } from '../../../../../../plugins/data_views/public';
import { useKibana } from '../../../../../../plugins/kibana_react/public';

interface ScriptedFieldsTableProps {
indexPattern: DataView;
Expand All @@ -41,16 +39,10 @@ interface ScriptedFieldsTableState {
fields: ScriptedFieldItem[];
}

const withHooks = (Comp: typeof Component) => {
return (props: any) => {
const { application } = useKibana<IndexPatternManagmentContext>().services;
const userEditPermission = !!application?.capabilities?.indexPatterns?.save;

return <Comp userEditPermission={userEditPermission} {...props} />;
};
};

class ScriptedFields extends Component<ScriptedFieldsTableProps, ScriptedFieldsTableState> {
export class ScriptedFieldsTable extends Component<
ScriptedFieldsTableProps,
ScriptedFieldsTableState
> {
constructor(props: ScriptedFieldsTableProps) {
super(props);

Expand Down Expand Up @@ -168,5 +160,3 @@ class ScriptedFields extends Component<ScriptedFieldsTableProps, ScriptedFieldsT
);
}
}

export const ScriptedFieldsTable = withHooks(ScriptedFields);
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export function Tabs({
location,
refreshFields,
}: TabsProps) {
const { application, uiSettings, docLinks, dataViewFieldEditor, overlays, theme } =
const { uiSettings, docLinks, dataViewFieldEditor, overlays, theme, dataViews } =
useKibana<IndexPatternManagmentContext>().services;
const [fieldFilter, setFieldFilter] = useState<string>('');
const [syncingStateFunc, setSyncingStateFunc] = useState<any>({
Expand Down Expand Up @@ -241,7 +241,7 @@ export function Tabs({
[uiSettings]
);

const userEditPermission = !!application?.capabilities?.indexPatterns?.save;
const userEditPermission = dataViews.getCanSaveSync();
const getFilterSection = useCallback(
(type: string) => {
return (
Expand Down Expand Up @@ -448,7 +448,8 @@ export function Tabs({
getFieldInfo,
}}
openModal={overlays.openModal}
theme={theme}
theme={theme!}
userEditPermission={dataViews.getCanSaveSync()}
/>
)}
</DeleteRuntimeFieldProvider>
Expand All @@ -472,6 +473,7 @@ export function Tabs({
}}
onRemoveField={refreshFilters}
painlessDocLink={docLinks.links.scriptedFields.painless}
userEditPermission={dataViews.getCanSaveSync()}
/>
</Fragment>
);
Expand Down Expand Up @@ -510,6 +512,7 @@ export function Tabs({
refreshFields,
overlays,
theme,
dataViews,
]
);

Expand Down
Loading

0 comments on commit ded0478

Please sign in to comment.