var User = require("./user"); var DataSavable = require("./SavableData"); var E = require("./Errors"); var utils = require('./utils'); var global = require("./global"); var fs = require("fs"); module.exports = class UserManager extends DataSavable{ constructor() { super(); utils.mkdir(global.userPath); this.index=0; this.users={}; // id: name this.path = "data/users.json"; this.load(); } createUser(name, password) { console.log("createUser('"+name+"', '"+password+"')") if(this.findIdByName(name)>=0) return -1; var u = new User( name, password); this.users[this.index]=name; this.index++; utils.mkdir(global.pathToUserDir(name)); u.save(); this.save(); return u; } removeUser(name) { var user = this.getUser(name); if(!E.check(user)) return user; var id = this.findIdByName(name); delete this.users[id]; console.log("-- "+global.pathToUserDir(name)+" --") utils.rmdir(global.pathToUserDir(name)); this.save(); return E.SUCCESS; } renameUser(oldName, newName) { var oldPath = global.pathToUserDir(oldName); var newPath = global.pathToUserDir(newName); console.log(oldPath+" -> "+newPath); try { utils.mv(oldPath, newPath); } catch (e) { return E.UNKNWON; } finally { } var id = this.findIdByName(oldName); this.users[id]=newName; this.save(); var user = this.getUser(newName); if(!E.check(user)) return user; user.name=newName; user.save(); return E.SUCCESS; } editUser(name, data) { console.log("\n\n'"+data.name+"' "+"'"+name+"'") if(data.name!=undefined && data.name!=name) { var t = this.renameUser(name, data.name); if( !E.check(t) ) return t; this.save(); name=data.name; } var user = this.getUser(name); if(!E.check(user)) return user; console.log("\n\ndata: "+JSON.stringify(data)); console.log("avant: "+JSON.stringify(user)); Object.assign(user, data); console.log("apres: "+JSON.stringify(user)); user.save(); return E.SUCCESS; } findIdByName(name) { var keys = Object.keys(this.users); for(var i=0; i