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/packages/hydrooj/src/handler/user.ts

276 lines
10 KiB
TypeScript

import moment from 'moment-timezone';
import {
UserAlreadyExistError, InvalidTokenError, VerifyPasswordError,
UserNotFoundError, SystemError, BlacklistedError,
UserFacingError,
} from '../error';
import {
Route, Handler, Types, param,
} from '../service/server';
import * as user from '../model/user';
import * as token from '../model/token';
import * as record from '../model/record';
import * as problem from '../model/problem';
import * as task from '../model/task';
import * as system from '../model/system';
import { PERM, PRIV } from '../model/builtin';
import { isEmail, isPassword, isUname } from '../lib/validator';
import { sendMail } from '../lib/mail';
import * as misc from '../lib/misc';
import paginate from '../lib/paginate';
4 years ago
import { User } from '../interface';
class UserLoginHandler extends Handler {
async get() {
this.response.template = 'user_login.html';
}
5 years ago
@param('uname', Types.String)
@param('password', Types.String)
4 years ago
@param('rememberme', Types.Boolean)
async post(domainId: string, uname: string, password: string, rememberme = false) {
const udoc = await user.getByUname(domainId, uname);
if (!udoc) throw new UserNotFoundError(uname);
udoc.checkPassword(password);
await user.setById(udoc._id, { loginat: new Date(), loginip: this.request.ip });
if (udoc.priv === PRIV.PRIV_NONE) throw new BlacklistedError(uname);
this.session.uid = udoc._id;
4 years ago
this.session.save = rememberme;
this.response.redirect = this.request.referer.endsWith('/login') ? '/' : this.request.referer;
}
}
class UserLogoutHandler extends Handler {
async get() {
this.response.template = 'user_logout.html';
}
5 years ago
async post() {
4 years ago
this.session.uid = 0;
}
}
class UserRegisterHandler extends Handler {
async get() {
this.response.template = 'user_register.html';
}
5 years ago
@param('mail', Types.String, isEmail)
async post(domainId: string, mail: string) {
if (await user.getByEmail('system', mail)) throw new UserAlreadyExistError(mail);
this.limitRate('send_mail', 3600, 30);
5 years ago
const t = await token.add(
token.TYPE_REGISTRATION,
5 years ago
await system.get('registration_token_expire_seconds'),
5 years ago
{ mail },
);
5 years ago
if (await system.get('smtp.user')) {
const m = await this.renderHTML('user_register_mail.html', {
path: `register/${t[0]}`,
url_prefix: await system.get('server.url'),
});
await sendMail(mail, 'Sign Up', 'user_register_mail', m);
this.response.template = 'user_register_mail_sent.html';
} else {
4 years ago
this.response.redirect = this.url('user_register_with_code', { code: t[0] });
}
}
}
class UserRegisterWithCodeHandler extends Handler {
@param('code', Types.String)
async get(domainId: string, code: string) {
this.response.template = 'user_register_with_code.html';
5 years ago
const { mail } = await token.get(code, token.TYPE_REGISTRATION);
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
this.response.body = { mail };
}
5 years ago
@param('password', Types.String, isPassword)
@param('verifyPassword', Types.String)
@param('uname', Types.String, isUname)
@param('code', Types.String)
async post(
domainId: string, password: string, verify: string,
uname: string, code: string,
) {
5 years ago
const { mail } = await token.get(code, token.TYPE_REGISTRATION);
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
if (password !== verify) throw new VerifyPasswordError();
const uid = await user.create(mail, uname, password, undefined, this.request.ip);
4 years ago
await token.del(code, token.TYPE_REGISTRATION);
this.session.uid = uid;
4 years ago
this.response.redirect = this.url('homepage');
}
}
class UserLostPassHandler extends Handler {
async get() {
5 years ago
if (!await system.get('smtp.user')) throw new SystemError('Cannot send mail');
this.response.template = 'user_lostpass.html';
}
5 years ago
@param('mail', Types.String, isEmail)
async post(domainId: string, mail: string) {
5 years ago
if (!await system.get('smtp.user')) throw new SystemError('Cannot send mail');
const udoc = await user.getByEmail('system', mail);
if (!udoc) throw new UserNotFoundError(mail);
const [tid] = await token.add(
token.TYPE_LOSTPASS,
5 years ago
await system.get('lostpass_token_expire_seconds'),
5 years ago
{ uid: udoc._id },
);
const m = await this.renderHTML('user_lostpass_mail', { url: `lostpass/${tid}`, uname: udoc.uname });
await sendMail(mail, 'Lost Password', 'user_lostpass_mail', m);
this.response.template = 'user_lostpass_mail_sent.html';
}
}
class UserLostPassWithCodeHandler extends Handler {
async get({ domainId, code }) {
5 years ago
const tdoc = await token.get(code, token.TYPE_LOSTPASS);
if (!tdoc) throw new InvalidTokenError(token.TYPE_LOSTPASS, code);
const udoc = await user.getById(domainId, tdoc.uid);
this.response.body = { uname: udoc.uname };
}
5 years ago
@param('code', Types.String)
@param('password', Types.String, isPassword)
@param('verifyPassword', Types.String)
async post(domainId: string, code: string, password: string, verifyPassword: string) {
5 years ago
const tdoc = await token.get(code, token.TYPE_LOSTPASS);
if (!tdoc) throw new InvalidTokenError(token.TYPE_LOSTPASS, code);
5 years ago
if (password !== verifyPassword) throw new VerifyPasswordError();
5 years ago
await user.setPassword(tdoc.uid, password);
4 years ago
await token.del(code, token.TYPE_LOSTPASS);
4 years ago
this.response.redirect = this.url('homepage');
}
}
class UserDetailHandler extends Handler {
@param('uid', Types.Int)
async get(domainId: string, uid: number) {
5 years ago
const isSelfProfile = this.user._id === uid;
const udoc = await user.getById(domainId, uid);
if (!udoc) throw new UserNotFoundError(uid);
const [sdoc, rdocs, [pdocs, pcount]] = await Promise.all([
token.getMostRecentSessionByUid(uid),
record.getByUid(domainId, uid, 30),
paginate(
problem.getMulti(domainId, { owner: this.user._id }),
1,
100,
),
]);
4 years ago
const pdict = await problem.getList(
domainId, rdocs.map((rdoc) => rdoc.pid),
this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN), false,
4 years ago
);
// Remove sensitive data
if (!isSelfProfile && sdoc) {
sdoc.createIp = '';
sdoc.updateIp = '';
sdoc._id = '';
}
const path = [
['Hydro', 'homepage'],
['user_detail', 'user_detail', { uid }],
];
5 years ago
this.response.template = 'user_detail.html';
this.response.body = {
isSelfProfile, udoc, sdoc, rdocs, pdocs, pcount, pdict, path,
};
}
}
class UserDeleteHandler extends Handler {
async post({ password }) {
this.user.checkPassword(password);
const tid = await task.add({
executeAfter: moment().add(7, 'days').toDate(),
type: 'script',
id: 'deleteUser',
args: { uid: this.user._id },
});
await user.setById(this.user._id, { del: tid });
this.response.template = 'user_delete_pending.html';
}
}
class UserSearchHandler extends Handler {
@param('q', Types.String)
4 years ago
@param('exectMatch', Types.Boolean)
async get(domainId: string, q: string, exactMatch = false) {
let udoc = await user.getById(domainId, parseInt(q, 10));
4 years ago
const udocs: User[] = udoc ? [udoc] : [];
udoc = await user.getByUname(domainId, q);
if (udoc) udocs.push(udoc);
udoc = await user.getByEmail(domainId, q);
if (udoc) udocs.push(udoc);
4 years ago
if (!exactMatch) udocs.push(...await user.getPrefixList(domainId, q, 20));
5 years ago
for (const i in udocs) {
udocs[i].gravatar = misc.gravatar(udocs[i].gravatar || '');
5 years ago
}
this.response.body = udocs;
}
}
class OauthHandler extends Handler {
@param('type', Types.String)
async get(domainId: string, type: string) {
if (global.Hydro.lib[`oauth_${type}`]) await global.Hydro.lib[`oauth_${type}`].get.call(this);
}
}
class OauthCallbackHandler extends Handler {
4 years ago
async get(args: any) {
4 years ago
let r;
if (global.Hydro.lib[`oauth_${args.type}`]) r = await global.Hydro.lib[`oauth_${args.type}`].callback(args);
4 years ago
else throw new UserFacingError('Oauth type');
const udoc = await user.getByEmail('system', r.email);
4 years ago
if (udoc) {
this.session.uid = udoc._id;
} else {
this.checkPriv(PRIV.PRIV_REGISTER_USER);
4 years ago
let username = '';
r.uname = r.uname || [];
r.uname.push(String.random(16));
for (const uname of r.uname) {
// eslint-disable-next-line no-await-in-loop
const nudoc = await user.getByUname('system', uname);
4 years ago
if (!nudoc) {
username = uname;
break;
}
}
const uid = await user.create(
r.email, username, String.random(32),
undefined, this.request.ip,
);
const $set: any = {
4 years ago
oauth: args.type,
};
if (r.bio) $set.bio = r.bio;
if (r.viewLang) $set.viewLang = r.viewLang;
await user.setById(uid, $set);
this.session.uid = uid;
}
}
}
export async function apply() {
Route('user_login', '/login', UserLoginHandler);
Route('user_oauth', '/oauth/:type', OauthHandler);
Route('user_oauth_callback', '/oauth/:type/callback', OauthCallbackHandler);
Route('user_register', '/register', UserRegisterHandler, PRIV.PRIV_REGISTER_USER);
Route('user_register_with_code', '/register/:code', UserRegisterWithCodeHandler, PRIV.PRIV_REGISTER_USER);
Route('user_logout', '/logout', UserLogoutHandler, PRIV.PRIV_USER_PROFILE);
Route('user_lostpass', '/lostpass', UserLostPassHandler);
Route('user_lostpass_with_code', '/lostpass/:code', UserLostPassWithCodeHandler);
4 years ago
Route('user_search', '/user/search', UserSearchHandler, PRIV.PRIV_USER_PROFILE);
Route('user_delete', '/user/delete', UserDeleteHandler, PRIV.PRIV_USER_PROFILE);
Route('user_detail', '/user/:uid', UserDetailHandler);
5 years ago
}
global.Hydro.handler.user = apply;