Skip to content

Commit

Permalink
feat: add banned user privilege and enforce restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
luoingly committed Jan 26, 2025
1 parent 54ad640 commit 1a56fb7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const config = {
secretKey: String(process.env.secretKey),

privilege: {
Banned: 0,
PrimaryUser: 1,
Teacher: 2,
Root: 3,
Expand Down
9 changes: 5 additions & 4 deletions controllers/session.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const only = require('only')
const User = require('../models/User')
const { generatePwd } = require('../utils/helper')
const { privilege } = require('../config')

// 登录
const login = async (ctx) => {
Expand All @@ -11,12 +12,12 @@ const login = async (ctx) => {
.findOne({ uid })
.exec()

if (user == null) {
if (user == null)
ctx.throw(400, 'No such a user')
}
if (user.pwd !== pwd) {
if (user.pwd !== pwd)
ctx.throw(400, 'Wrong password')
}
if (user.privilege === privilege.Banned)
ctx.throw(403, 'Account banned')

ctx.session.profile = only(user, 'uid nick privilege pwd')
ctx.session.profile.verifyContest = []
Expand Down
12 changes: 12 additions & 0 deletions test/controllers/session.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const test = require('ava')
const supertest = require('supertest')
const app = require('../../app')
const users = require('../seed/users')
const config = require('../../config')

const server = app.listen()
const request = supertest.agent(server)

test('Bannded user login', async (t) => {
const res = await request
.post('/api/session')
.send({
uid: users.data.banned.uid,
pwd: users.data.banned.pwd,
})

t.is(res.status, 403)
})

test.before(async (t) => {
const res = await request
.post('/api/session')
Expand Down
2 changes: 1 addition & 1 deletion test/seed/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const users = {
nick: 'user',
pwd: 'testtest',
},
"hulllinda": { uid: "hulllinda", nick: "HULLLINDA", pwd: ")zD1d_mh)7" },
"banned": { uid: "banned", pwd: ")zD1d_mh)7", privilege: config.privilege.Banned },
"kevin63": { uid: "kevin63", pwd: "^I^+6XYfGV" },
"ugordon": { uid: "ugordon", nick: "UGORDON", pwd: "BwcTvXC%&8" },
"hallpatrick": { uid: "hallpatrick", nick: "HALLPATRICK", pwd: "(7#gZxV)5+" },
Expand Down
6 changes: 5 additions & 1 deletion utils/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { RateLimit } = require('koa2-ratelimit')
const { isAdmin, isRoot } = require('./helper')
const { privilege } = require('../config')
const User = require('../models/User')

const login = async (ctx, next) => {
Expand All @@ -8,7 +9,10 @@ const login = async (ctx, next) => {
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) {
if (user == null ||
user.pwd !== ctx.session.profile.pwd ||
user.privilege == privilege.Banned
) {
delete ctx.session.profile
ctx.throw(401, 'Login required')
}
Expand Down

0 comments on commit 1a56fb7

Please sign in to comment.