]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/user-right.ts
Translated using Weblate (Vietnamese)
[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.status(HttpStatusCode.FORBIDDEN_403)
14 .json({ error: message })
15 }
16
17 return next()
18 }
19 }
20
21 // ---------------------------------------------------------------------------
22
23 export {
24 ensureUserHasRight
25 }