major rework of backend loading and usage:
- split config from user data to config.json, add config.hson.template - moved default user data localdb to root folder - moved pve, localdb, ldap backend handlers to backends sub folder - add dynamic loading of all backends - add dynamic mapping for auth backends to support multiple auth sources - update affected endpoints
This commit is contained in:
71
src/backends/localdb.js
Normal file
71
src/backends/localdb.js
Normal file
@@ -0,0 +1,71 @@
|
||||
import { readFileSync, writeFileSync } from "fs";
|
||||
import { exit } from "process";
|
||||
|
||||
export default class LocalDB {
|
||||
#path = null;
|
||||
#data = null;
|
||||
#defaultuser = null;
|
||||
constructor (config) {
|
||||
const path = config.dbfile;
|
||||
try {
|
||||
this.#path = path;
|
||||
this.#load();
|
||||
this.#defaultuser = global.config.defaultuser;
|
||||
}
|
||||
catch {
|
||||
console.log(`Error: ${path} was not found. Please follow the directions in the README to initialize localdb.json.`);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load db from local file system. Reads from file path store in path.
|
||||
*/
|
||||
#load () {
|
||||
this.#data = JSON.parse(readFileSync(this.#path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Save db to local file system. Saves to file path stored in path.
|
||||
*/
|
||||
#save () {
|
||||
writeFileSync(this.#path, JSON.stringify(this.#data));
|
||||
}
|
||||
|
||||
addUser (username, config = null) {
|
||||
config = config || this.#defaultuser;
|
||||
this.#data.users[username] = config;
|
||||
this.#save();
|
||||
}
|
||||
|
||||
getUser (username) {
|
||||
if (this.#data.users[username]) {
|
||||
return this.#data.users[username];
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
setUser (username, config) {
|
||||
if (this.#data.users[username]) {
|
||||
this.#data.users[username] = config;
|
||||
this.#save();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
delUser (username) {
|
||||
if (this.#data.users[username]) {
|
||||
delete this.#data.users[username];
|
||||
this.#save();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user