/*
* This file is part of caronte (https://github.com/eciavatta/caronte).
* Copyright (c) 2020 Emiliano Ciavatta.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
import React, {Component} from "react";
import {Col, Container, Row} from "react-bootstrap";
import Table from "react-bootstrap/Table";
import backend from "../../backend";
import {createCurlCommand} from "../../utils";
import validation from "../../validation";
import ButtonField from "../fields/ButtonField";
import CheckField from "../fields/CheckField";
import InputField from "../fields/InputField";
import TextField from "../fields/TextField";
import Header from "../Header";
import LinkPopover from "../objects/LinkPopover";
import "../panels/common.scss";
import "./common.scss";
import "./ConfigurationPage.scss";
class ConfigurationPage extends Component {
constructor(props) {
super(props);
this.state = {
settings: {
"config": {
"server_address": "",
"flag_regex": "",
"auth_required": false
},
"accounts": {}
},
newUsername: "",
newPassword: ""
};
}
saveSettings = () => {
if (this.validateSettings(this.state.settings)) {
backend.post("/setup", this.state.settings).then((_) => {
this.props.onConfigured();
}).catch((res) => {
this.setState({setupStatusCode: res.status, setupResponse: JSON.stringify(res.json)});
});
}
};
validateSettings = (settings) => {
let valid = true;
if (!validation.isValidAddress(settings.config["server_address"], true)) {
this.setState({serverAddressError: "invalid ip_address"});
valid = false;
}
if (settings.config["flag_regex"].length < 8) {
this.setState({flagRegexError: "flag_regex.length < 8"});
valid = false;
}
return valid;
};
updateParam = (callback) => {
callback(this.state.settings);
this.setState({settings: this.state.settings});
};
addAccount = () => {
if (this.state.newUsername.length !== 0 && this.state.newPassword.length !== 0) {
const settings = this.state.settings;
settings.accounts[this.state.newUsername] = this.state.newPassword;
this.setState({
newUsername: "",
newPassword: "",
settings
});
} else {
this.setState({
newUsernameActive: this.state.newUsername.length === 0,
newPasswordActive: this.state.newPassword.length === 0
});
}
};
render() {
const settings = this.state.settings;
const curlCommand = createCurlCommand("/setup", "POST", settings);
const accounts = Object.entries(settings.accounts).map(([username, password]) =>
{username} |
|
this.updateParam((s) => delete s.accounts[username])}/> |
).concat(
this.setState({newUsername: v})}/> |
this.setState({newPassword: v})}/> |
|
);
return (
POST /setup
this.updateParam((s) => s.config["server_address"] = v)}/>
this.updateParam((s) => s.config["flag_regex"] = v)}
error={this.state.flagRegexError}/>
this.updateParam((s) => s.config["auth_required"] = v)}/>
accounts:
username |
password |
actions |
{accounts}
);
}
}
export default ConfigurationPage;