diff --git a/backend/middlewares/user/AuthenticationMWs.ts b/backend/middlewares/user/AuthenticationMWs.ts index 52efd349..99758271 100644 --- a/backend/middlewares/user/AuthenticationMWs.ts +++ b/backend/middlewares/user/AuthenticationMWs.ts @@ -9,6 +9,7 @@ import {ObjectManagerRepository} from "../../model/ObjectManagerRepository"; export class AuthenticationMWs { public static authenticate(req:Request, res:Response, next:NextFunction) { + if (typeof req.session.user === 'undefined') { return next(new Error(ErrorCodes.NOT_AUTHENTICATED)); } diff --git a/backend/model/memory/UserManager.ts b/backend/model/memory/UserManager.ts index 8698a14a..20f4f175 100644 --- a/backend/model/memory/UserManager.ts +++ b/backend/model/memory/UserManager.ts @@ -1,38 +1,93 @@ import {User, UserRoles} from "../../../common/entities/User"; import {IUserManager} from "../IUserManager"; -export class UserManager implements IUserManager { +import {ProjectPath} from "../../ProjectPath"; +import {Utils} from "../../../common/Utils"; +import * as flatfile from "flat-file-db"; +import * as path from "path"; + + +export class UserManager implements IUserManager { + private db:any = null; + + generateId():string { + function s4() { + return Math.floor((1 + Math.random()) * 0x10000) + .toString(16) + .substring(1); + } + + return s4() + s4() + s4() + s4(); + } + + constructor() { + console.log("ctor"); + this.db = flatfile.sync(path.join(ProjectPath.Root, 'users.db')); + + if (!this.db.has("idCounter")) { + console.log("creating counter"); + this.db.put("idCounter", 1); + } + + if (!this.db.has("users")) { + this.db.put("users", []); + this.createUser(new User("developer", "developer", UserRoles.Developer)); + this.createUser(new User("admin", "admin", UserRoles.Admin)); + this.createUser(new User("user", "user", UserRoles.User)); + this.createUser(new User("guest", "guest", UserRoles.Guest)); + } + + + } - private users = [new User(1, "developer", "developer", UserRoles.Developer), - new User(2, "admin", "admin", UserRoles.Admin), - new User(3, "user", "user", UserRoles.User), - new User(4, "guest", "guest", UserRoles.Guest)]; public findOne(filter, cb:(error:any, result:User) => void) { - return cb(null, this.users[1]); + this.find(filter, (error, result:Array)=> { + if (error) { + return cb(error, null); + } + if (result.length == 0) { + return cb("User not found", null); + } + return cb(null, result[0]); + + }); } public find(filter, cb:(error:any, result:Array) => void) { - return cb(null, this.users); + + let users = this.db.get("users").filter((u) => Utils.equalsFilter(u, filter)); + + return cb(null, users); } - public createUser(user, cb:(error:any, result:User) => void) { + public createUser(user:User, cb:(error:any, result:User) => void = (e, r) => { + }) { + user.id = parseInt(this.db.get("idCounter")) + 1; + this.db.put("idCounter", user.id); + let users = this.db.get("users"); + users.push(user); - this.users.push(user); + this.db.put("users", users); return cb(null, user); } public deleteUser(id:number, cb:(error:any) => void) { - this.users = this.users.filter(u => u.id != id); + let users = this.db.get("users").filter((u) => u.id != id); + this.db.put("users", users); return cb(null); } public changeRole(id:number, newRole:UserRoles, cb:(error:any, result:string) => void) { - for (let i = 0; i < this.users.length; i++) { - if (this.users[i].id === id) { - this.users[i].role = newRole; - return cb(null, "ok"); + + let users:Array = this.db.get("users"); + + for (let i = 0; i < users.length; i++) { + if (users[i].id == id) { + users[i].role = newRole; + break; } } + this.db.put("users", users); } public changePassword(request:any, cb:(error:any, result:string) => void) { diff --git a/backend/model/memory/flat-file-db.ts b/backend/model/memory/flat-file-db.ts new file mode 100644 index 00000000..cb5b6c91 --- /dev/null +++ b/backend/model/memory/flat-file-db.ts @@ -0,0 +1,13 @@ +declare module "flat-file-db" { + export function sync(path:string):DB; +} + +declare interface DB { + sync(); + put(); + get(); + del(); + has(); + keys(); + close(); +} \ No newline at end of file diff --git a/backend/server.ts b/backend/server.ts index d90999cf..7cc1aeb5 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -27,7 +27,6 @@ export class Server { this.debug = _debug("PiGallery2:server"); this.app = _express(); - this.app.set('view engine', 'ejs'); if (process.env.DEBUG) { diff --git a/common/Utils.ts b/common/Utils.ts index 16161fed..649b61ac 100644 --- a/common/Utils.ts +++ b/common/Utils.ts @@ -1,9 +1,23 @@ export class Utils { + static clone(object:T):T { return JSON.parse(JSON.stringify(object)); } + static equalsFilter(object:any, filter:any):boolean { + + let keys = Object.keys(filter); + for (let i = 0; i < keys.length; i++) { + let key = keys[i]; + if (object[key] !== filter[key]) { + return false; + } + } + + return true; + } + static concatUrls(...args:Array) { let url = ""; diff --git a/common/config/Config.ts b/common/config/Config.ts index 0e01852e..a239efe3 100644 --- a/common/config/Config.ts +++ b/common/config/Config.ts @@ -22,6 +22,7 @@ interface ClientConfig { enableCache:boolean; enableOnScrollRendering:boolean; enableOnScrollThumbnailPrioritising:boolean; + authenticationRequired:boolean; } export class ConfigClass { @@ -37,7 +38,8 @@ export class ConfigClass { concurrentThumbnailGenerations: 1, enableCache: false, enableOnScrollRendering: true, - enableOnScrollThumbnailPrioritising: true + enableOnScrollThumbnailPrioritising: true, + authenticationRequired: true }; public setDatabaseType(type:DatabaseType) { diff --git a/common/entities/User.ts b/common/entities/User.ts index 32d0e5af..8291ad40 100644 --- a/common/entities/User.ts +++ b/common/entities/User.ts @@ -7,6 +7,8 @@ export enum UserRoles{ } export class User { - constructor(public id?:number, public name?:string, public password?:string, public role:UserRoles = UserRoles.User) { + public id:number; + + constructor(public name?:string, public password?:string, public role:UserRoles = UserRoles.User) { } } \ No newline at end of file diff --git a/frontend/app/frame/frame.component.html b/frontend/app/frame/frame.component.html index 9a0902e4..aa3fd21f 100644 --- a/frontend/app/frame/frame.component.html +++ b/frontend/app/frame/frame.component.html @@ -15,7 +15,7 @@
  • Gallery
  • Admin
  • -