2018-10-24 09:50:34 -04:00
|
|
|
AuthenticationController = require('../Authentication/AuthenticationController')
|
|
|
|
AuthorizationMiddlewear = require('../Authorization/AuthorizationMiddlewear')
|
|
|
|
UserMembershipHandler = require('./UserMembershipHandler')
|
|
|
|
EntityConfigs = require('./UserMembershipEntityConfigs')
|
|
|
|
Errors = require('../Errors/Errors')
|
|
|
|
logger = require("logger-sharelatex")
|
2018-12-05 09:54:00 -05:00
|
|
|
settings = require 'settings-sharelatex'
|
|
|
|
request = require 'request'
|
2018-10-24 09:50:34 -04:00
|
|
|
|
2018-12-05 09:54:00 -05:00
|
|
|
module.exports = UserMembershipAuthorization =
|
2019-01-11 09:17:27 -05:00
|
|
|
requireTeamMetricsAccess: (req, res, next) ->
|
|
|
|
requireAccessToEntity('team', req.params.id, req, res, next, 'groupMetrics')
|
2018-11-30 08:03:35 -05:00
|
|
|
|
2019-01-11 09:17:27 -05:00
|
|
|
requireGroupManagementAccess: (req, res, next) ->
|
|
|
|
requireAccessToEntity('group', req.params.id, req, res, next, 'groupManagement')
|
2018-11-30 08:03:35 -05:00
|
|
|
|
2019-01-11 09:17:27 -05:00
|
|
|
requireGroupMetricsAccess: (req, res, next) ->
|
|
|
|
requireAccessToEntity('group', req.params.id, req, res, next, 'groupMetrics')
|
2018-11-30 08:03:35 -05:00
|
|
|
|
2019-01-11 09:17:27 -05:00
|
|
|
requireGroupManagersManagementAccess: (req, res, next) ->
|
|
|
|
requireAccessToEntity('groupManagers', req.params.id, req, res, next, 'groupManagement')
|
2018-11-30 08:03:35 -05:00
|
|
|
|
2019-01-11 09:17:27 -05:00
|
|
|
requireInstitutionMetricsAccess: (req, res, next) ->
|
|
|
|
requireAccessToEntity('institution', req.params.id, req, res, next, 'institutionMetrics')
|
2018-11-30 08:03:35 -05:00
|
|
|
|
2019-01-11 09:17:27 -05:00
|
|
|
requireInstitutionManagementAccess: (req, res, next) ->
|
|
|
|
requireAccessToEntity('institution', req.params.id, req, res, next, 'institutionManagement')
|
|
|
|
|
|
|
|
requirePublisherMetricsAccess: (req, res, next) ->
|
|
|
|
requireAccessToEntity('publisher', req.params.id, req, res, next, 'publisherMetrics')
|
|
|
|
|
|
|
|
requirePublisherManagementAccess: (req, res, next) ->
|
|
|
|
requireAccessToEntity('publisher', req.params.id, req, res, next, 'publisherManagement')
|
|
|
|
|
|
|
|
requireTemplateMetricsAccess: (req, res, next) ->
|
2018-12-05 09:54:00 -05:00
|
|
|
templateId = req.params.id
|
|
|
|
request {
|
|
|
|
baseUrl: settings.apis.v1.url
|
|
|
|
url: "/api/v2/templates/#{templateId}"
|
|
|
|
method: 'GET'
|
|
|
|
auth:
|
|
|
|
user: settings.apis.v1.user
|
|
|
|
pass: settings.apis.v1.pass
|
|
|
|
sendImmediately: true
|
|
|
|
}, (error, response, body) =>
|
|
|
|
if response.statusCode == 404
|
|
|
|
return next(new Errors.NotFoundError())
|
|
|
|
|
|
|
|
if response.statusCode != 200
|
|
|
|
logger.err { templateId }, "[TemplateMetrics] Couldn't fetch template data from v1"
|
|
|
|
return next(new Error("Couldn't fetch template data from v1"))
|
|
|
|
|
|
|
|
return next(error) if error?
|
|
|
|
try
|
|
|
|
body = JSON.parse(body)
|
|
|
|
catch error
|
|
|
|
return next(error)
|
|
|
|
|
|
|
|
req.template =
|
|
|
|
id: body.id
|
|
|
|
title: body.title
|
2018-12-06 08:52:41 -05:00
|
|
|
if body?.brand?.slug
|
2019-01-11 09:17:27 -05:00
|
|
|
req.params.id = body.brand.slug
|
|
|
|
UserMembershipAuthorization.requirePublisherMetricsAccess(req, res, next)
|
2018-12-06 08:52:41 -05:00
|
|
|
else
|
|
|
|
AuthorizationMiddlewear.ensureUserIsSiteAdmin(req, res, next)
|
2018-12-05 09:54:00 -05:00
|
|
|
|
2018-11-30 08:03:35 -05:00
|
|
|
requireGraphAccess: (req, res, next) ->
|
2019-01-11 09:17:27 -05:00
|
|
|
req.params.id = req.query.resource_id
|
2018-12-05 09:54:00 -05:00
|
|
|
if req.query.resource_type == 'template'
|
2019-01-11 09:17:27 -05:00
|
|
|
return UserMembershipAuthorization.requireTemplateMetricsAccess(req, res, next)
|
|
|
|
else if req.query.resource_type == 'institution'
|
|
|
|
return UserMembershipAuthorization.requireInstitutionMetricsAccess(req, res, next)
|
|
|
|
else if req.query.resource_type == 'group'
|
|
|
|
return UserMembershipAuthorization.requireGroupMetricsAccess(req, res, next)
|
|
|
|
else if req.query.resource_type == 'team'
|
|
|
|
return UserMembershipAuthorization.requireTeamMetricsAccess(req, res, next)
|
|
|
|
requireAccessToEntity(req.query.resource_type, req.query.resource_id, req, res, next)
|
|
|
|
|
|
|
|
requireAccessToEntity = (entityName, entityId, req, res, next, requiredStaffAccess=null) ->
|
2018-11-30 08:03:35 -05:00
|
|
|
loggedInUser = AuthenticationController.getSessionUser(req)
|
|
|
|
unless loggedInUser
|
|
|
|
return AuthorizationMiddlewear.redirectToRestricted req, res, next
|
|
|
|
|
2019-01-11 09:17:27 -05:00
|
|
|
getEntity entityName, entityId, loggedInUser, requiredStaffAccess, (error, entity, entityConfig, entityExists) ->
|
2018-11-30 08:03:35 -05:00
|
|
|
return next(error) if error?
|
2018-12-06 05:52:54 -05:00
|
|
|
|
|
|
|
if entity?
|
|
|
|
req.entity = entity
|
|
|
|
req.entityConfig = entityConfig
|
|
|
|
return next()
|
|
|
|
|
|
|
|
if entityExists # user doesn't have access to entity
|
2018-11-30 08:03:35 -05:00
|
|
|
return AuthorizationMiddlewear.redirectToRestricted(req, res, next)
|
|
|
|
|
2018-12-06 05:52:54 -05:00
|
|
|
if loggedInUser.isAdmin and entityConfig.canCreate
|
|
|
|
# entity doesn't exists, admin can create it
|
|
|
|
return res.redirect "/entities/#{entityName}/create/#{entityId}"
|
2018-10-24 09:50:34 -04:00
|
|
|
|
2018-12-06 05:52:54 -05:00
|
|
|
next(new Errors.NotFoundError())
|
|
|
|
|
2019-01-11 09:17:27 -05:00
|
|
|
getEntity = (entityName, entityId, user, requiredStaffAccess, callback = (error, entity, entityConfig, entityExists)->) ->
|
2018-10-24 09:50:34 -04:00
|
|
|
entityConfig = EntityConfigs[entityName]
|
|
|
|
unless entityConfig
|
|
|
|
return callback(new Errors.NotFoundError("No such entity: #{entityName}"))
|
|
|
|
|
2019-01-11 09:17:27 -05:00
|
|
|
UserMembershipHandler.getEntity entityId, entityConfig, user, requiredStaffAccess, (error, entity)->
|
2018-10-24 09:50:34 -04:00
|
|
|
return callback(error) if error?
|
2018-12-06 05:52:54 -05:00
|
|
|
return callback(null, entity, entityConfig, true) if entity?
|
|
|
|
|
|
|
|
# no access to entity. Check if entity exists
|
|
|
|
UserMembershipHandler.getEntityWithoutAuthorizationCheck entityId, entityConfig, (error, entity)->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, null, entityConfig, entity?)
|