]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/user-right.ts
refactor API errors to standard error format
[github/Chocobozzz/PeerTube.git] / server / middlewares / user-right.ts
1 import * as express from 'express'
2 import { UserRight } from '../../shared'
3 import { logger } from '../helpers/logger'
4 import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
5
6 function ensureUserHasRight (userRight: UserRight) {
7 return function (req: express.Request, res: express.Response, next: express.NextFunction) {
8 const user = res.locals.oauth.token.user
9 if (user.hasRight(userRight) === false) {
10 const message = `User ${user.username} does not have right ${userRight} to access to ${req.path}.`
11 logger.info(message)
12
13 return res.fail({
14 status: HttpStatusCode.FORBIDDEN_403,
15 message
16 })
17 }
18
19 return next()
20 }
21 }
22
23 // ---------------------------------------------------------------------------
24
25 export {
26 ensureUserHasRight
27 }