user-right.ts 769 B

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