2019-05-25 16:41:27 +00:00
|
|
|
import React, { Fragment } from 'react';
|
2019-05-25 08:45:49 +00:00
|
|
|
import { ValidatorForm } from 'react-material-ui-form-validator';
|
2019-05-21 22:34:48 +00:00
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
import { Table, TableBody, TableCell, TableHead, TableFooter, TableRow } from '@material-ui/core';
|
|
|
|
import { Box, Button, Typography, } from '@material-ui/core';
|
|
|
|
|
2019-05-21 22:34:48 +00:00
|
|
|
import EditIcon from '@material-ui/icons/Edit';
|
|
|
|
import DeleteIcon from '@material-ui/icons/Delete';
|
2019-05-25 08:45:49 +00:00
|
|
|
import CloseIcon from '@material-ui/icons/Close';
|
|
|
|
import CheckIcon from '@material-ui/icons/Check';
|
2019-05-24 11:19:27 +00:00
|
|
|
import IconButton from '@material-ui/core/IconButton';
|
2019-11-30 12:34:52 +00:00
|
|
|
import SaveIcon from '@material-ui/icons/Save';
|
|
|
|
import PersonAddIcon from '@material-ui/icons/PersonAdd';
|
2019-05-24 11:19:27 +00:00
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
import { withAuthenticatedContext, AuthenticatedContextProps } from '../authentication';
|
|
|
|
import { RestFormProps, FormActions, FormButton } from '../components';
|
|
|
|
|
2019-05-24 11:19:27 +00:00
|
|
|
import UserForm from './UserForm';
|
2020-02-09 10:21:13 +00:00
|
|
|
import { SecuritySettings, User } from './types';
|
2019-05-21 22:34:48 +00:00
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
function compareUsers(a: User, b: User) {
|
2019-05-24 11:19:27 +00:00
|
|
|
if (a.username < b.username) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.username > b.username) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
type ManageUsersFormProps = RestFormProps<SecuritySettings> & AuthenticatedContextProps;
|
2019-05-21 22:34:48 +00:00
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
type ManageUsersFormState = {
|
|
|
|
creating: boolean;
|
|
|
|
user?: User;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ManageUsersForm extends React.Component<ManageUsersFormProps, ManageUsersFormState> {
|
|
|
|
|
|
|
|
state: ManageUsersFormState = {
|
|
|
|
creating: false
|
|
|
|
};
|
2019-05-24 11:19:27 +00:00
|
|
|
|
|
|
|
createUser = () => {
|
|
|
|
this.setState({
|
|
|
|
creating: true,
|
|
|
|
user: {
|
|
|
|
username: "",
|
|
|
|
password: "",
|
2019-05-25 08:45:49 +00:00
|
|
|
admin: true
|
2019-05-24 11:19:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
uniqueUsername = (username: string) => {
|
|
|
|
return !this.props.data.users.find(u => u.username === username);
|
2019-05-24 11:19:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 08:45:49 +00:00
|
|
|
noAdminConfigured = () => {
|
2020-02-09 10:21:13 +00:00
|
|
|
return !this.props.data.users.find(u => u.admin);
|
2019-05-24 11:19:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
removeUser = (user: User) => {
|
|
|
|
const { data } = this.props;
|
|
|
|
const users = data.users.filter(u => u.username !== user.username);
|
|
|
|
this.props.setData({ ...data, users });
|
2019-05-25 16:41:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
startEditingUser = (user: User) => {
|
2019-05-24 11:19:27 +00:00
|
|
|
this.setState({
|
|
|
|
creating: false,
|
|
|
|
user
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
cancelEditingUser = () => {
|
|
|
|
this.setState({
|
|
|
|
user: undefined
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
doneEditingUser = () => {
|
|
|
|
const { user } = this.state;
|
2020-02-09 10:21:13 +00:00
|
|
|
if (user) {
|
|
|
|
const { data } = this.props;
|
|
|
|
const users = data.users.filter(u => u.username !== user.username);
|
|
|
|
users.push(user);
|
|
|
|
this.props.setData({ ...data, users });
|
|
|
|
this.setState({
|
|
|
|
user: undefined
|
|
|
|
});
|
|
|
|
}
|
2019-05-24 11:19:27 +00:00
|
|
|
};
|
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
handleUserValueChange = (name: keyof User) => (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
this.setState({ user: { ...this.state.user!, [name]: event.target.value } });
|
2019-05-24 11:19:27 +00:00
|
|
|
};
|
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
handleUserCheckboxChange = (name: keyof User) => (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
this.setState({ user: { ...this.state.user!, [name]: event.target.checked } });
|
2019-05-25 08:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit = () => {
|
2020-02-09 10:21:13 +00:00
|
|
|
this.props.saveData();
|
|
|
|
this.props.authenticatedContext.refresh();
|
2019-05-25 08:45:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 22:34:48 +00:00
|
|
|
render() {
|
2020-02-09 10:21:13 +00:00
|
|
|
const { data, loadData } = this.props;
|
2019-05-24 11:19:27 +00:00
|
|
|
const { user, creating } = this.state;
|
2019-05-21 22:34:48 +00:00
|
|
|
return (
|
2019-08-09 17:21:28 +00:00
|
|
|
<Fragment>
|
|
|
|
<ValidatorForm onSubmit={this.onSubmit}>
|
2020-02-09 10:21:13 +00:00
|
|
|
<Table size="small">
|
2019-08-09 17:21:28 +00:00
|
|
|
<TableHead>
|
|
|
|
<TableRow>
|
|
|
|
<TableCell>Username</TableCell>
|
|
|
|
<TableCell align="center">Admin?</TableCell>
|
|
|
|
<TableCell />
|
|
|
|
</TableRow>
|
|
|
|
</TableHead>
|
|
|
|
<TableBody>
|
2020-02-09 10:21:13 +00:00
|
|
|
{data.users.sort(compareUsers).map(user => (
|
2019-08-09 17:21:28 +00:00
|
|
|
<TableRow key={user.username}>
|
|
|
|
<TableCell component="th" scope="row">
|
|
|
|
{user.username}
|
|
|
|
</TableCell>
|
|
|
|
<TableCell align="center">
|
|
|
|
{
|
|
|
|
user.admin ? <CheckIcon /> : <CloseIcon />
|
|
|
|
}
|
|
|
|
</TableCell>
|
|
|
|
<TableCell align="center">
|
2020-02-19 00:04:57 +00:00
|
|
|
<IconButton size="small" aria-label="Delete" onClick={() => this.removeUser(user)}>
|
2019-08-09 17:21:28 +00:00
|
|
|
<DeleteIcon />
|
|
|
|
</IconButton>
|
2020-02-19 00:04:57 +00:00
|
|
|
<IconButton size="small" aria-label="Edit" onClick={() => this.startEditingUser(user)}>
|
2019-08-09 17:21:28 +00:00
|
|
|
<EditIcon />
|
|
|
|
</IconButton>
|
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
))}
|
|
|
|
</TableBody>
|
|
|
|
<TableFooter>
|
|
|
|
<TableRow>
|
|
|
|
<TableCell colSpan={2} />
|
|
|
|
<TableCell align="center">
|
2019-11-30 12:34:52 +00:00
|
|
|
<Button startIcon={<PersonAddIcon />} variant="contained" color="secondary" onClick={this.createUser}>
|
2019-08-09 17:21:28 +00:00
|
|
|
Add User
|
|
|
|
</Button>
|
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
</TableFooter>
|
|
|
|
</Table>
|
|
|
|
{
|
|
|
|
this.noAdminConfigured() &&
|
|
|
|
<Typography component="div" variant="body1">
|
|
|
|
<Box bgcolor="error.main" color="error.contrastText" p={2} mt={2} mb={2}>
|
|
|
|
You must have at least one admin user configured.
|
|
|
|
</Box>
|
2019-05-26 18:09:34 +00:00
|
|
|
</Typography>
|
2019-08-09 17:21:28 +00:00
|
|
|
}
|
2020-02-09 10:21:13 +00:00
|
|
|
<FormActions>
|
|
|
|
<FormButton startIcon={<SaveIcon />} variant="contained" color="primary" type="submit" disabled={this.noAdminConfigured()}>
|
|
|
|
Save
|
|
|
|
</FormButton>
|
|
|
|
<FormButton variant="contained" color="secondary" onClick={loadData}>
|
|
|
|
Reset
|
|
|
|
</FormButton>
|
|
|
|
</FormActions>
|
2019-08-09 17:21:28 +00:00
|
|
|
</ValidatorForm>
|
|
|
|
{
|
|
|
|
user &&
|
|
|
|
<UserForm
|
|
|
|
user={user}
|
|
|
|
creating={creating}
|
|
|
|
onDoneEditing={this.doneEditingUser}
|
|
|
|
onCancelEditing={this.cancelEditingUser}
|
|
|
|
handleValueChange={this.handleUserValueChange}
|
|
|
|
uniqueUsername={this.uniqueUsername}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
</Fragment>
|
2019-05-21 22:34:48 +00:00
|
|
|
);
|
|
|
|
}
|
2019-05-24 11:19:27 +00:00
|
|
|
|
2019-05-21 22:34:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 10:21:13 +00:00
|
|
|
export default withAuthenticatedContext(ManageUsersForm);
|