var DataSavable = require("./SavableData"); var Project = require("./Project"); var E = require("./Errors"); var utils = require('./utils'); var global = require("./global"); var fs = require("fs"); module.exports = class User extends DataSavable{ constructor(name, password) { super(); this.name=""; this.password=""; this.groups={ user: true }; this.projects={}; // { name= path} if(name!=undefined) this.name=name; if(password!=undefined) this.password=password; } getProject(projectName) { var project = new Project(); return project.load(this.name, projectName); } save() { fs.writeFileSync(global.pathToUserFile(this.name), this.toString()); } load(name) { if( name!=undefined ) this.path=global.pathToUserFile(name); var obj = JSON.parse(fs.readFileSync(this.path)); if(obj!=null) Object.assign(this, obj); return this; } createProject(project) { var old = this.getProject(project); if(E.check(old)) return E.PROJECT_EXISTS; utils.mkdir(global.pathToProjectDir(this.name, project)); utils.mkdir(global.pathToResourceDir(this.name, project)); var p = new Project(); p.name=project; p.save(global.pathToProjectFile(this.name, project)); this.projects[project] = true; this.save(); return E.SUCCESS; } renameProject(oldName, newName) { var n = this.getProject(oldName); n.name=newName; var oldPath = global.pathToProjectDir(this.name, oldName); var newPath = global.pathToProjectDir(this.name, newName); try { utils.mv(oldPath, newPath); } catch (e) { return E.UNKNWON; } finally { } delete this.projects[oldName]; this.projects[newName] = true; n.save(global.pathToProjectFile(this.name, newName)); this.save(); return E.SUCCESS; } removeProject(project) { var x = this.getProject(project); if(!E.check(x)) return E.BAD_PROJECT; delete this.projects[project]; utils.rmdir(global.pathToProjectDir(this.name, project)); this.save(global.pathToUserFile(this.name)); } editProject(name, data) { if(data.name!=undefined && data.name!=name) { var t = this.renameProject(name, data.name); if( !E.check(t) ) return t; this.save(); name=data.name; } var pro = this.getProject(name); if(!E.check(pro)) return pro; Object.assign(pro, data); pro.save(global.pathToProjectFile(this.name, name)); return E.SUCCESS; } }