-
Notifications
You must be signed in to change notification settings - Fork 12
/
UserManagement.jsx
242 lines (226 loc) · 9.73 KB
/
UserManagement.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import React from 'react';
import { useTranslation, Trans } from 'react-i18next';
import alertifyjs from 'alertifyjs'
import {
Button, Tooltip, Switch, FormControlLabel, FormGroup, FormControl, Dialog
} from '@mui/material';
import { includes } from 'lodash'
import APIService from '../../services/APIService'
import ForgotPasswordForm from './ForgotPasswordForm';
import ConceptContainerDelete from '../common/ConceptContainerDelete';
const UserManagement = ({ user }) => {
const { t } = useTranslation()
const [status, setStatus] = React.useState(user.status)
const [isAdmin, setIsAdmin] = React.useState(user.is_staff)
const [isResettingPassword, setIsResettingPassword] = React.useState(false)
const [isHardDelete, setIsHardDelete] = React.useState(false)
const isVerified = status === 'verified'
const isUnverified = includes(['verification_pending', 'unverified'], status)
const isDeactivated = status === 'deactivated'
const activateLabel = isDeactivated ? t('common.activate') : t('common.deactivate')
const adminToggleLabel = isAdmin ? t('user.home.management.remove_admin_privileges') : t('user.home.management.make_admin')
const userLabel = `<i>${user.name}(${user.username})</i>`
const usernameConfirmationMessage = t('user.home.management.username_confirmation_message')
const usernameIncorrectMessage = t('user.home.management.username_incorrect_message')
const genericError = t('user.home.management.generic_error')
const onMarkVerified = () => {
alertifyjs.prompt(
t('user.home.management.mark_verified.title', {userLabel: userLabel}),
t('user.home.management.mark_verified.body'),
'',
(event, value) => {
if(!value) {
alertifyjs.error(usernameConfirmationMessage, 3)
return false
}
if(value !== user.username) {
alertifyjs.error(usernameIncorrectMessage, 3)
return false
}
setStatus('verified')
markVerified()
},
() => {}
)
}
const onActivateToggle = () => {
const isUserOriginallyDeactivated = user.status === 'deactivated'
const message = isUserOriginallyDeactivated ? t('user.home.management.activate.message') : t('user.home.management.deactivate.message')
alertifyjs.prompt(
`${activateLabel} ${userLabel}`,
`${message}.<br/><br/><b>${usernameConfirmationMessage}</b>`,
'',
(event, value) => {
if(!value) {
alertifyjs.error(usernameConfirmationMessage, 3)
return false
}
if(value !== user.username) {
alertifyjs.error(usernameIncorrectMessage, 3)
return false
}
setStatus(isUserOriginallyDeactivated ? 'verified' : 'deactivated')
isUserOriginallyDeactivated ? activate() : deactivate()
},
() => {}
)
}
const onAdminToggle = () => {
const newIsAdmin = !isAdmin
const message = newIsAdmin ? t('user.home.management.mark_admin.message') : t('user.home.management.unmark_admin.message')
alertifyjs.prompt(
`${adminToggleLabel} ${userLabel}`,
`${message}<br/><br/><b>${usernameConfirmationMessage}</b>`,
'',
(event, value) => {
if(!value) {
alertifyjs.error(usernameConfirmationMessage, 3)
return false
}
if(value !== user.username) {
alertifyjs.error(usernameIncorrectMessage, 3)
return false
}
setIsAdmin(newIsAdmin)
toggleAdmin()
},
() => {}
)
}
const markVerified = () => {
APIService.users(user.username).appendToUrl('verify/unknown-token/').get().then(response => {
if(response.status === 200) {
alertifyjs.success(t('user.home.management.mark_verified.success'), 2, () => window.location.reload())
} else {
alertifyjs.error(genericError, 2, () => window.location.reload())
}
})
}
const deactivate = () => {
APIService.users(user.username).delete().then(response => {
if(response.status === 204)
alertifyjs.success(t('user.home.management.deactivate.success'), 2, () => window.location.reload())
else
alertifyjs.error(genericError, 2, () => window.location.reload())
})
}
const onHardDelete = () => {
APIService.users(user.username).appendToUrl('?hardDelete=true').delete().then(response => {
if(response.status === 204)
alertifyjs.success(t('user.home.management.hard_delete.success'), 2, () => window.location.hash = '/search/?type=users')
else
alertifyjs.error(genericError, 2, () => window.location.reload())
})
}
const activate = () => {
APIService.users(user.username).appendToUrl('reactivate/').put().then(response => {
if(response.status === 204)
alertifyjs.success(t('user.home.management.activate.success'), 2, () => window.location.reload())
else
alertifyjs.error(genericError, 2, () => window.location.reload())
})
}
const toggleAdmin = () => {
APIService.users(user.username).appendToUrl('staff/').put().then(response => {
if(response.status === 204)
alertifyjs.success(t('user.home.management.mark_admin.success'), 2, () => window.location.reload())
else
alertifyjs.error(genericError, 2, () => window.location.reload())
})
}
const resetPassword = newPassword => {
APIService.users(user.username).put({password: newPassword}).then(response => {
setIsResettingPassword(false)
if(response.status === 200)
alertifyjs.success(t('user.home.management.reset_password.success'))
else
alertifyjs.error(genericError, 2, () => window.location.reload())
})
}
return (
<div className='col-xs-12 no-side-padding'>
<fieldset style={{border: `1px solid rgba(255, 129, 130, 0.4)`, width: '100%', borderRadius: '4px'}}>
<legend style={{color: 'rgba(255, 129, 130)', fontStyle: 'italic'}}> {t('user.home.management.title')} </legend>
<FormControl component='fieldset' variant='standard'>
<h4 style={{margin: '4px 0'}}>{t('user.home.management.header')}</h4>
<FormGroup>
<FormControlLabel
control={
<Tooltip arrow title={t('user.home.management.tooltip.mark_verified')} placement='right'>
<span>
<Switch checked={isVerified} onChange={onMarkVerified} color='primary' disabled={isDeactivated || !isUnverified}/>
</span>
</Tooltip>
}
label={<span style={{fontSize: '0.9125rem'}}>{t('user.home.management.mark_verified.label')}</span>}
/>
<FormControlLabel
control={
<Tooltip arrow title={t('user.home.management.tooltip.activate', {activate_or_deactivate: activateLabel})} placement='right'>
<span>
<Switch checked={!isDeactivated} onChange={onActivateToggle} color='primary'/>
</span>
</Tooltip>
}
label={<span style={{fontSize: '0.9125rem'}}>{t('user.home.management.activate.label')}</span>}
/>
<FormControlLabel
control={
<Tooltip arrow title={adminToggleLabel} placement='right'>
<span>
<Switch checked={isAdmin} onChange={onAdminToggle} color='primary' disabled={!isVerified || isDeactivated} />
</span>
</Tooltip>
}
label={<span style={{fontSize: '0.9125rem'}}>{t('common.admin')}</span>}
/>
</FormGroup>
</FormControl>
<Tooltip arrow title={t('user.home.management.tooltip.reset_password')} placement='right'>
<span>
<Button color='error' variant={isResettingPassword ? 'contained' : 'outlined'} size='small' style={{textTransform: 'none', marginRight: '10px'}} onClick={() => setIsResettingPassword(true)} disabled={isDeactivated || !isVerified}>
{t('user.home.management.reset_password.label')}
</Button>
</span>
</Tooltip>
<Tooltip arrow title={t('user.home.management.tooltip.hard_delete')} placement='right'>
<span>
<Button color='error' variant={isHardDelete ? 'contained' : 'outlined'} size='small' style={{textTransform: 'none'}} onClick={() => setIsHardDelete(true)}>
{t('user.home.management.hard_delete.label')}
</Button>
</span>
</Tooltip>
{
isResettingPassword &&
<Dialog open={isResettingPassword} onClose={() => setIsResettingPassword(false)} maxWidth="md" fullWidth>
<div className='col-xs-12 no-side-padding' style={{marginBottom: '25px'}}>
<ForgotPasswordForm match={{params: {user: user.username, token: 'unknown-token'}}} forceReset user={user} onSubmit={resetPassword} />
</div>
</Dialog>
}
{
isHardDelete &&
<ConceptContainerDelete
open={isHardDelete}
resource={{...user, short_code: user.username}}
onClose={() => setIsHardDelete(false)}
associatedResources={['organizations', 'sources', 'collections']}
associationRelation='owned'
onDelete={onHardDelete}
summaryContent={
<React.Fragment>
<p>{t('user.this_user_own')}:</p>
<ul>
<li>{t('common.resoures.orgs')}: {user.owned_orgs}</li>
<li>{t('common.resoures.sources')}: {user.sources}</li>
<li>{t('common.resoures.collections')}: {user.collections}</li>
</ul>
</React.Fragment>
}
/>
}
</fieldset>
</div>
)
}
export default UserManagement;