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

282 lines
8.0 KiB
JavaScript

const builtin = require('./builtin');
const document = require('./document');
5 years ago
const system = require('./system');
4 years ago
const token = require('./token');
const setting = require('./setting');
const { UserNotFoundError, UserAlreadyExistError, LoginError } = require('../error');
const perm = require('../permission');
4 years ago
const pwhash = require('../lib/hash.hydro');
5 years ago
const db = require('../service/db');
const coll = db.collection('user');
class USER {
constructor(udoc, dudoc) {
this.udoc = () => udoc;
this.dudoc = () => dudoc;
this._id = udoc._id;
this.mail = udoc.mail;
this.uname = udoc.uname;
this.salt = () => udoc.salt;
this.hash = () => udoc.hash;
this.priv = udoc.priv;
for (const key in setting.SETTINGS_BY_KEY) {
if (udoc[key]) this[key] = udoc[key];
else if (setting.SETTINGS_BY_KEY[key].value) {
this[key] = setting.SETTINGS_BY_KEY[key].value;
}
}
this.regat = udoc.regat;
this.loginat = udoc.loginat;
4 years ago
this.ban = udoc.ban || false;
this.nAccept = dudoc.nAccept || 0;
this.nSubmit = dudoc.nSubmit || 0;
this.rating = dudoc.rating || 1500;
this.perm = dudoc.perm;
this.role = dudoc.role || 'default';
}
5 years ago
hasPerm(p) {
return this.perm.includes(p);
}
5 years ago
checkPassword(password) {
4 years ago
const h = global.Hydro.lib[`hash.${this.hashType || 'hydro'}`];
4 years ago
if (!h) throw new Error('Unknown hash method');
if (!(h(password, this.salt(), this) === this.hash())) {
throw new LoginError(this.uname);
}
}
}
5 years ago
async function getInDomain(domainId, udoc) {
4 years ago
let dudoc = await document.getStatus(domainId, document.TYPE_DOMAIN_USER, 0, udoc._id);
dudoc = dudoc || {};
if (udoc._id === 1) dudoc.role = 'guest';
if (udoc.priv === 1) dudoc.role = 'admin';
const p = await document.get(domainId, document.TYPE_DOMAIN_USER, dudoc.role || 'default');
dudoc.perm = p ? p.content : builtin.BUILTIN_ROLES[dudoc.role || 'default'].perm;
return dudoc;
}
async function getById(domainId, _id, throwError = false) {
5 years ago
const udoc = await coll.findOne({ _id });
if (!udoc) {
if (throwError) throw new UserNotFoundError(_id);
else return null;
}
const dudoc = await getInDomain(domainId, udoc);
return new USER(udoc, dudoc);
}
5 years ago
async function getList(domainId, uids) {
4 years ago
uids = new Set(uids);
5 years ago
const r = {};
// eslint-disable-next-line no-await-in-loop
for (const uid of uids) r[uid] = await getById(domainId, uid);
5 years ago
return r;
}
5 years ago
async function getByUname(domainId, uname, ignoreMissing = false) {
5 years ago
const unameLower = uname.trim().toLowerCase();
const udoc = await coll.findOne({ unameLower });
if (!udoc) {
if (ignoreMissing) return null;
throw new UserNotFoundError(uname);
}
const dudoc = await getInDomain(domainId, udoc);
return new USER(udoc, dudoc);
}
5 years ago
async function getByEmail(domainId, 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
}
const dudoc = await getInDomain(domainId, udoc);
return new USER(udoc, dudoc);
}
5 years ago
5 years ago
function setPassword(uid, password) {
4 years ago
const salt = String.random();
return coll.findOneAndUpdate(
{ _id: uid },
{ $set: { salt, hash: pwhash(password, salt), hashType: 'hydro' } },
);
}
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);
4 years ago
const salt = String.random();
return await coll.findOneAndUpdate(
{ _id: udoc._id },
{ $set: { salt, hash: pwhash(newPassword, salt), hashType: 'hydro' } },
);
}
5 years ago
async function inc(_id, field, n = 1) {
const udoc = await coll.findOne({ _id });
udoc[field] = udoc[field] + n || n;
await coll.updateOne({ _id }, { $set: { [field]: udoc[field] } });
5 years ago
return udoc;
}
5 years ago
function setInDomain(domainId, uid, params) {
return document.setStatus(domainId, document.TYPE_DOMAIN_USER, 0, uid, params);
}
async function incDomain(domainId, uid, field, n = 1) {
const dudoc = await getInDomain(domainId, { _id: uid });
dudoc[field] = dudoc[field] + n || n;
await setInDomain(domainId, uid, { [field]: dudoc[field] });
return dudoc;
}
/**
* @returns {Promise<number>} uid
*/
5 years ago
async function create({
uid, mail, uname, password, regip = '127.0.0.1', priv = perm.PRIV_NONE,
5 years ago
}) {
4 years ago
const salt = String.random();
if (!uid) uid = await system.inc('user');
try {
await coll.insertOne({
_id: uid,
mail,
mailLower: mail.trim().toLowerCase(),
uname,
unameLower: uname.trim().toLowerCase(),
password: pwhash(password, salt),
salt,
hashType: 'hydro',
regat: new Date(),
regip,
loginat: new Date(),
loginip: regip,
priv,
gravatar: mail,
});
} catch (e) {
5 years ago
throw new UserAlreadyExistError([uid, uname, mail]);
}
return uid;
}
5 years ago
function getMulti(params) {
return coll.find(params);
}
4 years ago
function setMultiInDomain(domainId, query, params) {
return document.setMultiStatus(domainId, document.TYPE_DOMAIN_USER, query, 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;
}
function setRole(domainId, uid, role) {
return document.setStatus(domainId, document.TYPE_DOMAIN_USER, 0, uid, { role });
5 years ago
}
function setRoles(domainId, roles) {
const tasks = [];
for (const role in roles) {
tasks.push(document.set(
domainId, document.TYPE_DOMAIN_USER, role, { content: roles[role] },
));
}
return Promise.all(tasks);
}
async function getRoles(domainId) {
const docs = await document.getMulti(domainId, document.TYPE_DOMAIN_USER).sort('_id', 1).toArray();
const roles = [];
for (const doc of docs) {
roles.push({ _id: doc.docId, perm: doc.content });
}
return roles;
}
function getRole(domainId, name) {
return document.get(domainId, document.TYPE_DOMAIN_USER, name);
5 years ago
}
function getMultiInDomain(domainId, query) {
return document.getMultiStatus(domainId, document.TYPE_DOMAIN_USER, query);
}
function addRole(domainId, name, permission) {
return document.add(domainId, permission, 1, document.TYPE_DOMAIN_USER, name);
5 years ago
}
function deleteRoles(domainId, roles) {
return Promise.all([
document.deleteMulti(domainId, document.TYPE_DOMAIN_USER, { docId: { $in: roles } }),
document.deleteMultiStatus(domainId, document.TYPE_DOMAIN_USER, { role: { $in: roles } }),
]);
}
4 years ago
function ban(uid) {
return Promise.all([
coll.updateOne({ _id: uid }, { $set: { ban: true } }),
4 years ago
token.delByUid(uid),
4 years ago
]);
}
function setSuperAdmin(uid) {
return setById(uid, { priv: 1 });
}
function ensureIndexes() {
return Promise.all([
coll.createIndex('unameLower', { unique: true }),
coll.createIndex('mailLower', { sparse: true }),
]);
5 years ago
}
4 years ago
global.Hydro.model.user = module.exports = {
5 years ago
changePassword,
create,
getByEmail,
getById,
getByUname,
5 years ago
getMulti,
5 years ago
inc,
incDomain,
5 years ago
setById,
setEmail,
setPassword,
setInDomain,
4 years ago
setMultiInDomain,
getMultiInDomain,
getPrefixList,
5 years ago
setRole,
setRoles,
5 years ago
getRole,
5 years ago
getList,
getRoles,
getInDomain,
5 years ago
addRole,
deleteRoles,
setSuperAdmin,
4 years ago
ban,
ensureIndexes,
5 years ago
};