]>
Commit | Line | Data |
---|---|---|
954605a8 | 1 | import * as express from 'express' |
3fd3ab2d | 2 | import 'express-validator' |
954605a8 | 3 | import { UserRight } from '../../shared' |
da854ddd | 4 | import { logger } from '../helpers/logger' |
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) { |
eec63bbc C |
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() | |
954605a8 C |
18 | } |
19 | ||
20 | return next() | |
21 | } | |
22 | } | |
23 | ||
24 | // --------------------------------------------------------------------------- | |
25 | ||
26 | export { | |
27 | ensureUserHasRight | |
28 | } |