remove roles, as a simplification

This commit is contained in:
Rick Watson 2019-05-25 09:45:49 +01:00
parent 0c630f0f93
commit 6935b63706
5 changed files with 59 additions and 109 deletions

View File

@ -1,16 +1,15 @@
{
"jwt_secret":"esp8266-react",
"roles": ["admin", "guest"],
"users": [
{
"username": "admin",
"password": "admin",
"role": "admin"
"admin": true
},
{
"username": "guest",
"password": "guest",
"role": "guest"
"admin": false
}
]
}

View File

@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { TextValidator, ValidatorForm } from 'react-material-ui-form-validator';
import { ValidatorForm } from 'react-material-ui-form-validator';
import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
@ -13,14 +13,16 @@ import TableCell from '@material-ui/core/TableCell';
import TableFooter from '@material-ui/core/TableFooter';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Chip from '@material-ui/core/Chip';
import EditIcon from '@material-ui/icons/Edit';
import DeleteIcon from '@material-ui/icons/Delete';
import CloseIcon from '@material-ui/icons/Close';
import CheckIcon from '@material-ui/icons/Check';
import IconButton from '@material-ui/core/IconButton';
import SectionContent from '../components/SectionContent';
import UserForm from './UserForm';
import { withAuthenticationContext } from '../authentication/Context';
const styles = theme => ({
loadingSettings: {
@ -30,26 +32,15 @@ const styles = theme => ({
margin: theme.spacing.unit * 4,
textAlign: "center"
},
switchControl: {
width: "100%",
marginTop: theme.spacing.unit * 2,
marginBottom: theme.spacing.unit
},
textField: {
width: "100%"
},
button: {
marginRight: theme.spacing.unit * 2,
marginTop: theme.spacing.unit * 2,
},
chip: {
margin: theme.spacing.unit,
},
table: {
'& td, & th': { padding: theme.spacing.unit }
},
actions: {
color: theme.palette.text.secondary,
whiteSpace: "nowrap"
}
});
@ -76,7 +67,7 @@ class ManageUsersForm extends React.Component {
user: {
username: "",
password: "",
roles: []
admin: true
}
});
};
@ -85,8 +76,8 @@ class ManageUsersForm extends React.Component {
return !this.props.userData.users.find(u => u.username === username);
}
usersValid = username => {
return !!this.props.userData.users.find(u => u.roles.includes("admin"));
noAdminConfigured = () => {
return !this.props.userData.users.find(u => u.admin);
}
startEditingUser = user => {
@ -102,10 +93,6 @@ class ManageUsersForm extends React.Component {
});
}
sortedUsers(users) {
return users.sort(compareUsers);
}
doneEditingUser = () => {
const { user } = this.state;
const { userData } = this.props;
@ -120,17 +107,29 @@ class ManageUsersForm extends React.Component {
handleUserValueChange = name => event => {
const { user } = this.state;
if (user) {
this.setState({
user: {
...user, [name]: event.target.value
}
});
}
this.setState({
user: {
...user, [name]: event.target.value
}
});
};
handleUserCheckboxChange = name => event => {
const { user } = this.state;
this.setState({
user: {
...user, [name]: event.target.checked
}
});
}
onSubmit = () => {
this.props.onSubmit();
this.props.authenticationContex.refresh();
}
render() {
const { classes, userData, userDataFetched, errorMessage, onSubmit, onReset, handleValueChange } = this.props;
const { classes, userData, userDataFetched, errorMessage, onReset } = this.props;
const { user, creating } = this.state;
return (
<SectionContent title="Manage Users">
@ -148,33 +147,31 @@ class ManageUsersForm extends React.Component {
<UserForm
user={user}
creating={creating}
roles={userData.roles}
onDoneEditing={this.doneEditingUser}
onCancelEditing={this.cancelEditingUser}
handleValueChange={this.handleUserValueChange}
handleCheckboxChange={this.handleUserCheckboxChange}
uniqueUsername={this.uniqueUsername}
/>
:
<ValidatorForm onSubmit={onSubmit}>
<ValidatorForm onSubmit={this.onSubmit}>
<Table className={classes.table}>
<TableHead>
<TableRow>
<TableCell>Username</TableCell>
<TableCell align="center">Role(s)</TableCell>
<TableCell align="center">Admin?</TableCell>
<TableCell align="center">Action</TableCell>
</TableRow>
</TableHead>
<TableBody>
{this.sortedUsers(userData.users).map(user => (
{userData.users.sort(compareUsers).map(user => (
<TableRow key={user.username}>
<TableCell component="th" scope="row">
{user.username}
</TableCell>
<TableCell align="center">
{
user.roles.map(role => (
<Chip label={role} className={classes.chip} />
))
user.admin ? <CheckIcon /> : <CloseIcon />
}
</TableCell>
<TableCell align="center">
@ -192,7 +189,7 @@ class ManageUsersForm extends React.Component {
<TableRow>
<TableCell colSpan={2}>
{
!this.usersValid() &&
this.noAdminConfigured() &&
<Typography variant="body1" color="error">
You must have at least one admin user configured.
</Typography>
@ -206,7 +203,7 @@ class ManageUsersForm extends React.Component {
</TableRow>
</TableFooter>
</Table>
<Button variant="contained" color="primary" className={classes.button} type="submit">
<Button variant="contained" color="primary" className={classes.button} type="submit" disabled={this.noAdminConfigured()}>
Save
</Button>
<Button variant="contained" color="secondary" className={classes.button} onClick={onReset}>
@ -230,6 +227,7 @@ class ManageUsersForm extends React.Component {
}
ManageUsersForm.propTypes = {
authenticationContex: PropTypes.object.isRequired,
classes: PropTypes.object.isRequired,
userData: PropTypes.object,
userDataFetched: PropTypes.bool.isRequired,
@ -240,4 +238,4 @@ ManageUsersForm.propTypes = {
handleValueChange: PropTypes.func.isRequired
};
export default withStyles(styles)(ManageUsersForm);
export default withAuthenticationContext(withStyles(styles)(ManageUsersForm));

View File

@ -7,12 +7,9 @@ import Button from '@material-ui/core/Button';
import { TextValidator, ValidatorForm } from 'react-material-ui-form-validator';
import PasswordValidator from '../components/PasswordValidator';
import Input from '@material-ui/core/Input';
import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import FormControl from '@material-ui/core/FormControl';
import Select from '@material-ui/core/Select';
import Chip from '@material-ui/core/Chip';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Switch from '@material-ui/core/Switch';
import FormGroup from '@material-ui/core/FormGroup';
const styles = theme => ({
textField: {
@ -41,7 +38,7 @@ class UserForm extends React.Component {
}
render() {
const { classes, user, roles, creating, handleValueChange, onDoneEditing, onCancelEditing } = this.props;
const { classes, user, creating, handleValueChange, handleCheckboxChange, onDoneEditing, onCancelEditing } = this.props;
return (
<ValidatorForm onSubmit={onDoneEditing}>
<TextValidator
@ -62,31 +59,15 @@ class UserForm extends React.Component {
label="Password"
className={classes.textField}
value={user.password}
onChange={handleValueChange('password')}
onChange={handleCheckboxChange('password')}
margin="normal"
/>
<FormControl className={classes.textField}>
<InputLabel htmlFor="roles">Roles</InputLabel>
<Select
multiple
value={user.roles}
onChange={handleValueChange('roles')}
input={<Input id="roles" />}
renderValue={selected => (
<div className={classes.chips}>
{selected.map(value => (
<Chip key={value} label={value} className={classes.chip} />
))}
</div>
)}
>
{roles.map(name => (
<MenuItem key={name} value={name}>
{name}
</MenuItem>
))}
</Select>
</FormControl>
<FormGroup>
<FormControlLabel
control={<Switch checked={user.admin} onChange={handleCheckboxChange('admin')} id="admin" />}
label="Admin?"
/>
</FormGroup>
<Button variant="contained" color="primary" className={classes.button} type="submit">
Save
</Button>
@ -102,11 +83,11 @@ UserForm.propTypes = {
classes: PropTypes.object.isRequired,
user: PropTypes.object.isRequired,
creating: PropTypes.bool.isRequired,
roles: PropTypes.array.isRequired,
onDoneEditing: PropTypes.func.isRequired,
onCancelEditing: PropTypes.func.isRequired,
uniqueUsername: PropTypes.func.isRequired,
handleValueChange: PropTypes.func.isRequired
handleValueChange: PropTypes.func.isRequired,
handleCheckboxChange: PropTypes.func.isRequired
};
export default withStyles(styles)(UserForm);

View File

@ -7,26 +7,11 @@ void SecurityManager::readFromJsonObject(JsonObject& root) {
// secret
_jwtSecret = root["jwt_secret"] | DEFAULT_JWT_SECRET;
// roles
_roles.clear();
if (root["roles"].is<JsonArray>()) {
JsonArray roles = root["roles"];
for (JsonVariant role : roles) {
_roles.push_back(role.as<String>());
}
}
// users
_users.clear();
if (root["users"].is<JsonArray>()) {
for (JsonVariant user : root["users"].as<JsonArray>()) {
std::list<String> roles;
if (user["roles"].is<JsonArray>()) {
for (JsonVariant role : user["roles"].as<JsonArray>()) {
roles.push_back(role.as<String>());
}
}
_users.push_back(User(user["username"], user["password"], roles));
_users.push_back(User(user["username"], user["password"], user["admin"]));
}
}
}
@ -36,22 +21,13 @@ void SecurityManager::writeToJsonObject(JsonObject& root) {
// secret
root["jwt_secret"] = _jwtSecret;
// roles
JsonArray roles = root.createNestedArray("roles");
for (String _role : _roles) {
roles.add(_role);
}
// users
JsonArray users = root.createNestedArray("users");
for (User _user : _users) {
JsonObject user = users.createNestedObject();
user["username"] = _user.getUsername();
user["password"] = _user.getPassword();
JsonArray roles = user.createNestedArray("roles");
for (String _role : _user.getRoles()){
roles.add(_role);
}
user["admin"] = _user.isAdmin();
}
}
@ -100,10 +76,7 @@ Authentication SecurityManager::authenticate(String username, String password) {
inline void populateJWTPayload(JsonObject &payload, User *user) {
payload["username"] = user->getUsername();
JsonArray roles = payload.createNestedArray("roles");
for (String _role : user->getRoles()){
roles.add(_role);
}
payload["admin"] = user -> isAdmin();
}
boolean SecurityManager::validatePayload(JsonObject &parsedPayload, User *user) {

View File

@ -28,17 +28,17 @@ class User {
private:
String _username;
String _password;
std::list<String> _roles;
bool _admin;
public:
User(String username, String password, std::list<String> roles): _username(username), _password(password), _roles(roles) {}
User(String username, String password, bool admin): _username(username), _password(password), _admin(admin) {}
String getUsername() {
return _username;
}
String getPassword() {
return _password;
}
std::list<String> getRoles() {
return _roles;
bool isAdmin() {
return _admin;
}
};
@ -97,7 +97,6 @@ class SecurityManager : public SettingsService {
// access point settings
String _jwtSecret;
std::list<String> _roles;
std::list<User> _users;
// endpoint functions