2016-07-07 19:56:39 +08:00
|
|
|
///<reference path="flat-file-db.ts"/>
|
|
|
|
|
|
|
|
|
2016-05-03 20:29:24 +08:00
|
|
|
import {User, UserRoles} from "../../../common/entities/User";
|
2016-04-22 19:23:44 +08:00
|
|
|
import {IUserManager} from "../IUserManager";
|
2016-07-07 18:19:08 +08:00
|
|
|
import {ProjectPath} from "../../ProjectPath";
|
|
|
|
import {Utils} from "../../../common/Utils";
|
|
|
|
import * as flatfile from "flat-file-db";
|
|
|
|
import * as path from "path";
|
|
|
|
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
export class UserManager implements IUserManager {
|
2016-07-07 18:19:08 +08:00
|
|
|
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() {
|
|
|
|
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", []);
|
2016-07-08 04:16:04 +08:00
|
|
|
//TODO: remove defaults
|
2016-07-07 18:19:08 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2016-04-22 19:23:44 +08:00
|
|
|
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public findOne(filter, cb:(error:any, result:User) => void) {
|
2016-07-07 18:19:08 +08:00
|
|
|
this.find(filter, (error, result:Array<User>)=> {
|
|
|
|
if (error) {
|
|
|
|
return cb(error, null);
|
|
|
|
}
|
|
|
|
if (result.length == 0) {
|
|
|
|
return cb("User not found", null);
|
|
|
|
}
|
|
|
|
return cb(null, result[0]);
|
|
|
|
|
|
|
|
});
|
2016-04-22 19:23:44 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public find(filter, cb:(error:any, result:Array<User>) => void) {
|
2016-07-07 18:19:08 +08:00
|
|
|
|
|
|
|
let users = this.db.get("users").filter((u) => Utils.equalsFilter(u, filter));
|
|
|
|
|
|
|
|
return cb(null, users);
|
2016-04-22 19:23:44 +08:00
|
|
|
}
|
|
|
|
|
2016-07-07 18:19:08 +08:00
|
|
|
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);
|
2016-04-22 19:23:44 +08:00
|
|
|
|
2016-07-07 18:19:08 +08:00
|
|
|
this.db.put("users", users);
|
2016-04-22 19:23:44 +08:00
|
|
|
return cb(null, user);
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public deleteUser(id:number, cb:(error:any) => void) {
|
2016-07-07 18:19:08 +08:00
|
|
|
let users = this.db.get("users").filter((u) => u.id != id);
|
|
|
|
this.db.put("users", users);
|
2016-04-22 19:23:44 +08:00
|
|
|
return cb(null);
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
public changeRole(id:number, newRole:UserRoles, cb:(error:any, result:string) => void) {
|
2016-07-07 18:19:08 +08:00
|
|
|
|
|
|
|
let users:Array<User> = this.db.get("users");
|
|
|
|
|
|
|
|
for (let i = 0; i < users.length; i++) {
|
|
|
|
if (users[i].id == id) {
|
|
|
|
users[i].role = newRole;
|
|
|
|
break;
|
2016-05-03 20:29:24 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-07 18:19:08 +08:00
|
|
|
this.db.put("users", users);
|
2016-04-22 19:23:44 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
public changePassword(request:any, cb:(error:any, result:string) => void) {
|
2016-04-22 19:23:44 +08:00
|
|
|
throw new Error("not implemented"); //TODO: implement
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|