]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { UserRight } from '../../shared' | |
3 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' | |
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} to access to ${req.path}.` | |
11 | logger.info(message) | |
12 | ||
13 | return res.fail({ | |
14 | status: HttpStatusCode.FORBIDDEN_403, | |
15 | message | |
16 | }) | |
17 | } | |
18 | ||
19 | return next() | |
20 | } | |
21 | } | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
25 | export { | |
26 | ensureUserHasRight | |
27 | } |