DashboardPermissions.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { Tooltip, Icon, Button } from '@grafana/ui';
  4. import { SlideDown } from 'app/core/components/Animations/SlideDown';
  5. import AddPermission from 'app/core/components/PermissionList/AddPermission';
  6. import PermissionList from 'app/core/components/PermissionList/PermissionList';
  7. import PermissionsInfo from 'app/core/components/PermissionList/PermissionsInfo';
  8. import { StoreState } from 'app/types';
  9. import { DashboardAcl, PermissionLevel, NewDashboardAclItem } from 'app/types/acl';
  10. import { checkFolderPermissions } from '../../../folders/state/actions';
  11. import { DashboardModel } from '../../state/DashboardModel';
  12. import {
  13. getDashboardPermissions,
  14. addDashboardPermission,
  15. removeDashboardPermission,
  16. updateDashboardPermission,
  17. } from '../../state/actions';
  18. const mapStateToProps = (state: StoreState) => ({
  19. permissions: state.dashboard.permissions,
  20. canViewFolderPermissions: state.folder.canViewFolderPermissions,
  21. });
  22. const mapDispatchToProps = {
  23. getDashboardPermissions,
  24. addDashboardPermission,
  25. removeDashboardPermission,
  26. updateDashboardPermission,
  27. checkFolderPermissions,
  28. };
  29. const connector = connect(mapStateToProps, mapDispatchToProps);
  30. export interface OwnProps {
  31. dashboard: DashboardModel;
  32. }
  33. export type Props = OwnProps & ConnectedProps<typeof connector>;
  34. export interface State {
  35. isAdding: boolean;
  36. }
  37. export class DashboardPermissionsUnconnected extends PureComponent<Props, State> {
  38. constructor(props: Props) {
  39. super(props);
  40. this.state = {
  41. isAdding: false,
  42. };
  43. }
  44. componentDidMount() {
  45. this.props.getDashboardPermissions(this.props.dashboard.id);
  46. if (this.props.dashboard.meta.folderUid) {
  47. this.props.checkFolderPermissions(this.props.dashboard.meta.folderUid);
  48. }
  49. }
  50. onOpenAddPermissions = () => {
  51. this.setState({ isAdding: true });
  52. };
  53. onRemoveItem = (item: DashboardAcl) => {
  54. this.props.removeDashboardPermission(this.props.dashboard.id, item);
  55. };
  56. onPermissionChanged = (item: DashboardAcl, level: PermissionLevel) => {
  57. this.props.updateDashboardPermission(this.props.dashboard.id, item, level);
  58. };
  59. onAddPermission = (newItem: NewDashboardAclItem) => {
  60. return this.props.addDashboardPermission(this.props.dashboard.id, newItem);
  61. };
  62. onCancelAddPermission = () => {
  63. this.setState({ isAdding: false });
  64. };
  65. getFolder() {
  66. const { dashboard, canViewFolderPermissions } = this.props;
  67. return {
  68. id: dashboard.meta.folderId,
  69. title: dashboard.meta.folderTitle,
  70. url: dashboard.meta.folderUrl,
  71. canViewFolderPermissions,
  72. };
  73. }
  74. render() {
  75. const {
  76. permissions,
  77. dashboard: {
  78. meta: { hasUnsavedFolderChange },
  79. },
  80. } = this.props;
  81. const { isAdding } = this.state;
  82. return hasUnsavedFolderChange ? (
  83. <h5>You have changed a folder, please save to view permissions.</h5>
  84. ) : (
  85. <div>
  86. <div className="page-action-bar">
  87. <h3 className="page-sub-heading">Permissions</h3>
  88. <Tooltip placement="auto" content={<PermissionsInfo />}>
  89. <Icon className="icon--has-hover page-sub-heading-icon" name="question-circle" />
  90. </Tooltip>
  91. <div className="page-action-bar__spacer" />
  92. <Button className="pull-right" onClick={this.onOpenAddPermissions} disabled={isAdding}>
  93. Add permission
  94. </Button>
  95. </div>
  96. <SlideDown in={isAdding}>
  97. <AddPermission onAddPermission={this.onAddPermission} onCancel={this.onCancelAddPermission} />
  98. </SlideDown>
  99. <PermissionList
  100. items={permissions}
  101. onRemoveItem={this.onRemoveItem}
  102. onPermissionChanged={this.onPermissionChanged}
  103. isFetching={false}
  104. folderInfo={this.getFolder()}
  105. />
  106. </div>
  107. );
  108. }
  109. }
  110. export const DashboardPermissions = connector(DashboardPermissionsUnconnected);