Skip to content

Commit

Permalink
fix: 修复 Session 以及用户权限相关问题 [Patch f33d87] (#78)
Browse files Browse the repository at this point in the history
* feat: Python 3 的评测支持

* feat: 弃用无状态 Token

* fix: 修复用户编辑相关问题

* fix: 失效时主动清除 Session
  • Loading branch information
luoingly authored Sep 30, 2024
1 parent 9012485 commit 211dfe9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion controllers/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const login = async (ctx) => {
ctx.throw(400, 'Wrong password')
}

ctx.session.profile = only(user, 'uid nick privilege')
ctx.session.profile = only(user, 'uid nick privilege pwd')
ctx.session.profile.verifyContest = []
ctx.body = {
profile: ctx.session.profile,
Expand Down
4 changes: 3 additions & 1 deletion controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ const update = async (ctx) => {
user[field] = opt[field]
}
})
if (!isUndefined(opt.privilege)) {
if (!isUndefined(opt.privilege) && opt.privilege !== user.privilege) {
if (!isRoot(ctx.session.profile))
ctx.throw(400, 'You do not have permission to change the privilege!')
user.privilege = Number.parseInt(opt.privilege)
}
if (opt.newPwd) {
Expand Down
2 changes: 1 addition & 1 deletion services/node-0/judger.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const logger = require('../../utils/logger')
const config = require('../../config')
const redis = require('../../config/redis')

const extensions = [ '', 'c', 'cpp', 'java' ]
const extensions = ['', 'c', 'cpp', 'java', 'py']

// 转化代码
// 因为判题端各数字表示的含义与 OJ 默认的不同,因此需要做一次转化
Expand Down
13 changes: 12 additions & 1 deletion utils/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
const { RateLimit } = require('koa2-ratelimit')
const { isAdmin, isRoot } = require('./helper')
const User = require('../models/User')

const login = async (ctx, next) => {
if (!ctx.session || ctx.session.profile == null) { ctx.throw(401, 'Login required') }
if (!ctx.session || ctx.session.profile == null) {
delete ctx.session.profile
ctx.throw(401, 'Login required')
}
const user = await User.findOne({ uid: ctx.session.profile.uid }).exec()
if (user == null || user.pwd !== ctx.session.profile.pwd) {
delete ctx.session.profile
ctx.throw(401, 'Login required')
}
if (user.privilege !== ctx.session.profile.privilege)
ctx.session.profile.privilege = user.privilege
await next()
}

Expand Down

0 comments on commit 211dfe9

Please sign in to comment.