AccessControlDataSourcePermissions.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import React, { useEffect } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { Permissions } from 'app/core/components/AccessControl';
  4. import Page from 'app/core/components/Page/Page';
  5. import { UpgradeBox } from 'app/core/components/Upgrade/UpgradeBox';
  6. import { contextSrv } from 'app/core/core';
  7. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  8. import { getNavModel } from 'app/core/selectors/navModel';
  9. import { highlightTrial } from 'app/features/admin/utils';
  10. import { loadDataSource, loadDataSourceMeta } from 'app/features/datasources/state/actions';
  11. import { getDataSourceLoadingNav } from 'app/features/datasources/state/navModel';
  12. import { AccessControlAction as EnterpriseActions, EnterpriseStoreState } from '../types';
  13. interface RouteProps extends GrafanaRouteComponentProps<{ uid: string }> {}
  14. function mapStateToProps(state: EnterpriseStoreState, props: RouteProps) {
  15. const uid = props.match.params.uid;
  16. return {
  17. resourceId: uid,
  18. navModel: getNavModel(state.navIndex, `datasource-permissions-${uid}`, getDataSourceLoadingNav('permissions')),
  19. };
  20. }
  21. const mapDispatchToProps = {
  22. loadDataSource,
  23. loadDataSourceMeta,
  24. };
  25. export const connector = connect(mapStateToProps, mapDispatchToProps);
  26. export type Props = ConnectedProps<typeof connector>;
  27. const DataSourcePermissions = ({ resourceId, loadDataSource, navModel, loadDataSourceMeta }: Props) => {
  28. useEffect(() => {
  29. loadDataSource(resourceId);
  30. }, [resourceId, loadDataSource]);
  31. const canSetPermissions = contextSrv.hasPermission(EnterpriseActions.DataSourcesPermissionsWrite);
  32. useEffect(() => {
  33. // Initialize DS metadata on page load to populate tab navigation
  34. loadDataSource(resourceId as any).then(loadDataSourceMeta);
  35. }, [loadDataSource, loadDataSourceMeta, resourceId]);
  36. return (
  37. <Page navModel={navModel}>
  38. <Page.Contents>
  39. {highlightTrial() && (
  40. <UpgradeBox
  41. featureId={'data-source-permissions'}
  42. eventVariant={'trial'}
  43. featureName={'data source permissions'}
  44. text={'Enable data source permissions for free during your trial of Grafana Pro.'}
  45. />
  46. )}
  47. <Permissions resource="datasources" resourceId={resourceId} canSetPermissions={canSetPermissions} />
  48. </Page.Contents>
  49. </Page>
  50. );
  51. };
  52. export default connector(DataSourcePermissions);