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

137 lines
4.0 KiB
JavaScript

5 years ago
const system = require('./system');
const { UserNotFoundError, UserAlreadyExistError } = require('../error');
const pwhash = require('../lib/pwhash');
const validator = require('../lib/validator');
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) {
5 years ago
return this.perm === '-' || (this.perm || '').includes(perm);
}
5 years ago
checkPassword(password) {
return pwhash.check(password, this.salt, this.hash);
}
}
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
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;
}
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);
}
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
function setPassword(uid, password) {
validator.checkPassword(password);
5 years ago
const salt = pwhash.salt();
return coll.findOneAndUpdate({ _id: uid }, {
$set: { salt, hash: pwhash.hash(password, salt) },
});
}
function setById(uid, args) {
coll.findOneAndUpdate({ _id: uid }, { $set: args });
}
5 years ago
function setEmail(uid, mail) {
validator.checkEmail(mail);
return setById(uid, { mail, mailLower: mail.trim().toLowerCase() });
}
async function changePassword(uid, currentPassword, newPassword) {
validator.checkPassword(newPassword);
5 years ago
const udoc = await getById(uid);
udoc.checkPassword(currentPassword);
5 years ago
const salt = pwhash.salt();
return await coll.findOneAndUpdate({
5 years ago
_id: udoc._id,
}, {
5 years ago
$set: { salt, hash: pwhash.hash(newPassword, salt) },
});
}
5 years ago
async function create({
uid, mail, uname, password, regip = '127.0.0.1', role = 'default',
}) {
validator.checkUname(uname);
validator.checkPassword(password);
validator.checkEmail(mail);
5 years ago
const salt = pwhash.salt();
if (!uid) uid = system.incUserCounter();
try {
await coll.insertOne({
5 years ago
_id: uid,
mail,
mailLower: mail.trim().toLowerCase(),
uname,
5 years ago
unameLower: uname.trim().toLowerCase(),
password: pwhash.hash(password, salt),
salt,
regat: new Date(),
regip,
loginat: new Date(),
loginip: regip,
5 years ago
role,
5 years ago
gravatar: mail,
});
} catch (e) {
5 years ago
throw new UserAlreadyExistError([uid, uname, mail]);
}
}
function getMany(params) {
return coll.find(params);
}
module.exports = {
5 years ago
changePassword,
create,
getByEmail,
getById,
getByUname,
getMany,
setById,
setEmail,
setPassword,
getList,
};