删除用户

This commit is contained in:
life
2015-11-20 00:07:04 +08:00
parent 3fd0da6810
commit d620c30298
5 changed files with 203 additions and 7 deletions

19
node_modules/common.js generated vendored
View File

@@ -272,6 +272,23 @@ var Common = {
md5sum.update(key); md5sum.update(key);
str = md5sum.digest('hex'); str = md5sum.digest('hex');
return str; return str;
} },
// 删除文件夹
deleteFolderRecursive: function(path) {
var files = [];
if( fs.existsSync(path) ) {
files = fs.readdirSync(path);
files.forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.statSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
},
}; };
module.exports = Common; module.exports = Common;

2
node_modules/db.js generated vendored
View File

@@ -6,7 +6,7 @@ var Evt = require('evt');
// var dbPath = require('nw.gui').App.dataPath + '/nedb'; // var dbPath = require('nw.gui').App.dataPath + '/nedb';
// var dbPath = Evt.getBasePath() + '/Users/life/Library/Application Support/Leanote' + '/nedb'; // var dbPath = Evt.getBasePath() + '/Users/life/Library/Application Support/Leanote' + '/nedb';
// nedb2 为了port // nedb2 为了port
var dbPath = Evt.getBasePath() + '/nedb55'; var dbPath = Evt.getDBPath();
// console.error(dbPath); // console.error(dbPath);
// test // test

4
node_modules/evt.js generated vendored
View File

@@ -107,6 +107,10 @@ var Evt = {
fs.mkdirSync(dataBasePath); fs.mkdirSync(dataBasePath);
} }
catch(e) {}; catch(e) {};
},
getDBPath: function () {
return this.getBasePath() + '/nedb55';
} }
}; };
module.exports = Evt; module.exports = Evt;

40
node_modules/user.js generated vendored
View File

