ADMIN UI DONE

This commit is contained in:
andrei-mihnea-cerbu
2024-04-03 05:57:01 +03:00
parent 5e4a488bfd
commit 5e5bab933b
15 changed files with 229 additions and 13 deletions
+7
View File
@@ -14,6 +14,7 @@
"cheerio": "^1.0.0-rc.12",
"cors": "^2.8.5",
"cron": "^3.1.6",
"crypto": "^1.0.1",
"crypto-js": "^4.2.0",
"express": "^4.19.1",
"joi": "^17.12.2",
@@ -492,6 +493,12 @@
"luxon": "~3.4.0"
}
},
"node_modules/crypto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz",
"integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==",
"deprecated": "This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in."
},
"node_modules/crypto-js": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.2.0.tgz",
+1
View File
@@ -16,6 +16,7 @@
"cheerio": "^1.0.0-rc.12",
"cors": "^2.8.5",
"cron": "^3.1.6",
"crypto": "^1.0.1",
"crypto-js": "^4.2.0",
"express": "^4.19.1",
"joi": "^17.12.2",
+17 -1
View File
@@ -5,9 +5,25 @@ const cors = require('cors');
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(cors({
allowedHeaders: ['Authorization', 'Content-Type'] // Add 'Authorization' to the list of allowed headers
}));
app.use(json());
const {
usersDB,
departmentsDB,
backupSchemesDB,
ceoDB,
adminDB
} = require('./db/jsonDatabaseManager');
app.set('usersDB', usersDB);
app.set('departmentsDB', departmentsDB);
app.set('backupSchemesDB', backupSchemesDB);
app.set('ceoDB', ceoDB);
app.set('adminDB', adminDB);
const checkJson = require('./middlewares/checkJson');
const apiKeyValidation = require('./middlewares/apiKeyValidation');
+1
View File
@@ -0,0 +1 @@
{}
+5
View File
@@ -0,0 +1,5 @@
{
"name": "CEO",
"email": "ceo@yourfirm.com",
"password": "335ae110091d3bc5fbc90d5e3786890e"
}
+1
View File
@@ -0,0 +1 @@
[]
+49
View File
@@ -0,0 +1,49 @@
const fs = require('fs');
const path = require('path');
class JSONDatabase {
constructor(filePath) {
this.filePath = filePath;
this.readFile = this.readFile.bind(this); // Bind readFile method to the instance
this.writeFile = this.writeFile.bind(this); // Bind writeFile method to the instance
}
readFile() {
if (!fs.existsSync(this.filePath)) {
console.log('File does not exist, returning null:', this.filePath);
return null;
}
try {
const data = fs.readFileSync(this.filePath, 'utf8');
return JSON.parse(data);
} catch (error) {
console.error('Error reading file:', error);
return null;
}
}
writeFile(data) {
try {
fs.writeFileSync(this.filePath, JSON.stringify(data, null, 2), { flag: 'w' });
console.log('File written successfully.');
} catch (error) {
console.error('Error writing file:', error);
}
}
}
const usersDB = new JSONDatabase(path.join(__dirname, 'users.json'));
const departmentsDB = new JSONDatabase(path.join(__dirname, 'departments.json'));
const backupSchemesDB = new JSONDatabase(path.join(__dirname, 'backup_schemes.json'));
const ceoDB = new JSONDatabase(path.join(__dirname, 'ceo.json'));
const adminDB = new JSONDatabase(path.join(__dirname + '..', '..', '..', '..', 'frontend', 'config', 'credentials.json'));
module.exports = {
usersDB,
departmentsDB,
backupSchemesDB,
ceoDB,
adminDB,
JSONDatabase
};
+1
View File
@@ -0,0 +1 @@
[]
+31 -4
View File
@@ -1,20 +1,47 @@
const express = require('express');
const {httpStatus} = require("../helpers/httpResponses");
const router = express.Router();
const crypto = require('crypto');
function validateBody(req, res, next) {
const apiKey = req.headers['authorization'];
const adminDB = req.app.get('adminDB');
const {id} = adminDB.readFile();
if(apiKey !== id){
return res.status(httpStatus.UNAUTHORIZED).json(
{message: "You are not authorized as an admin."})
}
next();
}
router.post('/reset_server', validateBody, (req, res) => {
// Handle reset server logic here
router.get('/reset_server', validateBody, (req, res) => {
const usersDB = req.app.get('usersDB');
const departmentsDB = req.app.get('departmentsDB');
const backupSchemesDB = req.app.get('backupSchemesDB');
const ceoDB = req.app.get('ceoDB');
usersDB.writeFile([]);
departmentsDB.writeFile([]);
backupSchemesDB.writeFile({});
ceoDB.writeFile({
name: "CEO",
email: "ceo@yourfirm.com",
password: crypto.randomBytes(16).toString('hex')
});
res.status(httpStatus.OK).json({ message: "Resetting the server..." });
});
router.get('/ceo', validateBody, (req, res) => {
// Retrieve CEO info
const ceoDB = req.app.get('ceoDB');
res.status(httpStatus.OK).json({ message: "Retrieving CEO info...", data: ceoDB.readFile() }) // Corrected to readFile
});
router.get('/users', validateBody, (req, res) => {
// Retrieve all users in the system
const usersDB = req.app.get('usersDB');
res.status(httpStatus.OK).json({ message: "Retrieving users info...", data: usersDB.readFile() }) // Corrected to readFile
});
module.exports = router;