]>
Commit | Line | Data |
---|---|---|
954605a8 | 1 | import * as express from 'express' |
954605a8 | 2 | import { UserRight } from '../../shared' |
da854ddd | 3 | import { logger } from '../helpers/logger' |
954605a8 C |
4 | |
5 | function ensureUserHasRight (userRight: UserRight) { | |
6 | return function (req: express.Request, res: express.Response, next: express.NextFunction) { | |
dae86118 | 7 | const user = res.locals.oauth.token.user |
954605a8 | 8 | if (user.hasRight(userRight) === false) { |
eec63bbc C |
9 | const message = `User ${user.username} does not have right ${UserRight[userRight]} to access to ${req.path}.` |
10 | logger.info(message) | |
11 | ||
12 | return res.status(403) | |
13 | .json({ | |
14 | error: message | |
15 | }) | |
16 | .end() | |
954605a8 C |
17 | } |
18 | ||
19 | return next() | |
20 | } | |
21 | } | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
25 | export { | |
26 | ensureUserHasRight | |
27 | } |