diff --git a/models/contest.js b/models/contest.js index 9365f70..47e5b92 100644 --- a/models/contest.js +++ b/models/contest.js @@ -46,6 +46,7 @@ let model = db.define('contest', { information: { type: Sequelize.TEXT }, problems: { type: Sequelize.TEXT }, + admins: { type: Sequelize.TEXT }, ranklist_id: { type: Sequelize.INTEGER, @@ -57,17 +58,17 @@ let model = db.define('contest', { is_public: { type: Sequelize.BOOLEAN } }, { - timestamps: false, - tableName: 'contest', - indexes: [ - { - fields: ['holder_id'], - }, - { - fields: ['ranklist_id'], - } - ] -}); + timestamps: false, + tableName: 'contest', + indexes: [ + { + fields: ['holder_id'], + }, + { + fields: ['ranklist_id'], + } + ] + }); let Model = require('./common'); class Contest extends Model { @@ -76,6 +77,7 @@ class Contest extends Model { title: '', subtitle: '', problems: '', + admins: '', information: '', type: 'noi', start_time: 0, @@ -92,7 +94,7 @@ class Contest extends Model { } async isSupervisior(user) { - return user && (user.is_admin || this.holder_id === user.id); + return user && (user.is_admin || this.holder_id === user.id || this.admins.split('|').includes(user.id.toString())); } allowedSeeingOthers() { diff --git a/modules/api_v2.js b/modules/api_v2.js index f79d5af..3dd25de 100644 --- a/modules/api_v2.js +++ b/modules/api_v2.js @@ -19,6 +19,37 @@ 'use strict'; +app.get('/api/v2/search/users/:keyword*?', async (req, res) => { + try { + let User = syzoj.model('user'); + + let keyword = req.params.keyword || ''; + let conditions = []; + const uid = parseInt(keyword); + if (uid != null && !isNaN(uid)) { + conditions.push({ id: uid }); + } + if (keyword != null && String(keyword).length >= 2) { + conditions.push({ username: { like: `%${req.params.keyword}%` } }); + } + if (conditions.length === 0) { + res.send({ success: true, results: [] }); + } else { + let users = await User.query(null, { + $or: conditions + }, [['username', 'asc']]); + + let result = []; + + result = users.map(x => ({ name: `${x.username}`, value: x.id, url: syzoj.utils.makeUrl(['user', x.id]) })); + res.send({ success: true, results: result }); + } + } catch (e) { + syzoj.log(e); + res.send({ success: false }); + } +}); + app.get('/api/v2/search/problems/:keyword*?', async (req, res) => { try { let Problem = syzoj.model('problem'); diff --git a/modules/contest.js b/modules/contest.js index 0a210f1..3951b43 100644 --- a/modules/contest.js +++ b/modules/contest.js @@ -63,12 +63,14 @@ app.get('/contest/:id/edit', async (req, res) => { contest.id = 0; } - let problems = []; + let problems = [], admins = []; if (contest.problems) problems = await contest.problems.split('|').mapAsync(async id => await Problem.fromID(id)); + if (contest.admins) admins = await contest.admins.split('|').mapAsync(async id => await User.fromID(id)); res.render('contest_edit', { contest: contest, - problems: problems + problems: problems, + admins: admins }); } catch (e) { syzoj.log(e); @@ -103,6 +105,7 @@ app.post('/contest/:id/edit', async (req, res) => { contest.subtitle = req.body.subtitle; if (!Array.isArray(req.body.problems)) req.body.problems = [req.body.problems]; contest.problems = req.body.problems.join('|'); + contest.admins = req.body.admins.join('|'); contest.information = req.body.information; contest.start_time = syzoj.utils.parseDate(req.body.start_time); contest.end_time = syzoj.utils.parseDate(req.body.end_time); @@ -223,9 +226,13 @@ app.get('/contest/:id/ranklist', async (req, res) => { try { let contest_id = parseInt(req.params.id); let contest = await Contest.fromID(contest_id); + const curUser = res.locals.user; if (!contest) throw new ErrorMessage('无此比赛。'); - if (!await contest.isAllowedSeeResultBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); + if ([contest.allowedSeeingResult() && contest.allowedSeeingOthers(), + contest.isEnded(), + contest.isSupervisior(curUser)].every(x => !x)) + throw new ErrorMessage('您没有权限进行此操作。'); await contest.loadRelationships(); diff --git a/views/contest.ejs b/views/contest.ejs index feaedc6..20176df 100644 --- a/views/contest.ejs +++ b/views/contest.ejs @@ -71,7 +71,7 @@ <% if (problem.judge_id) { %> - + <% if (typeof problem.status === 'string') { %> diff --git a/views/contest_edit.ejs b/views/contest_edit.ejs index e1cd562..31b4ed9 100644 --- a/views/contest_edit.ejs +++ b/views/contest_edit.ejs @@ -18,6 +18,14 @@ <% } %> +
+ + +
@@ -62,6 +70,21 @@