core: use common plugin-api interface

This commit is contained in:
undefined committed 2022-09-30 19:02:40 +08:00
1 parent c61c875bee
commit 70a96aecac
54 files changed
+387 -566

No files matched your search

-27
View File
@@ -1,27 +0,0 @@
import superagent from 'superagent';
import { ForbiddenError, ValidationError } from 'hydrooj/src/error';
import { PRIV } from 'hydrooj/src/model/builtin';
import * as system from 'hydrooj/src/model/system';
import * as bus from 'hydrooj/src/service/bus';
bus.on('handler/before/UserRegister', async (thisArg) => {
if (!system.get('recaptcha.key')) return;
if (thisArg.request.method !== 'post') {
thisArg.UiContext.recaptchaKey = system.get('recaptcha.key');
return;
}
if (thisArg.user.hasPriv(PRIV.PRIV_UNLIMITED_ACCESS)) return;
if (!thisArg.args.captcha) throw new ValidationError('captcha');
const response = await superagent.post('https://recaptcha.net/recaptcha/api/siteverify')
.field('secret', system.get('recaptcha.secret'))
.field('response', thisArg.args.captcha)
.field('remoteip', thisArg.request.ip);
if (!response.body.success) throw new ForbiddenError('captcha fail');
});
bus.on('handler/after/UserRegister', async (thisArg) => {
thisArg.response.body.captcha = `\
<script src="https://recaptcha.net/recaptcha/api.js?render=${system.get('recaptcha.key')}"></script>
<input type="text" name="captcha" id="_captcha" style="display:none">
<input type="submit" id="_submit" style="display:none">`;
});
+27
View File
@@ -0,0 +1,27 @@
import {
Context, ForbiddenError, PRIV, superagent, SystemModel, ValidationError,
} from 'hydrooj';
export function apply(ctx: Context) {
ctx.on('handler/before/UserRegister', async (thisArg) => {
if (!SystemModel.get('recaptcha.key')) return;
if (thisArg.request.method !== 'post') {
thisArg.UiContext.recaptchaKey = SystemModel.get('recaptcha.key');
return;
}
if (thisArg.user.hasPriv(PRIV.PRIV_UNLIMITED_ACCESS)) return;
if (!thisArg.args.captcha) throw new ValidationError('captcha');
const response = await superagent.post('https://recaptcha.net/recaptcha/api/siteverify')
.field('secret', SystemModel.get('recaptcha.secret'))
.field('response', thisArg.args.captcha)
.field('remoteip', thisArg.request.ip);
if (!response.body.success) throw new ForbiddenError('captcha fail');
});
ctx.on('handler/after/UserRegister', async (thisArg) => {
thisArg.response.body.captcha = `\
<script src="https://recaptcha.net/recaptcha/api.js?render=${SystemModel.get('recaptcha.key')}"></script>
<input type="text" name="captcha" id="_captcha" style="display:none">
<input type="submit" id="_submit" style="display:none">`;
});
}
+1 -4
View File
@@ -6,8 +6,5 @@
"repository": "https://github.com/hydro-dev/Hydro.git",
"author": "undefined",
"license": "AGPL-3.0-or-later",
"preferUnplugged": true,
"dependencies": {
"superagent": "^8.0.0"
}
"preferUnplugged": true
}