adminUserEdit.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import {Component, Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import {browserHistory} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  6. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  7. import {openModal} from 'sentry/actionCreators/modal';
  8. import {Button} from 'sentry/components/button';
  9. import RadioGroup from 'sentry/components/forms/controls/radioGroup';
  10. import Form from 'sentry/components/forms/form';
  11. import JsonForm from 'sentry/components/forms/jsonForm';
  12. import FormModel from 'sentry/components/forms/model';
  13. import type {JsonFormObject} from 'sentry/components/forms/types';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import type {User} from 'sentry/types';
  17. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  18. const userEditForm: JsonFormObject = {
  19. title: 'User details',
  20. fields: [
  21. {
  22. name: 'name',
  23. type: 'string',
  24. required: true,
  25. label: t('Name'),
  26. },
  27. {
  28. name: 'username',
  29. type: 'string',
  30. required: true,
  31. label: t('Username'),
  32. help: t('The username is the unique id of the user in the system'),
  33. },
  34. {
  35. name: 'email',
  36. type: 'string',
  37. required: true,
  38. label: t('Email'),
  39. help: t('The users primary email address'),
  40. },
  41. {
  42. name: 'isActive',
  43. type: 'boolean',
  44. required: true,
  45. label: t('Active'),
  46. help: t(
  47. 'Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'
  48. ),
  49. },
  50. {
  51. name: 'isStaff',
  52. type: 'boolean',
  53. required: true,
  54. label: t('Admin'),
  55. help: t('Designates whether this user can perform administrative functions.'),
  56. },
  57. {
  58. name: 'isSuperuser',
  59. type: 'boolean',
  60. required: true,
  61. label: t('Superuser'),
  62. help: t(
  63. 'Designates whether this user has all permissions without explicitly assigning them.'
  64. ),
  65. },
  66. ],
  67. };
  68. const REMOVE_BUTTON_LABEL = {
  69. disable: t('Disable User'),
  70. delete: t('Permanently Delete User'),
  71. };
  72. type DeleteType = 'disable' | 'delete';
  73. type RemoveModalProps = ModalRenderProps & {
  74. onRemove: (type: DeleteType) => void;
  75. user: User;
  76. };
  77. type RemoveModalState = {
  78. deleteType: DeleteType;
  79. };
  80. class RemoveUserModal extends Component<RemoveModalProps, RemoveModalState> {
  81. state: RemoveModalState = {
  82. deleteType: 'disable',
  83. };
  84. onRemove = () => {
  85. this.props.onRemove(this.state.deleteType);
  86. this.props.closeModal();
  87. };
  88. render() {
  89. const {user} = this.props;
  90. const {deleteType} = this.state;
  91. return (
  92. <Fragment>
  93. <RadioGroup
  94. value={deleteType}
  95. label={t('Remove user %s', user.email)}
  96. onChange={type => this.setState({deleteType: type})}
  97. choices={[
  98. ['disable', t('Disable the account.')],
  99. ['delete', t('Permanently remove the user and their data.')],
  100. ]}
  101. />
  102. <ModalFooter>
  103. <Button priority="danger" onClick={this.onRemove}>
  104. {REMOVE_BUTTON_LABEL[deleteType]}
  105. </Button>
  106. <Button onClick={this.props.closeModal}>{t('Cancel')}</Button>
  107. </ModalFooter>
  108. </Fragment>
  109. );
  110. }
  111. }
  112. type Props = DeprecatedAsyncView['props'] & RouteComponentProps<{id: string}, {}>;
  113. type State = DeprecatedAsyncView['state'] & {
  114. user: User | null;
  115. };
  116. class AdminUserEdit extends DeprecatedAsyncView<Props, State> {
  117. get userEndpoint() {
  118. const {params} = this.props;
  119. return `/users/${params.id}/`;
  120. }
  121. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  122. return [['user', this.userEndpoint]];
  123. }
  124. async deleteUser() {
  125. await this.api.requestPromise(this.userEndpoint, {
  126. method: 'DELETE',
  127. data: {hardDelete: true, organizations: []},
  128. });
  129. addSuccessMessage(t("%s's account has been deleted.", this.state.user?.email));
  130. browserHistory.replace('/manage/users/');
  131. }
  132. async deactivateUser() {
  133. const response = await this.api.requestPromise(this.userEndpoint, {
  134. method: 'PUT',
  135. data: {isActive: false},
  136. });
  137. this.setState({user: response});
  138. this.formModel.setInitialData(response);
  139. addSuccessMessage(t("%s's account has been deactivated.", response.email));
  140. }
  141. removeUser = (actionTypes: DeleteType) =>
  142. actionTypes === 'delete' ? this.deleteUser() : this.deactivateUser();
  143. formModel = new FormModel();
  144. renderBody() {
  145. const {user} = this.state;
  146. if (user === null) {
  147. return null;
  148. }
  149. const openDeleteModal = () =>
  150. openModal(opts => (
  151. <RemoveUserModal user={user} onRemove={this.removeUser} {...opts} />
  152. ));
  153. return (
  154. <Fragment>
  155. <h3>{t('Users')}</h3>
  156. <p>{t('Editing user: %s', user.email)}</p>
  157. <Form
  158. model={this.formModel}
  159. initialData={user}
  160. apiMethod="PUT"
  161. apiEndpoint={this.userEndpoint}
  162. requireChanges
  163. onSubmitError={err => {
  164. addErrorMessage(err?.responseJSON?.detail);
  165. }}
  166. onSubmitSuccess={data => {
  167. this.setState({user: data});
  168. addSuccessMessage(t('User account updated.'));
  169. }}
  170. extraButton={
  171. <Button
  172. onClick={openDeleteModal}
  173. style={{marginLeft: space(1)}}
  174. priority="danger"
  175. >
  176. {t('Remove User')}
  177. </Button>
  178. }
  179. >
  180. <JsonForm forms={[userEditForm]} />
  181. </Form>
  182. </Fragment>
  183. );
  184. }
  185. }
  186. const ModalFooter = styled('div')`
  187. display: grid;
  188. grid-auto-flow: column;
  189. gap: ${space(1)};
  190. justify-content: end;
  191. padding: 20px 30px;
  192. margin: 20px -30px -30px;
  193. border-top: 1px solid ${p => p.theme.border};
  194. `;
  195. export default AdminUserEdit;