You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/model/user.js

196 lines
5.3 KiB
JavaScript

5 years ago
const system = require('./system');
const { UserNotFoundError, UserAlreadyExistError } = require('../error');
const perm = require('../permission');
5 years ago
const pwhash = require('../lib/pwhash');
const db = require('../service/db');
const coll = db.collection('user');
const collRole = db.collection('role');
class USER {
constructor(user) {
this._id = user._id;
this.mail = user.mail;
this.uname = user.uname;
this.salt = user.salt;
this.hash = user.hash;
this.perm = user.perm;
this.lang = user.language || 'zh_CN';
this.codeLang = user.codeLang || 'c';
this.codeTemplate = user.codeTemplate || '';
this.regat = user.regat;
this.loginat = user.loginat;
this.bio = user.bio || '';
this.nAccept = user.nAccept || 0;
this.nSubmit = user.nSubmit || 0;
}
5 years ago
hasPerm(perm) {
return this.perm.includes(perm);
}
5 years ago
checkPassword(password) {
return pwhash.check(password, this.salt, this.hash);
}
}
5 years ago
async function getById(_id) {
5 years ago
const udoc = await coll.findOne({ _id });
if (!udoc) throw new UserNotFoundError(_id);
5 years ago
const role = await collRole.findOne({ _id: udoc.role || 'default' });
udoc.perm = role.perm;
return new USER(udoc);
}
5 years ago
5 years ago
async function getList(uids) {
5 years ago
const r = {};
for (const uid of uids) r[uid] = await getById(uid); // eslint-disable-line no-await-in-loop
5 years ago
return r;
}
5 years ago
async function getByUname(uname) {
5 years ago
const unameLower = uname.trim().toLowerCase();
const udoc = await coll.findOne({ unameLower });
if (!udoc) throw new UserNotFoundError(uname);
5 years ago
const role = await collRole.findOne({ _id: udoc.role || 'default' });
udoc.perm = role.perm;
return new USER(udoc);
}
5 years ago
async function getByEmail(mail, ignoreMissing = false) {
5 years ago
const mailLower = mail.trim().toLowerCase();
const udoc = await coll.findOne({ mailLower });
5 years ago
if (!udoc) {
if (ignoreMissing) return null;
5 years ago
throw new UserNotFoundError(mail);
5 years ago
}
5 years ago
const role = await collRole.findOne({ _id: udoc.role || 'default' });
udoc.perm = role.perm;
return new USER(udoc);
}
5 years ago
5 years ago
function setPassword(uid, password) {
const salt = pwhash.salt();
return coll.findOneAndUpdate({ _id: uid }, {
$set: { salt, hash: pwhash.hash(password, salt) },
});
}
5 years ago
function setById(uid, args) {
5 years ago
return coll.findOneAndUpdate({ _id: uid }, { $set: args });
}
5 years ago
5 years ago
function setEmail(uid, mail) {
return setById(uid, { mail, mailLower: mail.trim().toLowerCase() });
}
5 years ago
async function changePassword(uid, currentPassword, newPassword) {
5 years ago
const udoc = await getById(uid);
udoc.checkPassword(currentPassword);
5 years ago
const salt = pwhash.salt();
return await coll.findOneAndUpdate({ // eslint-disable-line no-return-await
5 years ago
_id: udoc._id,
}, {
5 years ago
$set: { salt, hash: pwhash.hash(newPassword, salt) },
});
}
5 years ago
5 years ago
async function inc(_id, field, n) {
await coll.findOneAndUpdate({ _id }, { $inc: { [field]: n } });
const udoc = await getById(_id);
return udoc;
}
5 years ago
5 years ago
async function create({
uid, mail, uname, password, regip = '127.0.0.1', role = 'default',
}) {
const salt = pwhash.salt();
if (!uid) uid = system.inc('user');
try {
5 years ago
await Promise.all([
coll.insertOne({
_id: uid,
mail,
mailLower: mail.trim().toLowerCase(),
uname,
unameLower: uname.trim().toLowerCase(),
password: pwhash.hash(password, salt),
salt,
regat: new Date(),
regip,
loginat: new Date(),
loginip: regip,
role,
gravatar: mail,
}),
collRole.updateOne({ _id: role }, { $inc: { count: 1 } }),
5 years ago
]);
} catch (e) {
5 years ago
throw new UserAlreadyExistError([uid, uname, mail]);
}
}
5 years ago
function getMulti(params) {
return coll.find(params);
}
async function getPrefixList(prefix, limit = 50) {
prefix = prefix.toLowerCase();
const $regex = new RegExp(`\\A\\Q${prefix.replace(/\\E/gmi, /\\E\\E\\Q/gmi)}\\E`, 'gmi');
const udocs = await coll.find({ unameLower: { $regex } }).limit(limit).toArray();
return udocs;
}
5 years ago
async function setRole(uid, role) {
const udoc = await getById(uid);
return await Promise.all([
coll.findOneAndUpdate({ _id: uid }, { role }),
collRole.updateOne({ _id: udoc.role }, { $inc: { count: -1 } }),
collRole.updateOne({ _id: role }, { $inc: { count: 1 } }),
]);
}
function getRoles() {
return collRole.find().sort('_id', 1).toArray();
}
5 years ago
function getRole(name) {
return collRole.findOne({ _id: name });
}
function addRole(name, perm) {
return collRole.insertOne({ _id: name, perm, count: 0 });
}
function deleteRoles(roles) {
return Promise.all([
coll.updateMany({ role: { $in: roles } }, { $set: { role: 'default' } }),
collRole.deleteMany({ _id: { $in: roles } }),
]);
}
function init() {
return collRole.update({ _id: 'root' }, { $set: { perm: perm.PERM_ALL } });
5 years ago
}
module.exports = {
5 years ago
changePassword,
create,
getByEmail,
getById,
getByUname,
5 years ago
getMulti,
5 years ago
inc,
5 years ago
setById,
setEmail,
setPassword,
getPrefixList,
5 years ago
setRole,
getRole,
5 years ago
getList,
getRoles,
5 years ago
addRole,
deleteRoles,
init,
5 years ago
};