-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathUserAdminDetails.jsx
327 lines (321 loc) · 9.31 KB
/
UserAdminDetails.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import React from "react";
import TextField from "@material-ui/core/TextField";
import Grid from "@material-ui/core/Grid";
import { createUser } from "./util/api";
import { generate } from "generate-password";
import Typography from "@material-ui/core/Typography";
import UserGroupsPicker from "./UserGroupsPicker";
import RefreshIcon from "@material-ui/icons/Refresh";
import IconButton from "@material-ui/core/IconButton";
import InputAdornment from "@material-ui/core/InputAdornment";
import GroupServerPermissions from "./GroupServerPermissions";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Switch from "@material-ui/core/Switch";
import LockIcon from "@material-ui/icons/Lock";
import LockOpenIcon from "@material-ui/icons/LockOpen";
import SubmitButtons from "./SubmitButtons";
import ErrorDialog from "./ErrorDialog";
import {
getUser,
editGroupMemberships,
editUser,
editGroupServers
} from "./util/api";
var zxcvbn = require("zxcvbn");
class UserAdminDetails extends React.Component {
state = {
name: "",
username_helper_text: "",
password: "",
password_helper_text: "",
require_two_factor: false,
edit_mode: false,
groups: [],
servers: [],
group_id: null,
is_admin: false,
password_strength: null,
has_two_factor: false,
two_factor_can_be_disabled: false,
pageError: false,
errors: { message: "" }
};
async componentDidMount() {
const json = getUser(this.props.item_id);
try {
this.setState(await json);
this.setState({ edit_mode: true });
if ((await json)["has_two_factor"]) {
this.setState({ two_factor_can_be_disabled: true });
}
} catch (err) {
if (err.code !== 404) {
this.setState({ hasError: true, error: err });
}
}
}
generatePassword = event => {
var pass = generate({ length: 16, numbers: true, symbols: true });
var passStrength = zxcvbn(pass);
this.setState({
password: pass,
password_strength: passStrength.score
});
};
setTwoFactorRequired = event => {
this.setState({ require_two_factor: event.target.checked });
};
setHasTwoFactor = event => {
this.setState({ has_two_factor: event.target.checked });
};
setAdmin = event => {
this.setState({ is_admin: event.target.checked });
};
handleChange = name => event => {
this.setState({
pageError: false,
errors: ""
});
var state = {
[name]: event.target.value
};
if (name === "name") {
var letters = /^[A-Za-z0-9_]+$/;
let username = event.target.value;
if (username.match(letters)) {
state = Object.assign(state, {
username_helper_text: ""
});
} else if (username.length == 0) {
state = Object.assign(state, {
username_helper_text: "Username can not be blank."
});
} else {
state = Object.assign(state, {
username_helper_text:
"Username may only contain letters, numbers and underscores."
});
}
}
if (name === "password") {
var passStrength = zxcvbn(event.target.value);
console.log(passStrength.feedback.warning);
state = Object.assign(state, {
password_strength: passStrength.score,
password_helper_text: passStrength.feedback.suggestions
});
}
this.setState(state);
};
updateGroups = groups => {
this.setState({ groups: groups });
};
updateServers = servers => {
this.setState({ servers: servers });
};
handleSubmit = () => {
const { item_id, onClick } = this.props;
const {
edit_mode,
name,
password,
servers,
groups,
is_admin,
username_helper_text,
password_strength,
require_two_factor,
has_two_factor
} = this.state;
if (
username_helper_text === "" &&
(password.length === 0 || password_strength > 3)
) {
var task;
var uid;
if (edit_mode) {
task = editUser(
item_id,
name,
password ? password.length > 0 : undefined,
is_admin,
require_two_factor,
has_two_factor
);
} else {
task = createUser(name, password, is_admin, require_two_factor);
}
task
.then(json => {
uid = json.id;
return editGroupServers(json.group_id, servers);
})
.then(json => {
return editGroupMemberships(uid, groups);
})
.then(json => {
onClick();
})
.catch(err => {
if (err.code === 400) {
this.setState({ pageError: true, errors: err });
} else {
this.setState({ hasError: true, error: err });
}
});
}
};
render() {
if (this.state.hasError) throw this.state.error;
const { classes, onClick, item_id } = this.props;
const {
name,
password,
group_id,
servers,
edit_mode,
password_strength,
require_two_factor,
has_two_factor,
two_factor_can_be_disabled,
is_admin
} = this.state;
return (
<React.Fragment>
<Grid xs={12}>
<Typography variant="h5" component="h1">
{(edit_mode && "Edit User") || "New User"}
</Typography>
</Grid>
<Grid xs={6}>
<TextField
id="username"
label="Username"
className={classes.textField}
required={true}
value={name}
onChange={this.handleChange("name")}
margin="normal"
InputLabelProps={{ shrink: true }}
error={this.state.username_helper_text}
helperText={this.state.username_helper_text}
/>
</Grid>
<Grid xs={6}>
<TextField
id="password"
className={classes.textField}
value={password}
required={true}
label="Reset Password"
onChange={this.handleChange("password")}
margin="normal"
error={this.state.password_helper_text[0]}
helperText={this.state.password_helper_text[0]}
InputProps={{
endAdornment: (
<InputAdornment position="end">
<IconButton
color="inherit"
className={classes.button}
aria-label="New password"
onClick={this.generatePassword}
>
<RefreshIcon />
</IconButton>
{(password_strength || password_strength === 0) &&
((password_strength > 3 && <LockIcon />) || (
<LockOpenIcon color="secondary" />
))}
</InputAdornment>
)
}}
/>
</Grid>
<Grid xs={12}>
<Typography variant="h5" component="h1">
Administrator Rights
</Typography>
</Grid>
<Grid xs={2}>
<FormControlLabel
control={
<Switch
checked={is_admin}
onChange={this.setAdmin}
value="is_admin"
/>
}
label={(is_admin && "User is admin") || "User is not admin"}
/>
</Grid>
<Grid xs={12}>
<Typography variant="h5" component="h1">
Two-Factor Authentication
</Typography>
</Grid>
<Grid xs={2}>
<FormControlLabel
control={
<Switch
checked={require_two_factor}
onChange={this.setTwoFactorRequired}
value="require_two_factor"
/>
}
label={
(require_two_factor && "Two-factor authentication required") ||
"Two-factor authentication not required"
}
/>
</Grid>
<Grid xs={2}>
<FormControlLabel
control={
<Switch
checked={has_two_factor}
onChange={this.setHasTwoFactor}
value="has_two_factor"
disabled={!two_factor_can_be_disabled}
/>
}
label={
(has_two_factor && "Two-factor authentication enabled") ||
"Two-factor authentication not enabled"
}
/>
</Grid>
<Grid xs={12}>
<Typography variant="h5" component="h1">
Group Memberships
</Typography>
</Grid>
<Grid xs={12}>
<UserGroupsPicker
user_id={item_id}
updateGroups={this.updateGroups}
/>
</Grid>
<GroupServerPermissions
group_id={group_id}
updateServers={this.updateServers}
servers={servers}
classes={classes}
/>
<ErrorDialog
open={this.state.pageError}
message={this.state.errors.message}
/>
<Grid item xs={12} />
<SubmitButtons
handleSubmit={this.handleSubmit}
onClick={this.handleSubmit}
/>
</React.Fragment>
);
}
}
export default UserAdminDetails;