1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/model/memory/UserManager.ts

110 lines
3.0 KiB
TypeScript
Raw Normal View History

2016-07-07 19:56:39 +08:00
///<reference path="flat-file-db.ts"/>
2016-12-27 23:09:47 +08:00
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";
2017-07-10 04:36:25 +08:00
import {PasswordHelper} from "../PasswordHelper";
2016-05-09 23:04:56 +08:00
export class UserManager implements IUserManager {
2017-07-04 01:17:49 +08:00
private db: any = null;
2017-07-04 01:17:49 +08:00
generateId(): string {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
2017-07-04 01:17:49 +08:00
return s4() + s4() + s4() + s4();
}
2017-07-04 01:17:49 +08:00
constructor() {
this.db = flatfile.sync(path.join(ProjectPath.Root, 'users.db'));
2017-07-04 01:17:49 +08:00
if (!this.db.has("idCounter")) {
console.log("creating counter");
this.db.put("idCounter", 1);
}
2016-04-22 19:23:44 +08:00
2017-07-04 01:17:49 +08:00
if (!this.db.has("users")) {
this.db.put("users", []);
//TODO: remove defaults
this.createUser(<UserDTO>{name: "developer", password: "developer", role: UserRoles.Developer});
this.createUser(<UserDTO>{name: "admin", password: "admin", role: UserRoles.Admin});
this.createUser(<UserDTO>{name: "user", password: "user", role: UserRoles.User});
2017-07-09 18:03:17 +08:00
this.createUser(<UserDTO>{name: "guest", password: "guest", role: UserRoles.LimitedGuest});
2016-04-22 19:23:44 +08:00
}
2017-07-04 01:17:49 +08:00
}
2016-04-22 19:23:44 +08:00
2017-07-04 01:17:49 +08:00
public async findOne(filter: any) {
const result = await this.find(filter);
2016-04-22 19:23:44 +08:00
2017-07-04 01:17:49 +08:00
if (result.length == 0) {
throw "UserDTO not found";
2016-04-22 19:23:44 +08:00
}
2017-07-04 01:17:49 +08:00
return result[0];
}
public async find(filter: any) {
2017-07-10 04:36:25 +08:00
let pass = filter.password;
delete filter.password;
2017-07-11 00:08:39 +08:00
const users = await this.db.get("users");
let i = users.length;
while (i--) {
if (pass && !(await PasswordHelper.comparePassword(pass, users[i].password))) {
users.splice(i, 1);
continue;
2017-07-10 04:36:25 +08:00
}
2017-07-11 00:08:39 +08:00
if (Utils.equalsFilter(users[i], filter) == false) {
users.splice(i, 1);
}
}
return users;
2017-07-04 01:17:49 +08:00
}
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");
2017-07-10 04:36:25 +08:00
user.password = await PasswordHelper.cryptPassword(user.password);
2017-07-04 01:17:49 +08:00
users.push(user);
this.db.put("users", users);
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);
if (deleted.length > 0) {
return deleted[0];
2016-04-22 19:23:44 +08:00
}
2017-07-04 01:17:49 +08:00
return null;
}
2016-05-09 23:04:56 +08:00
2017-07-04 01:17:49 +08:00
public async changeRole(id: number, newRole: UserRoles): Promise<UserDTO> {
2017-07-04 01:17:49 +08:00
let users: Array<UserDTO> = this.db.get("users");
2017-07-04 01:17:49 +08:00
for (let i = 0; i < users.length; i++) {
if (users[i].id == id) {
users[i].role = newRole;
this.db.put("users", users);
2017-07-04 01:17:49 +08:00
return users[i];
}
2016-04-22 19:23:44 +08:00
}
2017-07-04 01:17:49 +08:00
}
2016-05-09 23:04:56 +08:00
2017-07-04 01:17:49 +08:00
public async changePassword(request: any) {
throw new Error("not implemented"); //TODO: implement
}
2016-04-22 19:23:44 +08:00
2017-07-04 01:17:49 +08:00
}