mirror of
https://github.com/Pax1601/DCSOlympus.git
synced 2025-10-29 16:56:34 +00:00
feat: added admin password and admin modal
This commit is contained in:
@@ -51,6 +51,7 @@ module.exports = function (configLocation, viteProxy) {
|
||||
/* Config specific routers */
|
||||
const elevationRouter = require("./routes/api/elevation")(configLocation);
|
||||
const resourcesRouter = require("./routes/resources")(configLocation);
|
||||
const adminRouter = require("./routes/admin")(configLocation);
|
||||
|
||||
/* Default routers */
|
||||
const airbasesRouter = require("./routes/api/airbases");
|
||||
@@ -113,6 +114,9 @@ module.exports = function (configLocation, viteProxy) {
|
||||
"Blue commander": config["authentication"]["blueCommanderPassword"],
|
||||
"Red commander": config["authentication"]["redCommanderPassword"],
|
||||
};
|
||||
if (config["authentication"]["adminPassword"]) {
|
||||
defaultUsers["Admin"] = config["authentication"]["adminPassword"];
|
||||
}
|
||||
let users = {};
|
||||
Object.keys(usersConfig).forEach(
|
||||
(user) => (users[user] = usersConfig[user].password)
|
||||
@@ -122,7 +126,9 @@ module.exports = function (configLocation, viteProxy) {
|
||||
});
|
||||
|
||||
/* Define middleware */
|
||||
app.use(logger("dev"));
|
||||
app.use(logger('dev', {
|
||||
skip: function (req, res) { return res.statusCode < 400 }
|
||||
}));
|
||||
|
||||
/* Authorization middleware */
|
||||
if (
|
||||
@@ -238,6 +244,9 @@ module.exports = function (configLocation, viteProxy) {
|
||||
app.use("/api/speech", speechRouter);
|
||||
app.use("/resources", resourcesRouter);
|
||||
|
||||
app.use("/admin", auth);
|
||||
app.use("/admin", adminRouter);
|
||||
|
||||
/* Set default index */
|
||||
if (viteProxy) {
|
||||
app.use(
|
||||
|
||||
100
frontend/server/src/routes/admin.ts
Normal file
100
frontend/server/src/routes/admin.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import express = require("express");
|
||||
import fs = require("fs");
|
||||
import path = require("path");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
module.exports = function (configLocation) {
|
||||
router.get("/config", function (req, res, next) {
|
||||
if (req.auth?.user === "Admin") {
|
||||
/* Read the users configuration file */
|
||||
let usersConfig = {};
|
||||
if (
|
||||
fs.existsSync(
|
||||
path.join(path.dirname(configLocation), "olympusUsers.json")
|
||||
)
|
||||
) {
|
||||
let rawdata = fs.readFileSync(
|
||||
path.join(path.dirname(configLocation), "olympusUsers.json"),
|
||||
{ encoding: "utf-8" }
|
||||
);
|
||||
usersConfig = JSON.parse(rawdata);
|
||||
}
|
||||
|
||||
/* Read the groups configuration file */
|
||||
let groupsConfig = {};
|
||||
if (
|
||||
fs.existsSync(
|
||||
path.join(path.dirname(configLocation), "olympusGroups.json")
|
||||
)
|
||||
) {
|
||||
let rawdata = fs.readFileSync(
|
||||
path.join(path.dirname(configLocation), "olympusGroups.json"),
|
||||
{ encoding: "utf-8" }
|
||||
);
|
||||
groupsConfig = JSON.parse(rawdata);
|
||||
}
|
||||
|
||||
res.send({ users: usersConfig, groups: groupsConfig });
|
||||
res.end();
|
||||
} else {
|
||||
res.sendStatus(401);
|
||||
}
|
||||
});
|
||||
|
||||
router.put("/config", function (req, res, next) {
|
||||
if (req.auth?.user === "Admin") {
|
||||
/* Create a backup folder for the configuration files */
|
||||
let backupFolder = path.join(path.dirname(configLocation), "Olympus Configs Backup");
|
||||
if (!fs.existsSync(backupFolder)) {
|
||||
fs.mkdirSync(backupFolder);
|
||||
}
|
||||
|
||||
/* Make a backup of the existing files */
|
||||
let timestamp = new Date().toISOString().replace(/:/g, "-");
|
||||
fs.copyFileSync(
|
||||
path.join(path.dirname(configLocation), "olympusUsers.json"),
|
||||
path.join(
|
||||
path.dirname(configLocation),
|
||||
"Olympus Configs Backup",
|
||||
"olympusUsers.json." + timestamp
|
||||
)
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(path.dirname(configLocation), "olympusGroups.json"),
|
||||
path.join(
|
||||
path.dirname(configLocation),
|
||||
"Olympus Configs Backup",
|
||||
"olympusGroups.json." + timestamp
|
||||
)
|
||||
);
|
||||
|
||||
/* Save the users configuration file */
|
||||
let usersConfig = req.body.users;
|
||||
|
||||
if (usersConfig) {
|
||||
fs.writeFileSync(
|
||||
path.join(path.dirname(configLocation), "olympusUsers.json"),
|
||||
JSON.stringify(usersConfig, null, 2)
|
||||
);
|
||||
}
|
||||
|
||||
/* Save the groups configuration file */
|
||||
let groupsConfig = req.body.groups;
|
||||
|
||||
if (groupsConfig) {
|
||||
fs.writeFileSync(
|
||||
path.join(path.dirname(configLocation), "olympusGroups.json"),
|
||||
JSON.stringify(groupsConfig, null, 2)
|
||||
);
|
||||
}
|
||||
|
||||
res.send({ users: usersConfig, groups: groupsConfig });
|
||||
res.end();
|
||||
} else {
|
||||
res.sendStatus(401);
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
};
|
||||
Reference in New Issue
Block a user