UserPermissions.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import React, { useState } from 'react';
  2. import { ConfirmButton, RadioButtonGroup, Icon } from '@grafana/ui';
  3. import { contextSrv } from 'app/core/core';
  4. import { AccessControlAction } from 'app/types';
  5. interface Props {
  6. isGrafanaAdmin: boolean;
  7. onGrafanaAdminChange: (isGrafanaAdmin: boolean) => void;
  8. }
  9. const adminOptions = [
  10. { label: 'Yes', value: true },
  11. { label: 'No', value: false },
  12. ];
  13. export function UserPermissions({ isGrafanaAdmin, onGrafanaAdminChange }: Props) {
  14. const [isEditing, setIsEditing] = useState(false);
  15. const [currentAdminOption, setCurrentAdminOption] = useState(isGrafanaAdmin);
  16. const onChangeClick = () => setIsEditing(true);
  17. const onCancelClick = () => {
  18. setIsEditing(false);
  19. setCurrentAdminOption(isGrafanaAdmin);
  20. };
  21. const handleGrafanaAdminChange = () => onGrafanaAdminChange(currentAdminOption);
  22. const canChangePermissions = contextSrv.hasPermission(AccessControlAction.UsersPermissionsUpdate);
  23. return (
  24. <>
  25. <h3 className="page-heading">Permissions</h3>
  26. <div className="gf-form-group">
  27. <div className="gf-form">
  28. <table className="filter-table form-inline">
  29. <tbody>
  30. <tr>
  31. <td className="width-16">Grafana Admin</td>
  32. {isEditing ? (
  33. <td colSpan={2}>
  34. <RadioButtonGroup
  35. options={adminOptions}
  36. value={currentAdminOption}
  37. onChange={setCurrentAdminOption}
  38. autoFocus
  39. />
  40. </td>
  41. ) : (
  42. <td colSpan={2}>
  43. {isGrafanaAdmin ? (
  44. <>
  45. <Icon name="shield" /> Yes
  46. </>
  47. ) : (
  48. <>No</>
  49. )}
  50. </td>
  51. )}
  52. <td>
  53. {canChangePermissions && (
  54. <ConfirmButton
  55. onClick={onChangeClick}
  56. onConfirm={handleGrafanaAdminChange}
  57. onCancel={onCancelClick}
  58. confirmText="Change"
  59. >
  60. Change
  61. </ConfirmButton>
  62. )}
  63. </td>
  64. </tr>
  65. </tbody>
  66. </table>
  67. </div>
  68. </div>
  69. </>
  70. );
  71. }