-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#520 ban 여부에 따른 서비스 이용 제한 #530
The head ref may contain hidden characters: "#520-ban-\uC5EC\uBD80\uC5D0-\uB530\uB978-\uC11C\uBE44\uC2A4-\uC774\uC6A9-\uC81C\uD55C"
Changes from 14 commits
b679c03
5f4a9b9
89cc169
2c280c8
1f52f5c
6bb6fe0
f6c49ae
c381eff
a8c35ef
56bd968
7eceae0
eecc5f6
ab69625
b341a33
074d357
8da0c4a
0729f66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const { validateServiceBanRecord } = require("../modules/ban"); | ||
|
||
const banMiddleware = async (req, res, next) => { | ||
console.log(`req.originalUrl: ${req.originalUrl}`); | ||
const serviceMapper = { | ||
"/rooms/create": "service", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 상황에는 Object 대신 Map을 쓰는게 좋아요! |
||
"/rooms/join": "service", | ||
}; | ||
const banErrorMessage = await validateServiceBanRecord( | ||
req, | ||
serviceMapper[req.originalUrl] | ||
); | ||
if (banErrorMessage !== undefined) { | ||
console.log("banned user"); | ||
return res.status(400).json({ error: banErrorMessage }); | ||
} | ||
console.log("next()"); | ||
next(); | ||
}; | ||
|
||
module.exports = banMiddleware; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const logger = require("./logger"); | ||
const { banModel } = require("./stores/mongo"); | ||
|
||
/** | ||
* | ||
* @param {*} req | ||
* @param {String} service | ||
*/ | ||
const validateServiceBanRecord = async (req, service) => { | ||
let banRecord = undefined; | ||
|
||
try { | ||
// 현재 시각이 expireAt 보다 작고, 본인인 경우(ban의 userId가 userId랑 같은 경우) 중 serviceName이 "service"인 record를 모두 가져옴 | ||
const bans = await banModel | ||
.find({ | ||
userSid: req.session.loginInfo.sid, | ||
expireAt: { | ||
$gte: req.timestamp, | ||
}, | ||
serviceName: service, | ||
}) | ||
.sort({ expireAt: -1 }); | ||
if (bans.length > 0) { | ||
// 가장 expireAt이 큰 정지 기록만 반환함. | ||
banRecord = bans[0]; | ||
} | ||
} catch (err) { | ||
logger.error( | ||
"Error occured while getValidServiceBanRecord: " + err.message | ||
); | ||
return; | ||
} | ||
if (banRecord !== undefined) { | ||
const formattedExpireAt = banRecord.expireAt | ||
.toISOString() | ||
.replace("T", " ") | ||
.split(".")[0]; | ||
const banErrorMessage = `${req.originalUrl} : user ${req.userId} (${req.session.loginInfo.sid}) is temporarily restricted from service until ${formattedExpireAt}.`; | ||
return banErrorMessage; | ||
} | ||
return; | ||
}; | ||
|
||
module.exports = { | ||
validateServiceBanRecord, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,9 @@ router.get( | |
roomHandlers.infoHandler | ||
); | ||
|
||
// 방 생성/참여전 ban 여부 확인 | ||
router.use(require("../middlewares/ban")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. auth middleware 바로 아래로 올리는 것으로 논의 완료 |
||
|
||
// JSON으로 받은 정보로 방을 생성한다. | ||
router.post( | ||
"/create", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저희 로그 남길 땐
logger
사용해야 합니다. 그리고 특이사항이 있는게 아니라면 이 내용은 로깅 안하는게 더 좋을 것 같아요!