@@ -251,6 +251,46 @@ User = {
return 'data/' + this.getCurActiveUserId() + '/attachs'; return 'data/' + this.getCurActiveUserId() + '/attachs';
}, },
getUserImagesPath: function(userId) {
return Evt.getBasePath() + '/data/' + userId + '/images';
},
getUserAttachsPath: function(userId) {
return Evt.getBasePath() + '/data/' + userId + '/attachs';
},
getUserDBPath: function (userId) {
var base = Evt.getDBPath();
if (!base) {
return false;
}
return base + '/' + userId;
},
// 删除用户的文件目录
deleteUserImagesAndAttachsPath: function (userId) {
var me = this;
// 防止误删
if (!Evt.getBasePath()) {
return;
}
var imagesPath = me.getUserImagesPath(userId);
var attachsPath = me.getUserAttachsPath(userId);
if (imagesPath) {
Common.deleteFolderRecursive(imagesPath);
}
if (attachsPath) {
Common.deleteFolderRecursive(attachsPath);
}
},
// 删除用户
deleteUser: function (userId) {
db.users.remove({_id: userId}, function () {
});
},
setUserDataPath: function(userId) { setUserDataPath: function(userId) {
var me = this; var me = this;
// 判断是否存在, 不存在则创建dir // 判断是否存在, 不存在则创建dir

View File

@@ -103,7 +103,11 @@ define(function() {
}, },
'open-db-dir': '', 'open-db-dir': '',
'open-files-dir': '', 'open-files-dir': '',
'delete': '' 'delete': function (userId, $targetBtn) {
me.deleteUser(userId);
$targetBtn.closest('tr').remove();
}
}; };
// 事件 // 事件
@@ -114,7 +118,7 @@ define(function() {
var func = op2Func[option]; var func = op2Func[option];
if (func) { if (func) {
func(userId); func(userId, $this);
} }
}); });
}, },
@@ -199,9 +203,10 @@ define(function() {
return; return;
} }
// 已经存在 // 已经存在, 合并数据文件
if (user.HasDB) { if (user.HasDB) {
Api.loading.hide(3000); me.compactDatafile();
Api.loading.hide(2000);
Api.loading.setMsg('优化完成'); Api.loading.setMsg('优化完成');
return; return;
} }
@@ -223,6 +228,17 @@ define(function() {
}); });
}, },
// 合并数据
compactDatafile: function () {
var names = ['notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories'];
names.forEach(function (name) {
if (Api.dbService[name]) {
Api.dbService[name].persistence.compactDatafile();
}
});
return true;
},
// 迁移历史记录 // 迁移历史记录
migrateNoteHistories: function (noteId, sDB, dDB, callback) { migrateNoteHistories: function (noteId, sDB, dDB, callback) {
var me = this; var me = this;
@@ -287,10 +303,13 @@ define(function() {
}); });
}, },
dbNames: ['notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories'],
// 迁移到独立目录 // 迁移到独立目录
migrateAllDBs: function (userId, callback, msgCallbac) { migrateAllDBs: function (userId, callback, msgCallbac) {
var me = this; var me = this;
var names = ['notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories']; var names = me.dbNames;
// notes, notebooks, tags, attachs, images, noteHistories // notes, notebooks, tags, attachs, images, noteHistories
// 判断当前db是否是全局的, 如果不是, 则初始化全局的 // 判断当前db是否是全局的, 如果不是, 则初始化全局的
var sourceDb = {}; var sourceDb = {};
@@ -328,8 +347,124 @@ define(function() {
cb(); cb();
}); });
}, function () { }, function () {
// 如果优化的是当前的用户, 则需要将db的表替换成distDb
if (Api.userService.getCurActiveUserId() == userId) {
for (var i = 0; i < names.length; ++i) {
var name = names[i];
Api.dbService[name] = distDb[name];
}
}
callback(); callback();
}); });
},
// 删除笔记历史记录
_deleteNoteHistories: function (sourceDb, notes, callback) {
var me = this;
sourceDb.noteHistories.loadDB(function (ok) {
if (!ok) {
return callback();
}
async.eachSeries(notes, function (note, cb) {
Api.dbService.noteHistories.remove( {_id: note.NoteId}, { multi: true }, function () {
cb();
});
}, function () {
callback();
});
});
},
_deleteDB: function (userId, callback) {
var me = this;
// 判断当前db是否是全局的, 如果不是, 则初始化全局的
var names = me.dbNames;
var sourceDb = {};
if (Api.userService.hasDB) {
Api.dbService.initIt(sourceDb, names, '', false);
}
else {
sourceDb = Api.dbService;
}
var names = ['notebooks', 'notes', 'tags', 'images', 'attachs'];
var db = sourceDb;
var query = {UserId: userId};
async.eachSeries(names, function (name, cb) {
var dbIt = db[name];
if (!dbIt) {
cb();
return;
}
// 如果是笔记, 则要删除note histories
if (name == 'notes') {
dbIt.find(query, function(err, docs) {
if (err || !docs) {
cb();
return;
}
// 删除历史记录
me._deleteNoteHistories(sourceDb, docs, function () {
// 删除自己
dbIt.remove(query, { multi: true },function () {
cb();
});
});
});
}
else {
dbIt.remove(query, { multi: true }, function () {
cb();
});
}
}, function () {
callback();
});
},
_deleteUser: function(userInfo, callback) {
var me = this;
var userId = userInfo.UserId;
// 1. 删除附件,图片
Api.userService.deleteUserImagesAndAttachsPath(userId);
// 2. 删除之
Api.userService.deleteUser(userId);
// 3. 删除其它表
// 如果有自己独立的表, 则把文件夹删除即可
if (userInfo.HasDB) {
var dbPath = Api.userService.getUserDBPath(userId);
if (dbPath) {
Api.commonService.deleteFolderRecursive(dbPath);
}
callback();
}
// 没有, 那就要一个个删除了
else {
me._deleteDB(userId, function () {
callback();
});
}
},
// 删除用户
deleteUser: function(userId) {
var me = this;
if (confirm(me.getMsg("Are you sure, it can't be recovered after it has been deleted"))) {
Api.loading.show();
Api.userService.getUser(userId, function (user) {
me._deleteUser(user, function() {
Api.loading.setMsg(me.getMsg('Deleted'));
Api.loading.hide(2000);
});
});
}
} }
}; };