]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/user-right.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / user-right.ts
1 import * as express from 'express'
2 import 'express-validator'
3 import { UserRight } from '../../shared'
4 import { logger } from '../helpers/logger'
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[userRight]} to access to ${req.path}.`
11 logger.info(message)
12
13 return res.status(403)
14 .json({
15 error: message
16 })
17 .end()
18 }
19
20 return next()
21 }
22 }
23
24 // ---------------------------------------------------------------------------
25
26 export {
27 ensureUserHasRight
28 }