Data sources: Hide the datasource redirection banner for users who can't interact with data sources (#92886)

* hide the datasource redirection banner for users who can't list data sources

* readding explore evaluation

* add a test
This commit is contained in:
Ieva 2024-09-09 12:50:09 +01:00 committed by GitHub
parent 6548ea377d
commit ba9f1da28e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View File

@ -0,0 +1,23 @@
import { render, screen } from '@testing-library/react';
import { contextSrv } from '../../../../core/services/context_srv';
import { ConnectionsRedirectNotice } from './ConnectionsRedirectNotice';
const setup = (hasAccessToDS: boolean) => {
jest.spyOn(contextSrv, 'hasPermission').mockReturnValue(hasAccessToDS);
render(<ConnectionsRedirectNotice />);
};
describe('ConnectionsRedirectNotice', () => {
it('should render component when has access to data sources', () => {
setup(true);
expect(screen.getByRole('link')).toBeInTheDocument();
});
it('should not render component when has no access to data sources', () => {
setup(false);
expect(screen.queryByRole('link')).not.toBeInTheDocument();
});
});

View File

@ -4,6 +4,8 @@ import { useState } from 'react';
import { GrafanaTheme2 } from '@grafana/data';
import { Alert, LinkButton, useStyles2 } from '@grafana/ui';
import { contextSrv } from '../../../../core/core';
import { AccessControlAction } from '../../../../types';
import { ROUTES } from '../../constants';
const getStyles = (theme: GrafanaTheme2) => ({
@ -22,7 +24,10 @@ const getStyles = (theme: GrafanaTheme2) => ({
export function ConnectionsRedirectNotice() {
const styles = useStyles2(getStyles);
const [showNotice, setShowNotice] = useState(true);
const canAccessDataSources =
contextSrv.hasPermission(AccessControlAction.DataSourcesCreate) ||
contextSrv.hasPermission(AccessControlAction.DataSourcesWrite);
const [showNotice, setShowNotice] = useState(canAccessDataSources);
return showNotice ? (
<Alert severity="info" title="" onRemove={() => setShowNotice(false)}>