update model.user.create() api

pull/10/head
undefined 4 years ago
parent 162d93e5db
commit 37756c3724

@ -5,7 +5,3 @@ vscode:
extensions:
- dbaeumer.vscode-eslint@2.1.3:1NRvj3UKNTNwmYjptmUmIw==
- vscode-icons-team.vscode-icons@10.2.0:DrQUJPtFm0woSFiyitha8Q==
ports:
- port: 8888
onOpen: open-preview
- port: 27017

@ -2,9 +2,7 @@
Hydro是一个高效的信息学在线测评系统。特点易于部署轻量功能强大且易于扩展。
[部署说明](docs/deploy.md)
[附加组件开发说明](docs/addon.md)
[Hydro开发说明](docs/development.md)
[中文文档](https://hydro-dev.github.io)
[Hydro UI 传送门](https://github.com/hydro-dev/ui-default)
如果您认为本项目有价值,欢迎 star 。

@ -26,16 +26,9 @@ export async function load(call, args) {
bus.subscribe(['system_database_connected'], h);
require('../service/db');
});
for (const i of builtinLib) require(`../lib/${i}`);
await lib(pending, fail);
require('../service/gridfs');
require('../service/monitor');
const server = require('../service/server');
await server.prepare();
await service(pending, fail);
for (const i of builtinModel) require(`../model/${i}`);
for (const i of builtinHandler) require(`../handler/${i}`);
await model(pending, fail);
for (const m in global.Hydro.model) {
if (global.Hydro.model[m].ensureIndexes) {
await global.Hydro.model[m].ensureIndexes();

@ -172,7 +172,8 @@ class ProblemDetailHandler extends ProblemHandler {
@param('pid', Types.String, null, parsePid)
async _prepare(domainId: string, pid: number | string) {
this.response.template = 'problem_detail.html';
this.pdoc = await problem.get(domainId, pid, this.user._id, true);
this.pdoc = await problem.get(domainId, pid, this.user._id);
if (!this.pdoc) throw new ProblemNotFoundError(domainId, pid);
if (this.pdoc.hidden && this.pdoc.owner !== this.user._id) {
this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
}

@ -72,7 +72,7 @@ class RecordDetailHandler extends RecordHandler {
}
if (rdoc.type !== 'run') rdoc.stdout = rdoc.stderr = '';
const [pdoc, udoc] = await Promise.all([
problem.get(domainId, rdoc.pid, null, false),
problem.get(domainId, rdoc.pid, null),
user.getById(domainId, rdoc.uid),
]);
this.response.body = {
@ -138,7 +138,7 @@ class RecordMainConnectionHandler extends RecordConnectionHandler {
// eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([
user.getById(this.domainId, rdoc.uid),
problem.get(this.domainId, rdoc.pid, null, false),
problem.get(this.domainId, rdoc.pid, null),
]);
if (pdoc && pdoc.hidden && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null;
else this.send({ html: await this.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });

@ -102,9 +102,7 @@ class UserRegisterWithCodeHandler extends Handler {
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
if (password !== verifyPassword) throw new VerifyPasswordError();
const uid = await system.inc('user');
await user.create({
uid, uname, password, mail, regip: this.request.ip,
});
await user.create(mail, uname, password, uid, this.request.ip);
await token.del(code, token.TYPE_REGISTRATION);
this.session.uid = uid;
this.response.redirect = this.url('homepage');
@ -331,9 +329,10 @@ class OauthCallbackHandler extends Handler {
break;
}
}
const uid = await user.create({
mail: r.email, uname: username, password: String.random(32), regip: this.request.ip,
});
const uid = await user.create(
r.email, username, String.random(32),
undefined, this.request.ip,
);
const $set: any = {
oauth: args.type,
};

@ -2,13 +2,14 @@ import fs from 'fs';
import path from 'path';
import serialize from 'serialize-javascript';
import nunjucks from 'nunjucks';
import { argv } from 'yargs';
import * as markdown from './markdown';
import * as misc from './misc';
class Loader extends nunjucks.Loader {
// eslint-disable-next-line class-methods-use-this
getSource(name: string) {
if (!process.env.debug) {
if (!argv.template) {
if (!global.Hydro.ui.template[name]) throw new Error(`Cannot get template ${name}`);
return {
src: global.Hydro.ui.template[name],
@ -17,12 +18,11 @@ class Loader extends nunjucks.Loader {
};
}
let fullpath = null;
const base = path.join(process.cwd(), 'templates');
const p = path.resolve(base, name);
const p = path.resolve(argv.template as string, name);
if (fs.existsSync(p)) fullpath = p;
if (!fullpath) throw new Error(`Cannot get template ${name}`);
return {
src: fs.readFileSync(fullpath, 'utf-8'),
src: fs.readFileSync(fullpath, 'utf-8').toString(),
path: fullpath,
noCache: true,
};

@ -42,7 +42,7 @@ export async function add(
export async function get(
domainId: string, pid: string | number,
uid: number = null, doThrow = true,
uid: number = null,
): Promise<Pdoc> {
if (typeof pid !== 'number') {
if (!Number.isNaN(parseInt(pid, 10))) pid = parseInt(pid, 10);
@ -50,10 +50,7 @@ export async function get(
const pdoc = Number.isInteger(pid)
? await document.get(domainId, document.TYPE_PROBLEM, pid)
: (await document.getMulti(domainId, document.TYPE_PROBLEM, { pid }).toArray())[0];
if (!pdoc) {
if (doThrow) throw new ProblemNotFoundError(domainId, pid);
return null;
}
if (!pdoc) return null;
if (uid) {
pdoc.psdoc = await document.getStatus(domainId, document.TYPE_PROBLEM, pdoc.docId, uid);
}

@ -62,7 +62,7 @@ export async function add(domainId: string, data: RdocBase, addTask: boolean): P
judgeAt: null,
});
const [pdoc, res] = await Promise.all([
problem.get(domainId, data.pid, null, false),
problem.get(domainId, data.pid, null),
coll.insertOne(data),
]);
if (addTask) {

@ -164,9 +164,10 @@ export async function inc(_id: number, field: string, n = 1) {
return udoc;
}
export async function create({
uid = null, mail, uname, password, regip = '127.0.0.1', priv = PRIV.PRIV_DEFAULT,
}) {
export async function create(
mail: string, uname: string, password: string,
uid: number, regip = '127.0.0.1', priv = PRIV.PRIV_DEFAULT,
) {
const salt = String.random();
if (!uid) uid = await system.inc('user');
try {

@ -11,14 +11,7 @@ export async function run({ username = '', password = '' } = {}) {
if (username && password) {
const udoc = await user.getById('system', -1);
if (!udoc) {
await user.create({
uid: -1,
mail: 'root@hydro.local',
uname: username,
password,
regip: '127.0.0.1',
priv: PRIV.PRIV_ALL,
});
await user.create('root@hydro.local', username, password, -1, '127.0.0.1', PRIV.PRIV_ALL);
} else {
const salt = String.random();
await user.setById(-1, {

@ -1,17 +1,13 @@
import * as user from '../model/user';
import * as system from '../model/system';
export const description = 'Create a new user';
export async function run({
uname, password, mail, uid,
}) {
if (!uid) uid = await system.inc('user');
else uid = parseInt(uid, 10);
if (Number.isNaN(uid)) throw new Error('uid');
await user.create({
uid, uname, password, mail, regip: '127.0.0.1',
});
if (uid) uid = parseInt(uid, 10);
if (uid && Number.isNaN(uid)) throw new Error('uid');
uid = await user.create(mail, uname, password, uid);
return uid;
}

@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.9.8",
"version": "2.9.9",
"main": "dist/loader.js",
"bin": "bin/hydrooj.js",
"repository": "https://github.com/hydro-dev/Hydro.git",
@ -49,7 +49,7 @@
"@types/koa-morgan": "^1.0.4",
"@types/koa-router": "^7.4.1",
"@types/koa-static-cache": "^5.1.0",
"@types/lodash": "^4.14.157",
"@types/lodash": "^4.14.158",
"@types/lru-cache": "^5.1.0",
"@types/markdown-it": "^10.0.1",
"@types/mongodb": "^3.5.25",

@ -205,10 +205,10 @@
resolved "https://registry.yarnpkg.com/@types/linkify-it/-/linkify-it-2.1.0.tgz#ea3dd64c4805597311790b61e872cbd1ed2cd806"
integrity sha512-Q7DYAOi9O/+cLLhdaSvKdaumWyHbm7HAk/bFwwyTuU0arR5yyCeW5GOoqt4tJTpDRxhpx9Q8kQL6vMpuw9hDSw==
"@types/lodash@^4.14.157":
version "4.14.157"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.157.tgz#fdac1c52448861dfde1a2e1515dbc46e54926dc8"
integrity sha512-Ft5BNFmv2pHDgxV5JDsndOWTRJ+56zte0ZpYLowp03tW+K+t8u8YMOzAnpuqPgzX6WO1XpDIUm7u04M8vdDiVQ==
"@types/lodash@^4.14.158":
version "4.14.158"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.158.tgz#b38ea8b6fe799acd076d7a8d7ab71c26ef77f785"
integrity sha512-InCEXJNTv/59yO4VSfuvNrZHt7eeNtWQEgnieIA+mIC+MOWM9arOWG2eQ8Vhk6NbOre6/BidiXhkZYeDY9U35w==
"@types/lru-cache@^5.1.0":
version "5.1.0"

Loading…
Cancel
Save