]>
Commit | Line | Data |
---|---|---|
954605a8 | 1 | import * as express from 'express' |
954605a8 | 2 | import { UserRight } from '../../shared' |
da854ddd | 3 | import { logger } from '../helpers/logger' |
2d53be02 | 4 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' |
954605a8 C |
5 | |
6 | function 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 | ||
23 | export { | |
24 | ensureUserHasRight | |
25 | } |