|
|
@ -130,10 +130,10 @@ app.get('/problem/:id', async (req, res) => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
if (!problem) throw 'No such problem.'; |
|
|
|
if (!problem) throw new ErrorMessage('无此题目。'); |
|
|
|
|
|
|
|
|
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) { |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) { |
|
|
|
throw 'Permission denied.'; |
|
|
|
throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
problem.allowedEdit = await problem.isAllowedEditBy(res.locals.user); |
|
|
|
problem.allowedEdit = await problem.isAllowedEditBy(res.locals.user); |
|
|
@ -141,7 +141,7 @@ app.get('/problem/:id', async (req, res) => { |
|
|
|
if (problem.is_public || problem.allowedEdit) { |
|
|
|
if (problem.is_public || problem.allowedEdit) { |
|
|
|
await syzoj.utils.markdown(problem, [ 'description', 'input_format', 'output_format', 'example', 'limit_and_hint' ]); |
|
|
|
await syzoj.utils.markdown(problem, [ 'description', 'input_format', 'output_format', 'example', 'limit_and_hint' ]); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
throw 'Permission denied'; |
|
|
|
throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let state = await problem.getJudgeState(res.locals.user, false); |
|
|
|
let state = await problem.getJudgeState(res.locals.user, false); |
|
|
@ -164,7 +164,7 @@ app.get('/problem/:id/export', async (req, res) => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
if (!problem || !problem.is_public) throw 'No such problem.'; |
|
|
|
if (!problem || !problem.is_public) throw new ErrorMessage('无此题目。'); |
|
|
|
|
|
|
|
|
|
|
|
let obj = { |
|
|
|
let obj = { |
|
|
|
title: problem.title, |
|
|
|
title: problem.title, |
|
|
@ -198,14 +198,14 @@ app.get('/problem/:id/edit', async (req, res) => { |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
|
|
|
|
|
|
|
|
if (!problem) { |
|
|
|
if (!problem) { |
|
|
|
if (!res.locals.user) throw 'Permission denied.'; |
|
|
|
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) }); |
|
|
|
problem = await Problem.create(); |
|
|
|
problem = await Problem.create(); |
|
|
|
problem.id = id; |
|
|
|
problem.id = id; |
|
|
|
problem.allowedEdit = true; |
|
|
|
problem.allowedEdit = true; |
|
|
|
problem.tags = []; |
|
|
|
problem.tags = []; |
|
|
|
problem.new = true; |
|
|
|
problem.new = true; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw 'Permission denied.'; |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
problem.allowedEdit = await problem.isAllowedEditBy(res.locals.user); |
|
|
|
problem.allowedEdit = await problem.isAllowedEditBy(res.locals.user); |
|
|
|
problem.tags = await problem.getTags(); |
|
|
|
problem.tags = await problem.getTags(); |
|
|
|
} |
|
|
|
} |
|
|
@ -226,23 +226,25 @@ app.post('/problem/:id/edit', async (req, res) => { |
|
|
|
let id = parseInt(req.params.id) || 0; |
|
|
|
let id = parseInt(req.params.id) || 0; |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
if (!problem) { |
|
|
|
if (!problem) { |
|
|
|
|
|
|
|
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) }); |
|
|
|
|
|
|
|
|
|
|
|
problem = await Problem.create(); |
|
|
|
problem = await Problem.create(); |
|
|
|
|
|
|
|
|
|
|
|
let customID = parseInt(req.body.id); |
|
|
|
let customID = parseInt(req.body.id); |
|
|
|
if (customID) { |
|
|
|
if (customID) { |
|
|
|
if (await Problem.fromID(customID)) throw 'The ID has been used.'; |
|
|
|
if (await Problem.fromID(customID)) throw new ErrorMessage('ID 已被使用。'); |
|
|
|
problem.id = customID; |
|
|
|
problem.id = customID; |
|
|
|
} else if (id) problem.id = id; |
|
|
|
} else if (id) problem.id = id; |
|
|
|
|
|
|
|
|
|
|
|
problem.user_id = res.locals.user.id; |
|
|
|
problem.user_id = res.locals.user.id; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw 'Permission denied.'; |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
if (!await problem.isAllowedEditBy(res.locals.user)) throw 'Permission denied.'; |
|
|
|
if (!await problem.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
|
|
|
|
|
|
|
|
if (res.locals.user.is_admin) { |
|
|
|
if (res.locals.user.is_admin) { |
|
|
|
let customID = parseInt(req.body.id); |
|
|
|
let customID = parseInt(req.body.id); |
|
|
|
if (customID && customID !== id) { |
|
|
|
if (customID && customID !== id) { |
|
|
|
if (await Problem.fromID(customID)) throw 'The ID has been used.'; |
|
|
|
if (await Problem.fromID(customID)) throw new ErrorMessage('ID 已被使用。'); |
|
|
|
await problem.changeID(customID); |
|
|
|
await problem.changeID(customID); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -280,14 +282,17 @@ app.get('/problem/:id/import', async (req, res) => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
let id = parseInt(req.params.id) || 0; |
|
|
|
let id = parseInt(req.params.id) || 0; |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
|
|
|
|
|
|
|
|
if (!problem) { |
|
|
|
if (!problem) { |
|
|
|
|
|
|
|
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) }); |
|
|
|
|
|
|
|
|
|
|
|
problem = await Problem.create(); |
|
|
|
problem = await Problem.create(); |
|
|
|
problem.id = id; |
|
|
|
problem.id = id; |
|
|
|
problem.new = true; |
|
|
|
problem.new = true; |
|
|
|
problem.user_id = res.locals.user.id; |
|
|
|
problem.user_id = res.locals.user.id; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw 'Permission denied.'; |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
if (!await problem.isAllowedEditBy(res.locals.user)) throw 'Permission denied.'; |
|
|
|
if (!await problem.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res.render('problem_import', { |
|
|
|
res.render('problem_import', { |
|
|
@ -306,18 +311,20 @@ app.post('/problem/:id/import', async (req, res) => { |
|
|
|
let id = parseInt(req.params.id) || 0; |
|
|
|
let id = parseInt(req.params.id) || 0; |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
if (!problem) { |
|
|
|
if (!problem) { |
|
|
|
|
|
|
|
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) }); |
|
|
|
|
|
|
|
|
|
|
|
problem = await Problem.create(); |
|
|
|
problem = await Problem.create(); |
|
|
|
|
|
|
|
|
|
|
|
let customID = parseInt(req.body.id); |
|
|
|
let customID = parseInt(req.body.id); |
|
|
|
if (customID) { |
|
|
|
if (customID) { |
|
|
|
if (await Problem.fromID(customID)) throw 'The ID has been used.'; |
|
|
|
if (await Problem.fromID(customID)) throw new ErrorMessage('ID 已被使用。'); |
|
|
|
problem.id = customID; |
|
|
|
problem.id = customID; |
|
|
|
} else if (id) problem.id = id; |
|
|
|
} else if (id) problem.id = id; |
|
|
|
|
|
|
|
|
|
|
|
problem.user_id = res.locals.user.id; |
|
|
|
problem.user_id = res.locals.user.id; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw 'Permission denied.'; |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
if (!await problem.isAllowedEditBy(res.locals.user)) throw 'Permission denied.'; |
|
|
|
if (!await problem.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let request = require('request-promise'); |
|
|
|
let request = require('request-promise'); |
|
|
@ -329,7 +336,7 @@ app.post('/problem/:id/import', async (req, res) => { |
|
|
|
json: true |
|
|
|
json: true |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (!json.success) throw `Failed to load problem: ${json.error}`; |
|
|
|
if (!json.success) throw new ErrorMessage('题目加载失败。', null, json.error); |
|
|
|
|
|
|
|
|
|
|
|
problem.title = json.obj.title; |
|
|
|
problem.title = json.obj.title; |
|
|
|
problem.description = json.obj.description; |
|
|
|
problem.description = json.obj.description; |
|
|
@ -344,7 +351,7 @@ app.post('/problem/:id/import', async (req, res) => { |
|
|
|
problem.file_io_output_name = json.obj.file_io_output_name; |
|
|
|
problem.file_io_output_name = json.obj.file_io_output_name; |
|
|
|
|
|
|
|
|
|
|
|
let validateMsg = await problem.validate(); |
|
|
|
let validateMsg = await problem.validate(); |
|
|
|
if (validateMsg) throw 'Invalid problem: ' + validateMsg; |
|
|
|
if (validateMsg) throw new ErrorMessage('无效的题目数据配置。', null, validateMsg); |
|
|
|
|
|
|
|
|
|
|
|
await problem.save(); |
|
|
|
await problem.save(); |
|
|
|
|
|
|
|
|
|
|
@ -378,8 +385,8 @@ app.get('/problem/:id/data', async (req, res) => { |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
|
|
|
|
|
|
|
|
if (!problem) throw 'No such problem'; |
|
|
|
if (!problem) throw new ErrorMessage('无此题目。'); |
|
|
|
if (!await problem.isAllowedEditBy(res.locals.user)) throw 'Permission denied'; |
|
|
|
if (!await problem.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
|
|
|
|
|
|
|
|
await problem.loadRelationships(); |
|
|
|
await problem.loadRelationships(); |
|
|
|
|
|
|
|
|
|
|
@ -399,8 +406,8 @@ app.post('/problem/:id/data', app.multer.single('testdata'), async (req, res) => |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
|
|
|
|
|
|
|
|
if (!problem) throw 'No such problem'; |
|
|
|
if (!problem) throw new ErrorMessage('无此题目。'); |
|
|
|
if (!await problem.isAllowedEditBy(res.locals.user)) throw 'Permission denied'; |
|
|
|
if (!await problem.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
|
|
|
|
|
|
|
|
await problem.loadRelationships(); |
|
|
|
await problem.loadRelationships(); |
|
|
|
|
|
|
|
|
|
|
@ -411,7 +418,7 @@ app.post('/problem/:id/data', app.multer.single('testdata'), async (req, res) => |
|
|
|
problem.file_io_output_name = req.body.file_io_output_name; |
|
|
|
problem.file_io_output_name = req.body.file_io_output_name; |
|
|
|
|
|
|
|
|
|
|
|
let validateMsg = await problem.validate(); |
|
|
|
let validateMsg = await problem.validate(); |
|
|
|
if (validateMsg) throw 'Invalid problem: ' + validateMsg; |
|
|
|
if (validateMsg) throw new ErrorMessage('无效的题目数据配置。', null, validateMsg); |
|
|
|
|
|
|
|
|
|
|
|
if (req.file) { |
|
|
|
if (req.file) { |
|
|
|
await problem.updateTestdata(req.file.path); |
|
|
|
await problem.updateTestdata(req.file.path); |
|
|
@ -428,14 +435,44 @@ app.post('/problem/:id/data', app.multer.single('testdata'), async (req, res) => |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set problem public
|
|
|
|
|
|
|
|
async function setPublic(req, res, is_public) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
let id = parseInt(req.params.id); |
|
|
|
|
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
|
|
|
|
if (!problem) throw new ErrorMessage('无此题目。'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let allowedEdit = await problem.isAllowedEditBy(res.locals.user); |
|
|
|
|
|
|
|
if (!allowedEdit) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
problem.is_public = is_public; |
|
|
|
|
|
|
|
await problem.save(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.redirect(syzoj.utils.makeUrl(['problem', id])); |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
syzoj.log(e); |
|
|
|
|
|
|
|
res.render('error', { |
|
|
|
|
|
|
|
err: e |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.get('/problem/:id/public', async (req, res) => { |
|
|
|
|
|
|
|
await setPublic(req, res, true); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.get('/problem/:id/dis_public', async (req, res) => { |
|
|
|
|
|
|
|
await setPublic(req, res, false); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
app.post('/problem/:id/submit', async (req, res) => { |
|
|
|
app.post('/problem/:id/submit', async (req, res) => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
|
|
|
|
|
|
|
|
if (!problem) throw 'No such problem.'; |
|
|
|
if (!problem) throw new ErrorMessage('无此题目。'); |
|
|
|
if (!syzoj.config.languages[req.body.language]) throw 'No such language.' |
|
|
|
if (!syzoj.config.languages[req.body.language]) throw new ErrorMessage('不支持该语言。'); |
|
|
|
if (!res.locals.user) throw 'Please login.'; |
|
|
|
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) }); |
|
|
|
|
|
|
|
|
|
|
|
let judge_state = await JudgeState.create({ |
|
|
|
let judge_state = await JudgeState.create({ |
|
|
|
code: req.body.code, |
|
|
|
code: req.body.code, |
|
|
@ -447,16 +484,16 @@ app.post('/problem/:id/submit', async (req, res) => { |
|
|
|
let contest_id = parseInt(req.query.contest_id); |
|
|
|
let contest_id = parseInt(req.query.contest_id); |
|
|
|
if (contest_id) { |
|
|
|
if (contest_id) { |
|
|
|
let contest = await Contest.fromID(contest_id); |
|
|
|
let contest = await Contest.fromID(contest_id); |
|
|
|
if (!contest) throw 'No such contest.'; |
|
|
|
if (!contest) throw new ErrorMessage('无此比赛。'); |
|
|
|
let problems_id = await contest.getProblems(); |
|
|
|
let problems_id = await contest.getProblems(); |
|
|
|
if (!problems_id.includes(id)) throw 'No such problem.'; |
|
|
|
if (!problems_id.includes(id)) throw new ErrorMessage('无此题目。'); |
|
|
|
|
|
|
|
|
|
|
|
judge_state.type = 1; |
|
|
|
judge_state.type = 1; |
|
|
|
judge_state.type_info = contest_id; |
|
|
|
judge_state.type_info = contest_id; |
|
|
|
|
|
|
|
|
|
|
|
await judge_state.save(); |
|
|
|
await judge_state.save(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw 'Permission denied.'; |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
judge_state.type = problem.is_public ? 0 : 2; |
|
|
|
judge_state.type = problem.is_public ? 0 : 2; |
|
|
|
await judge_state.save(); |
|
|
|
await judge_state.save(); |
|
|
|
} |
|
|
|
} |
|
|
@ -486,12 +523,12 @@ app.get('/problem/:id/download', async (req, res) => { |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
|
|
|
|
|
|
|
|
if (!problem) throw 'No such problem'; |
|
|
|
if (!problem) throw new ErrorMessage('无此题目。'); |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw 'Permission denied'; |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
|
|
|
|
|
|
|
|
await problem.loadRelationships(); |
|
|
|
await problem.loadRelationships(); |
|
|
|
|
|
|
|
|
|
|
|
if (!problem.testdata) throw 'No testdata'; |
|
|
|
if (!problem.testdata) throw new ErrorMessage('无测试数据。'); |
|
|
|
|
|
|
|
|
|
|
|
res.download(problem.testdata.getPath(), `testdata_${id}.zip`); |
|
|
|
res.download(problem.testdata.getPath(), `testdata_${id}.zip`); |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
@ -508,11 +545,11 @@ app.get('/problem/:id/statistics/:type', async (req, res) => { |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let id = parseInt(req.params.id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
let problem = await Problem.fromID(id); |
|
|
|
|
|
|
|
|
|
|
|
if (!problem) throw 'No such problem'; |
|
|
|
if (!problem) throw new ErrorMessage('无此题目。'); |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw 'Permission denied'; |
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); |
|
|
|
|
|
|
|
|
|
|
|
let count = await problem.countStatistics(req.params.type); |
|
|
|
let count = await problem.countStatistics(req.params.type); |
|
|
|
if (count === null) throw 'No such type'; |
|
|
|
if (count === null) throw new ErrorMessage('无此统计类型。'); |
|
|
|
|
|
|
|
|
|
|
|
let paginate = syzoj.utils.paginate(count, req.query.page, syzoj.config.page.problem_statistics); |
|
|
|
let paginate = syzoj.utils.paginate(count, req.query.page, syzoj.config.page.problem_statistics); |
|
|
|
let statistics = await problem.getStatistics(req.params.type, paginate); |
|
|
|
let statistics = await problem.getStatistics(req.params.type, paginate); |
|
|
|