adminUserEdit.tsx 5.7 KB

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