Skip to content

Commit

Permalink
[ML] Fix permission check for Discover/data view redirect from Anomal…
Browse files Browse the repository at this point in the history
…y detection explorer page (#124408) (#124989)

* Add permission check for data view redirect

* Add checks if data view doesn't exist & tooltip hints

* Remove console.error cause they are redundant

* Not show link if no access to Discover

* Fix duplicates

* Fix duplicates, text

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit e5678ff)

Co-authored-by: Quynh Nguyen <43350163+qn895@users.noreply.github.com>
  • Loading branch information
kibanamachine and qn895 committed Feb 10, 2022
1 parent 17e33bc commit 19912b7
Showing 1 changed file with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
EuiContextMenuPanel,
EuiPopover,
EuiProgress,
EuiToolTip,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import { i18n } from '@kbn/i18n';
Expand Down Expand Up @@ -57,33 +58,54 @@ interface LinksMenuProps {

export const LinksMenuUI = (props: LinksMenuProps) => {
const [openInDiscoverUrl, setOpenInDiscoverUrl] = useState<string | undefined>();
const [discoverUrlError, setDiscoverUrlError] = useState<string | undefined>();

const isCategorizationAnomalyRecord = isCategorizationAnomaly(props.anomaly);

const closePopover = props.onItemClick;

const kibana = useMlKibana();
const {
services: { share, application },
} = kibana;

useEffect(() => {
let unmounted = false;

const generateDiscoverUrl = async () => {
const {
services: { share },
} = kibana;
const discoverLocator = share.url.locators.get('DISCOVER_APP_LOCATOR');

if (!discoverLocator) {
// eslint-disable-next-line no-console
console.error('No locator for Discover detected');
const discoverLocatorMissing = i18n.translate(
'xpack.ml.anomaliesTable.linksMenu.discoverLocatorMissingErrorMessage',
{
defaultMessage: 'No locator for Discover detected',
}
);

setDiscoverUrlError(discoverLocatorMissing);

return;
}

const job = mlJobService.getJob(props.anomaly.jobId);

const index = job.datafeed_config.indices[0];
const interval = props.interval;
const dataViewId = (await getDataViewIdFromName(index)) || index;
const dataViewId = await getDataViewIdFromName(index);

// If data view doesn't exist for some reasons
if (!dataViewId) {
const autoGeneratedDiscoverLinkError = i18n.translate(
'xpack.ml.anomaliesTable.linksMenu.autoGeneratedDiscoverLinkErrorMessage',
{
defaultMessage: `Unable to link to Discover; no data view exists for index '{index}'`,
values: { index },
}
);

setDiscoverUrlError(autoGeneratedDiscoverLinkError);
}
const record = props.anomaly.source;

const earliestMoment = moment(record.timestamp).startOf(interval);
Expand Down Expand Up @@ -243,9 +265,6 @@ export const LinksMenuUI = (props: LinksMenuProps) => {
};

const viewSeries = async () => {
const {
services: { share },
} = kibana;
const mlLocator = share.url.locators.get(ML_APP_LOCATOR);

const record = props.anomaly.source;
Expand Down Expand Up @@ -501,22 +520,31 @@ export const LinksMenuUI = (props: LinksMenuProps) => {
});
}

if (!isCategorizationAnomalyRecord) {
if (application.capabilities.discover?.show && !isCategorizationAnomalyRecord) {
// Add item from the start, but disable it during the URL generation.
const isLoading = openInDiscoverUrl === undefined;
const isLoading = discoverUrlError === undefined && openInDiscoverUrl === undefined;

items.push(
<EuiContextMenuItem
key={`auto_raw_data_url`}
icon="discoverApp"
disabled={isLoading}
disabled={discoverUrlError !== undefined || isLoading}
href={openInDiscoverUrl}
data-test-subj={`mlAnomaliesListRowAction_viewInDiscoverButton`}
>
<FormattedMessage
id="xpack.ml.anomaliesTable.linksMenu.viewInDiscover"
defaultMessage="View in Discover"
/>
{discoverUrlError ? (
<EuiToolTip content={discoverUrlError}>
<FormattedMessage
id="xpack.ml.anomaliesTable.linksMenu.viewInDiscover"
defaultMessage="View in Discover"
/>
</EuiToolTip>
) : (
<FormattedMessage
id="xpack.ml.anomaliesTable.linksMenu.viewInDiscover"
defaultMessage="View in Discover"
/>
)}
{isLoading ? <EuiProgress size={'xs'} color={'accent'} /> : null}
</EuiContextMenuItem>
);
Expand Down

0 comments on commit 19912b7

Please sign in to comment.