|
| 1 | +import {browserHistory} from 'react-router'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import React from 'react'; |
| 4 | +import styled from 'react-emotion'; |
| 5 | + |
| 6 | +import {addErrorMessage, addSuccessMessage} from 'app/actionCreators/indicator'; |
| 7 | +import {openModal} from 'app/actionCreators/modal'; |
| 8 | +import {t, tct} from 'app/locale'; |
| 9 | +import AsyncView from 'app/views/asyncView'; |
| 10 | +import Button from 'app/components/button'; |
| 11 | +import Form from 'app/views/settings/components/forms/form'; |
| 12 | +import FormModel from 'app/views/settings/components/forms/model'; |
| 13 | +import JsonForm from 'app/views/settings/components/forms/jsonForm'; |
| 14 | +import RadioGroup from 'app/views/settings/components/forms/controls/radioGroup'; |
| 15 | +import SentryTypes from 'app/sentryTypes'; |
| 16 | +import space from 'app/styles/space'; |
| 17 | + |
| 18 | +const userEditForm = { |
| 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 | + |
| 69 | +const REMOVE_BUTTON_LABEL = { |
| 70 | + disable: t('Disable User'), |
| 71 | + delete: t('Permanently Delete User'), |
| 72 | +}; |
| 73 | + |
| 74 | +class RemoveUserModal extends React.Component { |
| 75 | + static propTypes = { |
| 76 | + user: SentryTypes.User, |
| 77 | + onRemove: PropTypes.func, |
| 78 | + closeModal: PropTypes.func, |
| 79 | + }; |
| 80 | + |
| 81 | + state = { |
| 82 | + deleteType: 'disable', |
| 83 | + }; |
| 84 | + |
| 85 | + onRemove = () => { |
| 86 | + this.props.onRemove(this.state.deleteType); |
| 87 | + this.props.closeModal(); |
| 88 | + }; |
| 89 | + |
| 90 | + render() { |
| 91 | + const {user} = this.props; |
| 92 | + const {deleteType} = this.state; |
| 93 | + |
| 94 | + return ( |
| 95 | + <React.Fragment> |
| 96 | + <p>{tct('Removing user [user]', {user: <strong>{user.email}</strong>})}</p> |
| 97 | + <RadioGroup |
| 98 | + value={deleteType} |
| 99 | + onChange={type => this.setState({deleteType: type})} |
| 100 | + choices={[ |
| 101 | + ['disable', t('Disable the account.')], |
| 102 | + ['delete', t('Permanently remove the user and their data.')], |
| 103 | + ]} |
| 104 | + /> |
| 105 | + <ModalFooter> |
| 106 | + <Button priority="danger" onClick={this.onRemove}> |
| 107 | + {REMOVE_BUTTON_LABEL[deleteType]} |
| 108 | + </Button> |
| 109 | + <Button onClick={this.props.closeModal}>{t('Nevermind')}</Button> |
| 110 | + </ModalFooter> |
| 111 | + </React.Fragment> |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +class AdminUserEdit extends AsyncView { |
| 117 | + get userEndpoint() { |
| 118 | + const {params} = this.props; |
| 119 | + return `/users/${params.id}/`; |
| 120 | + } |
| 121 | + |
| 122 | + getEndpoints() { |
| 123 | + return [['user', this.userEndpoint]]; |
| 124 | + } |
| 125 | + |
| 126 | + async deleteUser() { |
| 127 | + await this.api.requestPromise(this.userEndpoint, { |
| 128 | + method: 'DELETE', |
| 129 | + data: {hardDelete: true, organizations: []}, |
| 130 | + }); |
| 131 | + |
| 132 | + addSuccessMessage(t("%s's account has been deleted.", this.state.user.email)); |
| 133 | + browserHistory.replace('/manage/users/'); |
| 134 | + } |
| 135 | + |
| 136 | + async deactivateUser() { |
| 137 | + const response = await this.api.requestPromise(this.userEndpoint, { |
| 138 | + method: 'PUT', |
| 139 | + data: {isActive: false}, |
| 140 | + }); |
| 141 | + |
| 142 | + this.setState({user: response}); |
| 143 | + this.formModel.setInitialData(response); |
| 144 | + addSuccessMessage(t("%s's account has been deactivated.", response.email)); |
| 145 | + } |
| 146 | + |
| 147 | + removeUser = actionTypes => |
| 148 | + actionTypes === 'delete' ? this.deleteUser() : this.deactivateUser(); |
| 149 | + |
| 150 | + formModel = new FormModel(); |
| 151 | + |
| 152 | + renderBody() { |
| 153 | + const {user} = this.state; |
| 154 | + const openDeleteModal = () => |
| 155 | + openModal(opts => ( |
| 156 | + <RemoveUserModal user={user} onRemove={this.removeUser} {...opts} /> |
| 157 | + )); |
| 158 | + |
| 159 | + return ( |
| 160 | + <React.Fragment> |
| 161 | + <h3>{t('Users')}</h3> |
| 162 | + <p>{t('Editing user: %s', user.email)}</p> |
| 163 | + <Form |
| 164 | + model={this.formModel} |
| 165 | + initialData={user} |
| 166 | + apiMethod="PUT" |
| 167 | + apiEndpoint={this.userEndpoint} |
| 168 | + requireChanges |
| 169 | + onSubmitError={addErrorMessage} |
| 170 | + onSubmitSuccess={data => { |
| 171 | + this.setState({user: data}); |
| 172 | + addSuccessMessage('User account updated.'); |
| 173 | + }} |
| 174 | + extraButton={ |
| 175 | + <Button |
| 176 | + type="button" |
| 177 | + onClick={openDeleteModal} |
| 178 | + style={{marginLeft: space(1)}} |
| 179 | + priority="danger" |
| 180 | + > |
| 181 | + {t('Remove User')} |
| 182 | + </Button> |
| 183 | + } |
| 184 | + > |
| 185 | + <JsonForm forms={[userEditForm]} /> |
| 186 | + </Form> |
| 187 | + </React.Fragment> |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +const ModalFooter = styled('div')` |
| 193 | + display: grid; |
| 194 | + grid-auto-flow: column; |
| 195 | + grid-gap: ${space(1)}; |
| 196 | + justify-content: end; |
| 197 | + padding: 20px 30px; |
| 198 | + margin: 20px -30px -30px; |
| 199 | + border-top: 1px solid ${p => p.theme.borderLight}; |
| 200 | +`; |
| 201 | + |
| 202 | +export default AdminUserEdit; |
0 commit comments