AuthorizationMiddlewear.coffee 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. AuthorizationManager = require("./AuthorizationManager")
  2. async = require "async"
  3. logger = require "logger-sharelatex"
  4. ObjectId = require("mongojs").ObjectId
  5. Errors = require "../Errors/Errors"
  6. AuthenticationController = require "../Authentication/AuthenticationController"
  7. module.exports = AuthorizationMiddlewear =
  8. ensureUserCanReadMultipleProjects: (req, res, next) ->
  9. project_ids = (req.query.project_ids or "").split(",")
  10. AuthorizationMiddlewear._getUserId req, (error, user_id) ->
  11. return next(error) if error?
  12. # Remove the projects we have access to. Note rejectSeries doesn't use
  13. # errors in callbacks
  14. async.rejectSeries project_ids, (project_id, cb) ->
  15. AuthorizationManager.canUserReadProject user_id, project_id, (error, canRead) ->
  16. return next(error) if error?
  17. cb(canRead)
  18. , (unauthorized_project_ids) ->
  19. if unauthorized_project_ids.length > 0
  20. AuthorizationMiddlewear.redirectToRestricted req, res, next
  21. else
  22. next()
  23. ensureUserCanReadProject: (req, res, next) ->
  24. AuthorizationMiddlewear._getUserAndProjectId req, (error, user_id, project_id) ->
  25. return next(error) if error?
  26. AuthorizationManager.canUserReadProject user_id, project_id, (error, canRead) ->
  27. return next(error) if error?
  28. if canRead
  29. logger.log {user_id, project_id}, "allowing user read access to project"
  30. next()
  31. else
  32. logger.log {user_id, project_id}, "denying user read access to project"
  33. AuthorizationMiddlewear.redirectToRestricted req, res, next
  34. ensureUserCanWriteProjectSettings: (req, res, next) ->
  35. AuthorizationMiddlewear._getUserAndProjectId req, (error, user_id, project_id) ->
  36. return next(error) if error?
  37. AuthorizationManager.canUserWriteProjectSettings user_id, project_id, (error, canWrite) ->
  38. return next(error) if error?
  39. if canWrite
  40. logger.log {user_id, project_id}, "allowing user write access to project settings"
  41. next()
  42. else
  43. logger.log {user_id, project_id}, "denying user write access to project settings"
  44. AuthorizationMiddlewear.redirectToRestricted req, res, next
  45. ensureUserCanWriteProjectContent: (req, res, next) ->
  46. AuthorizationMiddlewear._getUserAndProjectId req, (error, user_id, project_id) ->
  47. return next(error) if error?
  48. AuthorizationManager.canUserWriteProjectContent user_id, project_id, (error, canWrite) ->
  49. return next(error) if error?
  50. if canWrite
  51. logger.log {user_id, project_id}, "allowing user write access to project content"
  52. next()
  53. else
  54. logger.log {user_id, project_id}, "denying user write access to project settings"
  55. AuthorizationMiddlewear.redirectToRestricted req, res, next
  56. ensureUserCanAdminProject: (req, res, next) ->
  57. AuthorizationMiddlewear._getUserAndProjectId req, (error, user_id, project_id) ->
  58. return next(error) if error?
  59. AuthorizationManager.canUserAdminProject user_id, project_id, (error, canAdmin) ->
  60. return next(error) if error?
  61. if canAdmin
  62. logger.log {user_id, project_id}, "allowing user admin access to project"
  63. next()
  64. else
  65. logger.log {user_id, project_id}, "denying user admin access to project"
  66. AuthorizationMiddlewear.redirectToRestricted req, res, next
  67. ensureUserIsSiteAdmin: (req, res, next) ->
  68. AuthorizationMiddlewear._getUserId req, (error, user_id) ->
  69. return next(error) if error?
  70. AuthorizationManager.isUserSiteAdmin user_id, (error, isAdmin) ->
  71. return next(error) if error?
  72. if isAdmin
  73. logger.log {user_id}, "allowing user admin access to site"
  74. next()
  75. else
  76. logger.log {user_id}, "denying user admin access to site"
  77. AuthorizationMiddlewear.redirectToRestricted req, res, next
  78. _getUserAndProjectId: (req, callback = (error, user_id, project_id) ->) ->
  79. project_id = req.params?.project_id or req.params?.Project_id
  80. if !project_id?
  81. return callback(new Error("Expected project_id in request parameters"))
  82. if !ObjectId.isValid(project_id)
  83. return callback(new Errors.NotFoundError("invalid project_id: #{project_id}"))
  84. AuthorizationMiddlewear._getUserId req, (error, user_id) ->
  85. return callback(error) if error?
  86. callback(null, user_id, project_id)
  87. _getUserId: (req, callback = (error, user_id) ->) ->
  88. user_id = AuthenticationController.getLoggedInUserId(req)
  89. return callback(null, user_id)
  90. redirectToRestricted: (req, res, next) ->
  91. res.redirect "/restricted?from=#{encodeURIComponent(req.url)}"
  92. restricted : (req, res, next)->
  93. if AuthenticationController.isUserLoggedIn(req)
  94. res.render 'user/restricted',
  95. title:'restricted'
  96. else
  97. from = req.query.from
  98. logger.log {from: from}, "redirecting to login"
  99. redirect_to = "/login"
  100. if from?
  101. redirect_to += "?redir=#{encodeURIComponent(from)}"
  102. res.redirect redirect_to