mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
update model.user.create() api
This commit is contained in:
1 parent
162d93e5db
commit
37756c3724
14 files changed
+31
-57
No files matched your search
@@ -5,7 +5,3 @@ vscode:
|
|||||||
extensions:
|
extensions:
|
||||||
- dbaeumer.vscode-eslint@2.1.3:1NRvj3UKNTNwmYjptmUmIw==
|
- dbaeumer.vscode-eslint@2.1.3:1NRvj3UKNTNwmYjptmUmIw==
|
||||||
- vscode-icons-team.vscode-icons@10.2.0:DrQUJPtFm0woSFiyitha8Q==
|
- vscode-icons-team.vscode-icons@10.2.0:DrQUJPtFm0woSFiyitha8Q==
|
||||||
ports:
|
|
||||||
- port: 8888
|
|
||||||
onOpen: open-preview
|
|
||||||
- port: 27017
|
|
||||||
@@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
Hydro是一个高效的信息学在线测评系统。特点:易于部署,轻量,功能强大且易于扩展。
|
Hydro是一个高效的信息学在线测评系统。特点:易于部署,轻量,功能强大且易于扩展。
|
||||||
|
|
||||||
[部署说明](docs/deploy.md)
|
[中文文档](https://hydro-dev.github.io)
|
||||||
[附加组件开发说明](docs/addon.md)
|
|
||||||
[Hydro开发说明](docs/development.md)
|
|
||||||
[Hydro UI 传送门](https://github.com/hydro-dev/ui-default)
|
[Hydro UI 传送门](https://github.com/hydro-dev/ui-default)
|
||||||
|
|
||||||
如果您认为本项目有价值,欢迎 star 。
|
如果您认为本项目有价值,欢迎 star 。
|
||||||
|
|||||||
@@ -26,16 +26,9 @@ export async function load(call, args) {
|
|||||||
bus.subscribe(['system_database_connected'], h);
|
bus.subscribe(['system_database_connected'], h);
|
||||||
require('../service/db');
|
require('../service/db');
|
||||||
});
|
});
|
||||||
for (const i of builtinLib) require(`../lib/${i}`);
|
|
||||||
await lib(pending, fail);
|
|
||||||
require('../service/gridfs');
|
|
||||||
require('../service/monitor');
|
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 builtinModel) require(`../model/${i}`);
|
||||||
for (const i of builtinHandler) require(`../handler/${i}`);
|
for (const i of builtinHandler) require(`../handler/${i}`);
|
||||||
await model(pending, fail);
|
|
||||||
for (const m in global.Hydro.model) {
|
for (const m in global.Hydro.model) {
|
||||||
if (global.Hydro.model[m].ensureIndexes) {
|
if (global.Hydro.model[m].ensureIndexes) {
|
||||||
await 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)
|
@param('pid', Types.String, null, parsePid)
|
||||||
async _prepare(domainId: string, pid: number | string) {
|
async _prepare(domainId: string, pid: number | string) {
|
||||||
this.response.template = 'problem_detail.html';
|
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) {
|
if (this.pdoc.hidden && this.pdoc.owner !== this.user._id) {
|
||||||
this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
|
this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ class RecordDetailHandler extends RecordHandler {
|
|||||||
}
|
}
|
||||||
if (rdoc.type !== 'run') rdoc.stdout = rdoc.stderr = '';
|
if (rdoc.type !== 'run') rdoc.stdout = rdoc.stderr = '';
|
||||||
const [pdoc, udoc] = await Promise.all([
|
const [pdoc, udoc] = await Promise.all([
|
||||||
problem.get(domainId, rdoc.pid, null, false),
|
problem.get(domainId, rdoc.pid, null),
|
||||||
user.getById(domainId, rdoc.uid),
|
user.getById(domainId, rdoc.uid),
|
||||||
]);
|
]);
|
||||||
this.response.body = {
|
this.response.body = {
|
||||||
@@ -138,7 +138,7 @@ class RecordMainConnectionHandler extends RecordConnectionHandler {
|
|||||||
// eslint-disable-next-line prefer-const
|
// eslint-disable-next-line prefer-const
|
||||||
let [udoc, pdoc] = await Promise.all([
|
let [udoc, pdoc] = await Promise.all([
|
||||||
user.getById(this.domainId, rdoc.uid),
|
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;
|
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 }) });
|
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 (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
|
||||||
if (password !== verifyPassword) throw new VerifyPasswordError();
|
if (password !== verifyPassword) throw new VerifyPasswordError();
|
||||||
const uid = await system.inc('user');
|
const uid = await system.inc('user');
|
||||||
await user.create({
|
await user.create(mail, uname, password, uid, this.request.ip);
|
||||||
uid, uname, password, mail, regip: this.request.ip,
|
|
||||||
});
|
|
||||||
await token.del(code, token.TYPE_REGISTRATION);
|
await token.del(code, token.TYPE_REGISTRATION);
|
||||||
this.session.uid = uid;
|
this.session.uid = uid;
|
||||||
this.response.redirect = this.url('homepage');
|
this.response.redirect = this.url('homepage');
|
||||||
@@ -331,9 +329,10 @@ class OauthCallbackHandler extends Handler {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const uid = await user.create({
|
const uid = await user.create(
|
||||||
mail: r.email, uname: username, password: String.random(32), regip: this.request.ip,
|
r.email, username, String.random(32),
|
||||||
});
|
undefined, this.request.ip,
|
||||||
|
);
|
||||||
const $set: any = {
|
const $set: any = {
|
||||||
oauth: args.type,
|
oauth: args.type,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,13 +2,14 @@ import fs from 'fs';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import serialize from 'serialize-javascript';
|
import serialize from 'serialize-javascript';
|
||||||
import nunjucks from 'nunjucks';
|
import nunjucks from 'nunjucks';
|
||||||
|
import { argv } from 'yargs';
|
||||||
import * as markdown from './markdown';
|
import * as markdown from './markdown';
|
||||||
import * as misc from './misc';
|
import * as misc from './misc';
|
||||||
|
|
||||||
class Loader extends nunjucks.Loader {
|
class Loader extends nunjucks.Loader {
|
||||||
// eslint-disable-next-line class-methods-use-this
|
// eslint-disable-next-line class-methods-use-this
|
||||||
getSource(name: string) {
|
getSource(name: string) {
|
||||||
if (!process.env.debug) {
|
if (!argv.template) {
|
||||||
if (!global.Hydro.ui.template[name]) throw new Error(`Cannot get template ${name}`);
|
if (!global.Hydro.ui.template[name]) throw new Error(`Cannot get template ${name}`);
|
||||||
return {
|
return {
|
||||||
src: global.Hydro.ui.template[name],
|
src: global.Hydro.ui.template[name],
|
||||||
@@ -17,12 +18,11 @@ class Loader extends nunjucks.Loader {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
let fullpath = null;
|
let fullpath = null;
|
||||||
const base = path.join(process.cwd(), 'templates');
|
const p = path.resolve(argv.template as string, name);
|
||||||
const p = path.resolve(base, name);
|
|
||||||
if (fs.existsSync(p)) fullpath = p;
|
if (fs.existsSync(p)) fullpath = p;
|
||||||
if (!fullpath) throw new Error(`Cannot get template ${name}`);
|
if (!fullpath) throw new Error(`Cannot get template ${name}`);
|
||||||
return {
|
return {
|
||||||
src: fs.readFileSync(fullpath, 'utf-8'),
|
src: fs.readFileSync(fullpath, 'utf-8').toString(),
|
||||||
path: fullpath,
|
path: fullpath,
|
||||||
noCache: true,
|
noCache: true,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ export async function add(
|
|||||||
|
|
||||||
export async function get(
|
export async function get(
|
||||||
domainId: string, pid: string | number,
|
domainId: string, pid: string | number,
|
||||||
uid: number = null, doThrow = true,
|
uid: number = null,
|
||||||
): Promise<Pdoc> {
|
): Promise<Pdoc> {
|
||||||
if (typeof pid !== 'number') {
|
if (typeof pid !== 'number') {
|
||||||
if (!Number.isNaN(parseInt(pid, 10))) pid = parseInt(pid, 10);
|
if (!Number.isNaN(parseInt(pid, 10))) pid = parseInt(pid, 10);
|
||||||
@@ -50,10 +50,7 @@ export async function get(
|
|||||||
const pdoc = Number.isInteger(pid)
|
const pdoc = Number.isInteger(pid)
|
||||||
? await document.get(domainId, document.TYPE_PROBLEM, pid)
|
? await document.get(domainId, document.TYPE_PROBLEM, pid)
|
||||||
: (await document.getMulti(domainId, document.TYPE_PROBLEM, { pid }).toArray())[0];
|
: (await document.getMulti(domainId, document.TYPE_PROBLEM, { pid }).toArray())[0];
|
||||||
if (!pdoc) {
|
if (!pdoc) return null;
|
||||||
if (doThrow) throw new ProblemNotFoundError(domainId, pid);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (uid) {
|
if (uid) {
|
||||||
pdoc.psdoc = await document.getStatus(domainId, document.TYPE_PROBLEM, pdoc.docId, 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,
|
judgeAt: null,
|
||||||
});
|
});
|
||||||
const [pdoc, res] = await Promise.all([
|
const [pdoc, res] = await Promise.all([
|
||||||
problem.get(domainId, data.pid, null, false),
|
problem.get(domainId, data.pid, null),
|
||||||
coll.insertOne(data),
|
coll.insertOne(data),
|
||||||
]);
|
]);
|
||||||
if (addTask) {
|
if (addTask) {
|
||||||
|
|||||||
+4
-3
@@ -164,9 +164,10 @@ export async function inc(_id: number, field: string, n = 1) {
|
|||||||
return udoc;
|
return udoc;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function create({
|
export async function create(
|
||||||
uid = null, mail, uname, password, regip = '127.0.0.1', priv = PRIV.PRIV_DEFAULT,
|
mail: string, uname: string, password: string,
|
||||||
}) {
|
uid: number, regip = '127.0.0.1', priv = PRIV.PRIV_DEFAULT,
|
||||||
|
) {
|
||||||
const salt = String.random();
|
const salt = String.random();
|
||||||
if (!uid) uid = await system.inc('user');
|
if (!uid) uid = await system.inc('user');
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -11,14 +11,7 @@ export async function run({ username = '', password = '' } = {}) {
|
|||||||
if (username && password) {
|
if (username && password) {
|
||||||
const udoc = await user.getById('system', -1);
|
const udoc = await user.getById('system', -1);
|
||||||
if (!udoc) {
|
if (!udoc) {
|
||||||
await user.create({
|
await user.create('root@hydro.local', username, password, -1, '127.0.0.1', PRIV.PRIV_ALL);
|
||||||
uid: -1,
|
|
||||||
mail: 'root@hydro.local',
|
|
||||||
uname: username,
|
|
||||||
password,
|
|
||||||
regip: '127.0.0.1',
|
|
||||||
priv: PRIV.PRIV_ALL,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
const salt = String.random();
|
const salt = String.random();
|
||||||
await user.setById(-1, {
|
await user.setById(-1, {
|
||||||
|
|||||||
@@ -1,17 +1,13 @@
|
|||||||
import * as user from '../model/user';
|
import * as user from '../model/user';
|
||||||
import * as system from '../model/system';
|
|
||||||
|
|
||||||
export const description = 'Create a new user';
|
export const description = 'Create a new user';
|
||||||
|
|
||||||
export async function run({
|
export async function run({
|
||||||
uname, password, mail, uid,
|
uname, password, mail, uid,
|
||||||
}) {
|
}) {
|
||||||
if (!uid) uid = await system.inc('user');
|
if (uid) uid = parseInt(uid, 10);
|
||||||
else uid = parseInt(uid, 10);
|
if (uid && Number.isNaN(uid)) throw new Error('uid');
|
||||||
if (Number.isNaN(uid)) throw new Error('uid');
|
uid = await user.create(mail, uname, password, uid);
|
||||||
await user.create({
|
|
||||||
uid, uname, password, mail, regip: '127.0.0.1',
|
|
||||||
});
|
|
||||||
return uid;
|
return uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "2.9.8",
|
"version": "2.9.9",
|
||||||
"main": "dist/loader.js",
|
"main": "dist/loader.js",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"repository": "https://github.com/hydro-dev/Hydro.git",
|
"repository": "https://github.com/hydro-dev/Hydro.git",
|
||||||
@@ -49,7 +49,7 @@
|
|||||||
"@types/koa-morgan": "^1.0.4",
|
"@types/koa-morgan": "^1.0.4",
|
||||||
"@types/koa-router": "^7.4.1",
|
"@types/koa-router": "^7.4.1",
|
||||||
"@types/koa-static-cache": "^5.1.0",
|
"@types/koa-static-cache": "^5.1.0",
|
||||||
"@types/lodash": "^4.14.157",
|
"@types/lodash": "^4.14.158",
|
||||||
"@types/lru-cache": "^5.1.0",
|
"@types/lru-cache": "^5.1.0",
|
||||||
"@types/markdown-it": "^10.0.1",
|
"@types/markdown-it": "^10.0.1",
|
||||||
"@types/mongodb": "^3.5.25",
|
"@types/mongodb": "^3.5.25",
|
||||||
|
|||||||
@@ -205,10 +205,10 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@types/linkify-it/-/linkify-it-2.1.0.tgz#ea3dd64c4805597311790b61e872cbd1ed2cd806"
|
resolved "https://registry.yarnpkg.com/@types/linkify-it/-/linkify-it-2.1.0.tgz#ea3dd64c4805597311790b61e872cbd1ed2cd806"
|
||||||
integrity sha512-Q7DYAOi9O/+cLLhdaSvKdaumWyHbm7HAk/bFwwyTuU0arR5yyCeW5GOoqt4tJTpDRxhpx9Q8kQL6vMpuw9hDSw==
|
integrity sha512-Q7DYAOi9O/+cLLhdaSvKdaumWyHbm7HAk/bFwwyTuU0arR5yyCeW5GOoqt4tJTpDRxhpx9Q8kQL6vMpuw9hDSw==
|
||||||
|
|
||||||
"@types/lodash@^4.14.157":
|
"@types/lodash@^4.14.158":
|
||||||
version "4.14.157"
|
version "4.14.158"
|
||||||
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.157.tgz#fdac1c52448861dfde1a2e1515dbc46e54926dc8"
|
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.158.tgz#b38ea8b6fe799acd076d7a8d7ab71c26ef77f785"
|
||||||
integrity sha512-Ft5BNFmv2pHDgxV5JDsndOWTRJ+56zte0ZpYLowp03tW+K+t8u8YMOzAnpuqPgzX6WO1XpDIUm7u04M8vdDiVQ==
|
integrity sha512-InCEXJNTv/59yO4VSfuvNrZHt7eeNtWQEgnieIA+mIC+MOWM9arOWG2eQ8Vhk6NbOre6/BidiXhkZYeDY9U35w==
|
||||||
|
|
||||||
"@types/lru-cache@^5.1.0":
|
"@types/lru-cache@^5.1.0":
|
||||||
version "5.1.0"
|
version "5.1.0"
|
||||||
|
|||||||
Reference in new issue
Block a user