!UNSTABLE 优化数据库结构,为domain功能准备

This commit is contained in:
masnn committed 2020-05-29 13:05:52 +08:00
1 parent 7d27e56018
commit fff2172c38
40 files changed
+1411 -1012

No files matched your search

+59 -60
View File
@@ -1,3 +1,4 @@
const document = require('./document');
const system = require('./system');
const { UserNotFoundError, UserAlreadyExistError } = require('../error');
const perm = require('../permission');
@@ -5,7 +6,6 @@ const pwhash = require('../lib/hash.hydro');
const db = require('../service/db');
const coll = db.collection('user');
const collRole = db.collection('role');
class USER {
constructor(user) {
@@ -37,38 +37,46 @@ class USER {
}
}
async function getById(_id) {
async function getPerm(domainId, udoc) {
if (udoc.priv === 1) {
const p = await document.get(domainId, document.TYPE_DOMAIN_ROLE, 'admin');
return p.content;
}
const role = await document.getStatus(domainId, document.TYPE_DOMAIN_ROLE, 0, udoc._id);
const p = await document.get(domainId, document.TYPE_DOMAIN_ROLE, role || 'default');
return p.content;
}
async function getById(domainId, _id) {
const udoc = await coll.findOne({ _id });
if (!udoc) throw new UserNotFoundError(_id);
const role = await collRole.findOne({ _id: udoc.role || 'default' });
udoc.perm = role.perm;
udoc.perm = await getPerm(domainId, udoc);
return new USER(udoc);
}
async function getList(uids) {
async function getList(domainId, uids) {
const r = {};
for (const uid of uids) r[uid] = await getById(uid); // eslint-disable-line no-await-in-loop
// eslint-disable-next-line no-await-in-loop
for (const uid of uids) r[uid] = await getById(domainId, uid);
return r;
}
async function getByUname(uname) {
async function getByUname(domainId, uname) {
const unameLower = uname.trim().toLowerCase();
const udoc = await coll.findOne({ unameLower });
if (!udoc) throw new UserNotFoundError(uname);
const role = await collRole.findOne({ _id: udoc.role || 'default' });
udoc.perm = role.perm;
udoc.perm = await getPerm(domainId, udoc);
return new USER(udoc);
}
async function getByEmail(mail, ignoreMissing = false) {
async function getByEmail(domainId, mail, ignoreMissing = false) {
const mailLower = mail.trim().toLowerCase();
const udoc = await coll.findOne({ mailLower });
if (!udoc) {
if (ignoreMissing) return null;
throw new UserNotFoundError(mail);
}
const role = await collRole.findOne({ _id: udoc.role || 'default' });
udoc.perm = role.perm;
udoc.perm = await getPerm(domainId, udoc);
return new USER(udoc);
}
@@ -106,30 +114,27 @@ async function inc(_id, field, n = 1) {
}
async function create({
uid, mail, uname, password, regip = '127.0.0.1', role = 'default',
uid, mail, uname, password, regip = '127.0.0.1', priv = perm.PRIV_NONE,
}) {
const salt = String.random();
if (!uid) uid = system.inc('user');
try {
await Promise.all([
coll.insertOne({
_id: uid,
mail,
mailLower: mail.trim().toLowerCase(),
uname,
unameLower: uname.trim().toLowerCase(),
password: pwhash.hash(password, salt),
salt,
hashType: 'hydro',
regat: new Date(),
regip,
loginat: new Date(),
loginip: regip,
role,
gravatar: mail,
}),
collRole.updateOne({ _id: role }, { $inc: { count: 1 } }),
]);
await coll.insertOne({
_id: uid,
mail,
mailLower: mail.trim().toLowerCase(),
uname,
unameLower: uname.trim().toLowerCase(),
password: pwhash.hash(password, salt),
salt,
hashType: 'hydro',
regat: new Date(),
regip,
loginat: new Date(),
loginip: regip,
priv,
gravatar: mail,
});
} catch (e) {
throw new UserAlreadyExistError([uid, uname, mail]);
}
@@ -146,37 +151,31 @@ async function getPrefixList(prefix, limit = 50) {
return udocs;
}
async function setRole(uid, role) {
const udoc = await getById(uid);
return await Promise.all([ // eslint-disable-line no-return-await
coll.findOneAndUpdate({ _id: uid }, { $set: { role } }),
collRole.updateOne({ _id: udoc.role }, { $inc: { count: -1 } }),
collRole.updateOne({ _id: role }, { $inc: { count: 1 } }),
function setRole(domainId, uid, role) {
return document.setStatus(domainId, document.TYPE_DOMAIN_ROLE, 0, uid, { role });
}
function getRoles(domainId) {
return document.getMulti(domainId, document.TYPE_DOMAIN_ROLE).sort('_id', 1).toArray();
}
function getRole(domainId, name) {
return document.get(domainId, document.TYPE_DOMAIN_ROLE, name);
}
function addRole(domainId, name, permission) {
return document.add(domainId, permission, 1, document.TYPE_DOMAIN_ROLE, name);
}
function deleteRoles(domainId, roles) {
return Promise.all([
document.deleteMulti(domainId, document.TYPE_DOMAIN_ROLE, { docId: { $in: roles } }),
document.deleteMultiStatus(domainId, document.TYPE_DOMAIN_ROLE, { role: { $in: roles } }),
]);
}
function getRoles() {
return collRole.find().sort('_id', 1).toArray();
}
function getRole(name) {
return collRole.findOne({ _id: name });
}
function addRole(name, permission) {
return collRole.insertOne({ _id: name, perm: permission, count: 0 });
}
function deleteRoles(roles) {
function ensureIndexes() {
return Promise.all([
coll.updateMany({ role: { $in: roles } }, { $set: { role: 'default' } }),
collRole.deleteMany({ _id: { $in: roles } }),
]);
}
function index() {
return Promise.all([
collRole.updateOne({ _id: 'root' }, { $set: { perm: perm.PERM_ALL } }),
coll.createIndex('unameLower', { unique: true }),
coll.createIndex('mailLower', { sparse: true }),
]);
@@ -200,5 +199,5 @@ global.Hydro.model.user = module.exports = {
getRoles,
addRole,
deleteRoles,
index,
ensureIndexes,
};