ServiceAccountProfile.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import { css, cx } from '@emotion/css';
  2. import React, { PureComponent, useRef, useState } from 'react';
  3. import { dateTimeFormat, GrafanaTheme2, OrgRole, TimeZone } from '@grafana/data';
  4. import { Button, ConfirmButton, ConfirmModal, Input, LegacyInputStatus, useStyles2 } from '@grafana/ui';
  5. import { contextSrv } from 'app/core/core';
  6. import { Role, ServiceAccountDTO, AccessControlAction } from 'app/types';
  7. import { ServiceAccountRoleRow } from './ServiceAccountRoleRow';
  8. interface Props {
  9. serviceAccount: ServiceAccountDTO;
  10. timeZone: TimeZone;
  11. roleOptions: Role[];
  12. builtInRoles: Record<string, Role[]>;
  13. deleteServiceAccount: (serviceAccountId: number) => void;
  14. updateServiceAccount: (serviceAccount: ServiceAccountDTO) => void;
  15. }
  16. export function ServiceAccountProfile({
  17. serviceAccount,
  18. timeZone,
  19. roleOptions,
  20. builtInRoles,
  21. deleteServiceAccount,
  22. updateServiceAccount,
  23. }: Props) {
  24. const [showDeleteModal, setShowDeleteModal] = useState(false);
  25. const [showDisableModal, setShowDisableModal] = useState(false);
  26. const ableToWrite = contextSrv.hasPermission(AccessControlAction.ServiceAccountsWrite);
  27. const deleteServiceAccountRef = useRef<HTMLButtonElement | null>(null);
  28. const showDeleteServiceAccountModal = (show: boolean) => () => {
  29. setShowDeleteModal(show);
  30. if (!show && deleteServiceAccountRef.current) {
  31. deleteServiceAccountRef.current.focus();
  32. }
  33. };
  34. const disableServiceAccountRef = useRef<HTMLButtonElement | null>(null);
  35. const showDisableServiceAccountModal = (show: boolean) => () => {
  36. setShowDisableModal(show);
  37. if (!show && disableServiceAccountRef.current) {
  38. disableServiceAccountRef.current.focus();
  39. }
  40. };
  41. const handleServiceAccountDelete = () => {
  42. deleteServiceAccount(serviceAccount.id);
  43. };
  44. const handleServiceAccountDisable = () => {
  45. updateServiceAccount({ ...serviceAccount, isDisabled: true });
  46. setShowDisableModal(false);
  47. };
  48. const handleServiceAccountEnable = () => {
  49. updateServiceAccount({ ...serviceAccount, isDisabled: false });
  50. };
  51. const handleServiceAccountRoleChange = (role: OrgRole) => {
  52. updateServiceAccount({ ...serviceAccount, role: role });
  53. };
  54. const onServiceAccountNameChange = (newValue: string) => {
  55. updateServiceAccount({ ...serviceAccount, name: newValue });
  56. };
  57. const styles = useStyles2(getStyles);
  58. return (
  59. <>
  60. <div style={{ marginBottom: '10px' }}>
  61. <a href="org/serviceaccounts" style={{ display: 'inline-block', verticalAlign: 'middle' }}>
  62. <Button fill="text" icon="backward" />
  63. </a>
  64. <h1
  65. className="page-heading"
  66. style={{ display: 'inline-block', verticalAlign: 'middle', margin: '0!important', marginBottom: '0px' }}
  67. >
  68. {serviceAccount.name}
  69. </h1>
  70. </div>
  71. <span style={{ marginBottom: '10px' }}>Information</span>
  72. <div className="gf-form-group">
  73. <div className="gf-form">
  74. <table className="filter-table form-inline">
  75. <tbody>
  76. <ServiceAccountProfileRow
  77. label="Name"
  78. value={serviceAccount.name}
  79. onChange={onServiceAccountNameChange}
  80. disabled={!ableToWrite}
  81. />
  82. <ServiceAccountProfileRow label="ID" value={serviceAccount.login} />
  83. <ServiceAccountRoleRow
  84. label="Roles"
  85. serviceAccount={serviceAccount}
  86. onRoleChange={handleServiceAccountRoleChange}
  87. builtInRoles={builtInRoles}
  88. roleOptions={roleOptions}
  89. />
  90. {/* <ServiceAccountProfileRow label="Teams" value={serviceAccount.teams.join(', ')} /> */}
  91. <ServiceAccountProfileRow
  92. label="Creation date"
  93. value={dateTimeFormat(serviceAccount.createdAt, { timeZone })}
  94. />
  95. </tbody>
  96. </table>
  97. </div>
  98. <div className={styles.buttonRow}>
  99. <>
  100. <Button
  101. type={'button'}
  102. variant="destructive"
  103. onClick={showDeleteServiceAccountModal(true)}
  104. ref={deleteServiceAccountRef}
  105. disabled={!contextSrv.hasPermission(AccessControlAction.ServiceAccountsDelete)}
  106. >
  107. Delete service account
  108. </Button>
  109. <ConfirmModal
  110. isOpen={showDeleteModal}
  111. title="Delete service account"
  112. body="Are you sure you want to delete this service account?"
  113. confirmText="Delete service account"
  114. onConfirm={handleServiceAccountDelete}
  115. onDismiss={showDeleteServiceAccountModal(false)}
  116. />
  117. </>
  118. {serviceAccount.isDisabled ? (
  119. <Button type={'button'} variant="secondary" onClick={handleServiceAccountEnable} disabled={!ableToWrite}>
  120. Enable service account
  121. </Button>
  122. ) : (
  123. <>
  124. <Button
  125. type={'button'}
  126. variant="secondary"
  127. onClick={showDisableServiceAccountModal(true)}
  128. ref={disableServiceAccountRef}
  129. disabled={!ableToWrite}
  130. >
  131. Disable service account
  132. </Button>
  133. <ConfirmModal
  134. isOpen={showDisableModal}
  135. title="Disable service account"
  136. body="Are you sure you want to disable this service account?"
  137. confirmText="Disable service account"
  138. onConfirm={handleServiceAccountDisable}
  139. onDismiss={showDisableServiceAccountModal(false)}
  140. />
  141. </>
  142. )}
  143. </div>
  144. </div>
  145. </>
  146. );
  147. }
  148. const getStyles = (theme: GrafanaTheme2) => {
  149. return {
  150. buttonRow: css`
  151. margin-top: ${theme.spacing(1.5)};
  152. > * {
  153. margin-right: ${theme.spacing(2)};
  154. }
  155. `,
  156. };
  157. };
  158. interface ServiceAccountProfileRowProps {
  159. label: string;
  160. value?: string;
  161. inputType?: string;
  162. onChange?: (value: string) => void;
  163. disabled?: boolean;
  164. }
  165. interface ServiceAccountProfileRowState {
  166. value: string;
  167. editing: boolean;
  168. }
  169. export class ServiceAccountProfileRow extends PureComponent<
  170. ServiceAccountProfileRowProps,
  171. ServiceAccountProfileRowState
  172. > {
  173. inputElem?: HTMLInputElement;
  174. static defaultProps: Partial<ServiceAccountProfileRowProps> = {
  175. value: '',
  176. inputType: 'text',
  177. };
  178. state = {
  179. editing: false,
  180. value: this.props.value || '',
  181. };
  182. setInputElem = (elem: any) => {
  183. this.inputElem = elem;
  184. };
  185. onEditClick = () => {
  186. this.setState({ editing: true }, this.focusInput);
  187. };
  188. onCancelClick = () => {
  189. this.setState({ editing: false, value: this.props.value || '' });
  190. };
  191. onInputChange = (event: React.ChangeEvent<HTMLInputElement>, status?: LegacyInputStatus) => {
  192. if (status === LegacyInputStatus.Invalid) {
  193. return;
  194. }
  195. this.setState({ value: event.target.value });
  196. };
  197. onInputBlur = (event: React.FocusEvent<HTMLInputElement>, status?: LegacyInputStatus) => {
  198. if (status === LegacyInputStatus.Invalid) {
  199. return;
  200. }
  201. this.setState({ value: event.target.value });
  202. };
  203. focusInput = () => {
  204. if (this.inputElem && this.inputElem.focus) {
  205. this.inputElem.focus();
  206. }
  207. };
  208. onSave = () => {
  209. this.setState({ editing: false });
  210. if (this.props.onChange) {
  211. this.props.onChange(this.state.value);
  212. }
  213. };
  214. render() {
  215. const { label, inputType } = this.props;
  216. const { value } = this.state;
  217. const labelClass = cx(
  218. 'width-16',
  219. css`
  220. font-weight: 500;
  221. `
  222. );
  223. const inputId = `${label}-input`;
  224. return (
  225. <tr>
  226. <td className={labelClass}>
  227. <label htmlFor={inputId}>{label}</label>
  228. </td>
  229. <td className="width-25" colSpan={2}>
  230. {!this.props.disabled && this.state.editing ? (
  231. <Input
  232. id={inputId}
  233. type={inputType}
  234. defaultValue={value}
  235. onBlur={this.onInputBlur}
  236. onChange={this.onInputChange}
  237. ref={this.setInputElem}
  238. width={30}
  239. />
  240. ) : (
  241. <span>{this.props.value}</span>
  242. )}
  243. </td>
  244. <td>
  245. {this.props.onChange && (
  246. <ConfirmButton
  247. closeOnConfirm
  248. confirmText="Save"
  249. onConfirm={this.onSave}
  250. onClick={this.onEditClick}
  251. onCancel={this.onCancelClick}
  252. disabled={this.props.disabled}
  253. >
  254. Edit
  255. </ConfirmButton>
  256. )}
  257. </td>
  258. </tr>
  259. );
  260. }
  261. }