-
Notifications
You must be signed in to change notification settings - Fork 12
/
UserManagement.jsx
236 lines (221 loc) · 9.61 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
import React from 'react';
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 [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 ? 'Activate' : 'Deactivate'
const adminToggleLabel = isAdmin ? 'Remove Admin Privileges' : 'Make Admin'
const userLabel = `<i>${user.name}(${user.username})</i>`
const onMarkVerified = () => {
alertifyjs.prompt(
`Mark Verified ${userLabel}`,
`This action will mark the user as <i>Verified</i> and will enable the login.<br/><br/><b>Type the username of this user to confirm this action.</b>`,
'',
(event, value) => {
if(!value) {
alertifyjs.error('Type the username of this user to confirm this action.', 3)
return false
}
if(value !== user.username) {
alertifyjs.error('Incorrect username entered.', 3)
return false
}
setStatus('verified')
markVerified()
},
() => {}
)
}
const onActivateToggle = () => {
const isUserOriginallyDeactivated = user.status === 'deactivated'
const message = isUserOriginallyDeactivated ? 'This action will mark the user as active and verified, which will enable the login' : 'This action will mark the is_active=False which will deactivate the user login'
alertifyjs.prompt(
`${activateLabel} ${userLabel}`,
`${message}.<br/><br/><b>Type the username of this user to confirm this action.</b>`,
'',
(event, value) => {
if(!value) {
alertifyjs.error('Type the username of this user to confirm this action.', 3)
return false
}
if(value !== user.username) {
alertifyjs.error('Incorrect username entered.', 3)
return false
}
setStatus(isUserOriginallyDeactivated ? 'verified' : 'deactivated')
isUserOriginallyDeactivated ? activate() : deactivate()
},
() => {}
)
}
const onAdminToggle = () => {
const newIsAdmin = !isAdmin
const message = newIsAdmin ? 'This action will make the user OCL Admin (not superuser).' : 'This action will remove all admin privileges from this user.'
alertifyjs.prompt(
`${adminToggleLabel} ${userLabel}`,
`${message}<br/><br/><b>Type the username of this user to confirm this action.</b>`,
'',
(event, value) => {
if(!value) {
alertifyjs.error('Type the username of this user to confirm this action.', 3)
return false
}
if(value !== user.username) {
alertifyjs.error('Incorrect username entered.', 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('Successfully marked this user verified. Reloading...', 2, () => window.location.reload())
} else {
alertifyjs.error('Something bad happened! Reloading to refresh User state.', 2, () => window.location.reload())
}
})
}
const deactivate = () => {
APIService.users(user.username).delete().then(response => {
if(response.status === 204)
alertifyjs.success('Successfully deactivated this user. Reloading...', 2, () => window.location.reload())
else
alertifyjs.error('Something bad happened! Reloading to refresh User state.', 2, () => window.location.reload())
})
}
const onHardDelete = () => {
APIService.users(user.username).appendToUrl('?hardDelete=true').delete().then(response => {
if(response.status === 204)
alertifyjs.success('Successfully deleted this user. Reloading...', 2, () => window.location.reload())
else
alertifyjs.error('Something bad happened! Reloading to refresh User state.', 2, () => window.location.reload())
})
}
const activate = () => {
APIService.users(user.username).appendToUrl('reactivate/').put().then(response => {
if(response.status === 204)
alertifyjs.success('Successfully activated this user. Reloading...', 2, () => window.location.reload())
else
alertifyjs.error('Something bad happened! Reloading to refresh User state.', 2, () => window.location.reload())
})
}
const toggleAdmin = () => {
APIService.users(user.username).appendToUrl('staff/').put().then(response => {
if(response.status === 204)
alertifyjs.success('Successfully toggle user state. Reloading...', 2, () => window.location.reload())
else
alertifyjs.error('Something bad happened! Reloading to refresh User state.', 2, () => window.location.reload())
})
}
const resetPassword = newPassword => {
APIService.users(user.username).put({password: newPassword}).then(response => {
setIsResettingPassword(false)
if(response.status === 200)
alertifyjs.success('Successfully resetted user password. Reloading...')
else
alertifyjs.error('Something bad happened! Reloading to refresh User state.', 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'}}> Admin Only </legend>
<FormControl component='fieldset' variant='standard'>
<h4 style={{margin: '4px 0'}}>User Management</h4>
<FormGroup>
<FormControlLabel
control={
<Tooltip arrow title="Mark this user as verified. This will allow them to login with there password." placement='right'>
<span>
<Switch checked={isVerified} onChange={onMarkVerified} color='primary' disabled={isDeactivated || !isUnverified}/>
</span>
</Tooltip>
}
label={<span style={{fontSize: '0.9125rem'}}>Verified</span>}
/>
<FormControlLabel
control={
<Tooltip arrow title={`${activateLabel} this user.`} placement='right'>
<span>
<Switch checked={!isDeactivated} onChange={onActivateToggle} color='primary'/>
</span>
</Tooltip>
}
label={<span style={{fontSize: '0.9125rem'}}>Activated</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'}}>Admin</span>}
/>
</FormGroup>
</FormControl>
<Tooltip arrow title="Reset this user's password." placement='right'>
<span>
<Button color='error' variant={isResettingPassword ? 'contained' : 'outlined'} size='small' style={{textTransform: 'none', marginRight: '10px'}} onClick={() => setIsResettingPassword(true)} disabled={isDeactivated || !isVerified}>
Reset Password
</Button>
</span>
</Tooltip>
<Tooltip arrow title="Delete this user" placement='right'>
<span>
<Button color='error' variant={isHardDelete ? 'contained' : 'outlined'} size='small' style={{textTransform: 'none', marginTop: '5px'}} onClick={() => setIsHardDelete(true)}>
Hard Delete
</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>This user own:</p>
<ul>
<li>Organizations: {user.owned_orgs}</li>
<li>Sources: {user.sources}</li>
<li>Collections: {user.collections}</li>
</ul>
</React.Fragment>
}
/>
}
</fieldset>
</div>
)
}
export default UserManagement;