Merge pull request #1193 from sharelatex/ta-template-metrics

Template Metrics

GitOrigin-RevId: 0a8648aec3a9446426c70cd8220bb1f776313303
This commit is contained in:
Timothée Alby 2018-12-05 16:54:00 +02:00 committed by sharelatex
parent e0148bf69b
commit fde672542e
2 changed files with 53 additions and 1 deletions

View file

@ -4,8 +4,10 @@ UserMembershipHandler = require('./UserMembershipHandler')
EntityConfigs = require('./UserMembershipEntityConfigs') EntityConfigs = require('./UserMembershipEntityConfigs')
Errors = require('../Errors/Errors') Errors = require('../Errors/Errors')
logger = require("logger-sharelatex") logger = require("logger-sharelatex")
settings = require 'settings-sharelatex'
request = require 'request'
module.exports = module.exports = UserMembershipAuthorization =
requireTeamAccess: (req, res, next) -> requireTeamAccess: (req, res, next) ->
requireAccessToEntity('team', req.params.id, req, res, next) requireAccessToEntity('team', req.params.id, req, res, next)
@ -21,7 +23,41 @@ module.exports =
requirePublisherAccess: (req, res, next) -> requirePublisherAccess: (req, res, next) ->
requireAccessToEntity('publisher', req.params.id, req, res, next) requireAccessToEntity('publisher', req.params.id, req, res, next)
requireTemplateAccess: (req, res, next) ->
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
requireAccessToEntity('publisher', body.brand.slug, req, res, next)
requireGraphAccess: (req, res, next) -> requireGraphAccess: (req, res, next) ->
if req.query.resource_type == 'template'
# templates are a special case; can't use requireaccesstoentity directly
req.params.id = req.query.resource_id
return UserMembershipAuthorization.requireTemplateAccess(req, res, next)
requireAccessToEntity( requireAccessToEntity(
req.query.resource_type, req.query.resource_id, req, res, next req.query.resource_type, req.query.resource_id, req, res, next
) )

View file

@ -26,6 +26,7 @@ describe "UserMembershipAuthorization", ->
'./UserMembershipHandler': @UserMembershipHandler './UserMembershipHandler': @UserMembershipHandler
'./EntityConfigs': EntityConfigs './EntityConfigs': EntityConfigs
'../Errors/Errors': Errors '../Errors/Errors': Errors
'request': @request = sinon.stub().yields(null, null, {})
"logger-sharelatex": "logger-sharelatex":
log: -> log: ->
err: -> err: ->
@ -103,6 +104,21 @@ describe "UserMembershipAuthorization", ->
) )
done() done()
it 'handle template access', (done) ->
templateData =
id: 123
title: 'Template Title'
brand: { slug: 'brand-slug' }
@request.yields(null, { statusCode: 200 }, JSON.stringify(templateData))
@UserMembershipAuthorization.requireTemplateAccess @req, null, (error) =>
expect(error).to.not.extist
sinon.assert.calledWithMatch(
@UserMembershipHandler.getEntity,
'brand-slug',
modelName: 'Publisher',
)
done()
it 'handle graph access', (done) -> it 'handle graph access', (done) ->
@req.query.resource_id = 'mock-resource-id' @req.query.resource_id = 'mock-resource-id'
@req.query.resource_type = 'institution' @req.query.resource_type = 'institution'