mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: use common plugin-api interface
This commit is contained in:
1 parent
c61c875bee
commit
70a96aecac
54 files changed
+387
-566
No files matched your search
@@ -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">`;
|
||||
});
|
||||
@@ -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">`;
|
||||
});
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user