]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/user-right.ts
replace numbers with typed http status codes (#3409)
[github/Chocobozzz/PeerTube.git] / server / middlewares / user-right.ts
CommitLineData
954605a8 1import * as express from 'express'
954605a8 2import { UserRight } from '../../shared'
da854ddd 3import { logger } from '../helpers/logger'
2d53be02 4import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
954605a8
C
5
6function ensureUserHasRight (userRight: UserRight) {
7 return function (req: express.Request, res: express.Response, next: express.NextFunction) {
dae86118 8 const user = res.locals.oauth.token.user
954605a8 9 if (user.hasRight(userRight) === false) {
bd45d503 10 const message = `User ${user.username} does not have right ${userRight} to access to ${req.path}.`
eec63bbc
C
11 logger.info(message)
12
2d53be02
RK
13 return res.status(HttpStatusCode.FORBIDDEN_403)
14 .json({ error: message })
954605a8
C
15 }
16
17 return next()
18 }
19}
20
21// ---------------------------------------------------------------------------
22
23export {
24 ensureUserHasRight
25}