diff --git a/README.md b/README.md index f5b9fcba..dc9f87d0 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ npm install npm start ``` To configure it. Run `PiGallery2` first to create `config.json` file, then edit it and restart. - +Default user: `admin` pass: `admin` ## Feature list diff --git a/backend/model/memory/UserManager.ts b/backend/model/memory/UserManager.ts index 07c5d234..01dc0b89 100644 --- a/backend/model/memory/UserManager.ts +++ b/backend/model/memory/UserManager.ts @@ -1,15 +1,15 @@ -/// import {UserDTO, UserRoles} from "../../../common/entities/UserDTO"; import {IUserManager} from "../interfaces/IUserManager"; import {ProjectPath} from "../../ProjectPath"; import {Utils} from "../../../common/Utils"; -import * as flatfile from "flat-file-db"; import * as path from "path"; +import * as fs from "fs"; import {PasswordHelper} from "../PasswordHelper"; export class UserManager implements IUserManager { - private db: any = null; + private db: { users?: UserDTO[], idCounter?: number } = {}; + private dbPath; generateId(): string { function s4() { @@ -22,22 +22,22 @@ export class UserManager implements IUserManager { } constructor() { - this.db = flatfile.sync(path.join(ProjectPath.Root, 'users.db')); + this.dbPath = path.join(ProjectPath.Root, 'users.db'); + if (fs.existsSync(this.dbPath)) { + this.loadDB(); + } - if (!this.db.has("idCounter")) { + if (!this.db.idCounter) { console.log("creating counter"); - this.db.put("idCounter", 1); + this.db.idCounter = 1; } - if (!this.db.has("users")) { - this.db.put("users", []); + if (!this.db.users) { + this.db.users = []; //TODO: remove defaults - this.createUser({name: "developer", password: "developer", role: UserRoles.Developer}); this.createUser({name: "admin", password: "admin", role: UserRoles.Admin}); - this.createUser({name: "user", password: "user", role: UserRoles.User}); - this.createUser({name: "guest", password: "guest", role: UserRoles.Guest}); } - + this.saveDB(); } @@ -54,7 +54,7 @@ export class UserManager implements IUserManager { public async find(filter: any) { let pass = filter.password; delete filter.password; - const users = (await this.db.get("users")).slice(); + const users = this.db.users.slice(); let i = users.length; while (i--) { if (pass && !(PasswordHelper.comparePassword(pass, users[i].password))) { @@ -69,20 +69,17 @@ export class UserManager implements IUserManager { } public async createUser(user: UserDTO) { - user.id = parseInt(this.db.get("idCounter")) + 1; - this.db.put("idCounter", user.id); - let users = this.db.get("users"); + user.id = this.db.idCounter++; user.password = PasswordHelper.cryptPassword(user.password); - users.push(user); - this.db.put("users", users); - + this.db.users.push(user); + this.saveDB(); return user; } - public deleteUser(id: number) { - let deleted = this.db.get("users").filter((u: UserDTO) => u.id == id); - let users = this.db.get("users").filter((u: UserDTO) => u.id != id); - this.db.put("users", users); + public async deleteUser(id: number) { + let deleted = this.db.users.filter((u: UserDTO) => u.id == id); + this.db.users = this.db.users.filter((u: UserDTO) => u.id != id); + this.saveDB(); if (deleted.length > 0) { return deleted[0]; } @@ -90,14 +87,11 @@ export class UserManager implements IUserManager { } public async changeRole(id: number, newRole: UserRoles): Promise { - - let users: Array = this.db.get("users"); - - for (let i = 0; i < users.length; i++) { - if (users[i].id == id) { - users[i].role = newRole; - this.db.put("users", users); - return users[i]; + for (let i = 0; i < this.db.users.length; i++) { + if (this.db.users[i].id == id) { + this.db.users[i].role = newRole; + this.saveDB(); + return this.db.users[i]; } } } @@ -106,4 +100,13 @@ export class UserManager implements IUserManager { throw new Error("not implemented"); //TODO: implement } + private loadDB() { + const data = fs.readFileSync(this.dbPath, 'utf8'); + this.db = JSON.parse(data); + } + + private saveDB() { + fs.writeFileSync(this.dbPath, JSON.stringify(this.db)); + } + } diff --git a/backend/model/memory/flat-file-db.ts b/backend/model/memory/flat-file-db.ts deleted file mode 100644 index 5c71023e..00000000 --- a/backend/model/memory/flat-file-db.ts +++ /dev/null @@ -1,13 +0,0 @@ -declare module "flat-file-db" { - export function sync(path: string): DB; -} - -declare interface DB { - sync(): any; - put(): any; - get(): any; - del(): any; - has(): any; - keys(): any; - close(): any; -} diff --git a/package.json b/package.json index c1b93e09..609b1370 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "cookie-session": "^2.0.0-beta.2", "ejs": "2.5.6", "express": "4.15.3", - "flat-file-db": "1.0.0", "jimp": "0.2.28", "reflect-metadata": "0.1.10", "sqlite3": "^3.1.8",