DataSourcePermissionsList.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React from 'react';
  2. import { Icon, LegacyForms } from '@grafana/ui';
  3. import { dataSourceAclLevels, DataSourcePermissionLevel } from 'app/types/acl';
  4. import { DataSourcePermission } from '../types';
  5. const { Select } = LegacyForms;
  6. export interface Props {
  7. items: DataSourcePermission[];
  8. onRemoveItem: (item: DataSourcePermission) => void;
  9. }
  10. const adminRole: DataSourcePermission = {
  11. id: -1,
  12. datasourceId: -1,
  13. updated: '',
  14. created: '',
  15. permissionName: 'Admin',
  16. builtInRole: 'Admin',
  17. permission: DataSourcePermissionLevel.Admin,
  18. };
  19. const adminLevel = {
  20. label: 'Admin',
  21. description: '',
  22. value: DataSourcePermissionLevel.Admin,
  23. };
  24. export const DataSourcePermissionsList = ({ items, onRemoveItem }: Props) => {
  25. const users = items.filter((i) => i.userId !== undefined);
  26. const teams = items.filter((i) => i.teamId !== undefined);
  27. const builtIns = [adminRole];
  28. return (
  29. <div>
  30. <PermissionTable title={'Roles'} items={builtIns} onRemove={onRemoveItem} />
  31. <PermissionTable title={'Users'} items={users} onRemove={onRemoveItem} />
  32. <PermissionTable title={'Teams'} items={teams} onRemove={onRemoveItem} />
  33. </div>
  34. );
  35. };
  36. interface PermissionTableProps {
  37. title: string;
  38. items: DataSourcePermission[];
  39. onRemove: (item: DataSourcePermission) => void;
  40. }
  41. const PermissionTable = ({ title, items, onRemove }: PermissionTableProps) => {
  42. if (items.length === 0) {
  43. return null;
  44. }
  45. return (
  46. <div>
  47. <h5>{title}</h5>
  48. <table className="filter-table gf-form-group">
  49. <tbody>
  50. {items.map((item, index) => (
  51. <TableRow key={`${index}-${item.id}`} item={item} onRemove={onRemove} />
  52. ))}
  53. </tbody>
  54. </table>
  55. </div>
  56. );
  57. };
  58. interface TableRowProps {
  59. item: DataSourcePermission;
  60. onRemove: (item: DataSourcePermission) => void;
  61. }
  62. const TableRow = ({ item, onRemove }: TableRowProps) => {
  63. const renderAvatar = () => {
  64. if (item.teamId) {
  65. return <img className="filter-table__avatar" src={item.teamAvatarUrl} alt={`Avatar for team ${item.teamId}`} />;
  66. } else if (item.userId) {
  67. return <img className="filter-table__avatar" src={item.userAvatarUrl} alt={`Avatar for user ${item.userId}`} />;
  68. }
  69. return <Icon size="xl" name="shield" />;
  70. };
  71. const renderDescription = () => {
  72. if (item.userId) {
  73. return <span key="name">{item.userLogin} </span>;
  74. } else if (item.teamId) {
  75. return <span key="name">{item.team} </span>;
  76. } else if (item.builtInRole) {
  77. return <span key="name">{item.builtInRole} </span>;
  78. }
  79. return <span key="name" />;
  80. };
  81. const levels = [...dataSourceAclLevels, adminLevel];
  82. return (
  83. <tr>
  84. <td style={{ width: '1%' }}>{renderAvatar()}</td>
  85. <td style={{ width: '90%' }}>{renderDescription()}</td>
  86. <td />
  87. <td className="query-keyword">Can</td>
  88. <td>
  89. <div className="gf-form">
  90. <Select
  91. isDisabled={true}
  92. onChange={() => {}}
  93. options={levels}
  94. value={levels.find((level) => level.value === item.permission)}
  95. />
  96. </div>
  97. </td>
  98. <td>
  99. {item.builtInRole ? (
  100. <button className="btn btn-inverse btn-small">
  101. <Icon name="lock" size="sm" />
  102. </button>
  103. ) : (
  104. <button className="btn btn-danger btn-small" onClick={() => onRemove(item)}>
  105. <Icon name="times" size="sm" />
  106. </button>
  107. )}
  108. </td>
  109. </tr>
  110. );
  111. };