Merge pull request #1580 from sharelatex/ta-entity-create-form-fix-2

Fix Publisher Creation Form Take2

GitOrigin-RevId: a51512cc48067f50ba05fa371ed1d759d3fca985
This commit is contained in:
Timothée Alby 2019-03-14 08:29:21 -05:00 committed by sharelatex
parent a1e16c9676
commit e4c684786d
3 changed files with 23 additions and 4 deletions

View file

@ -77,6 +77,12 @@ module.exports = UserMembershipAuthorization =
return UserMembershipAuthorization.requireTeamMetricsAccess(req, res, next) return UserMembershipAuthorization.requireTeamMetricsAccess(req, res, next)
requireAccessToEntity(req.query.resource_type, req.query.resource_id, req, res, next) requireAccessToEntity(req.query.resource_type, req.query.resource_id, req, res, next)
requireEntityCreationAccess: (req, res, next) ->
loggedInUser = AuthenticationController.getSessionUser(req)
unless loggedInUser and hasEntityCreationAccess(loggedInUser)
return AuthorizationMiddlewear.redirectToRestricted req, res, next
next()
requireAccessToEntity = (entityName, entityId, req, res, next, requiredStaffAccess=null) -> requireAccessToEntity = (entityName, entityId, req, res, next, requiredStaffAccess=null) ->
loggedInUser = AuthenticationController.getSessionUser(req) loggedInUser = AuthenticationController.getSessionUser(req)
unless loggedInUser unless loggedInUser
@ -93,7 +99,7 @@ requireAccessToEntity = (entityName, entityId, req, res, next, requiredStaffAcce
if entityExists # user doesn't have access to entity if entityExists # user doesn't have access to entity
return AuthorizationMiddleware.redirectToRestricted(req, res, next) return AuthorizationMiddleware.redirectToRestricted(req, res, next)
if loggedInUser.isAdmin and entityConfig.canCreate if hasEntityCreationAccess(loggedInUser) and entityConfig.canCreate
# entity doesn't exists, admin can create it # entity doesn't exists, admin can create it
return res.redirect "/entities/#{entityName}/create/#{entityId}" return res.redirect "/entities/#{entityName}/create/#{entityId}"
@ -112,3 +118,8 @@ getEntity = (entityName, entityId, user, requiredStaffAccess, callback = (error,
UserMembershipHandler.getEntityWithoutAuthorizationCheck entityId, entityConfig, (error, entity)-> UserMembershipHandler.getEntityWithoutAuthorizationCheck entityId, entityConfig, (error, entity)->
return callback(error) if error? return callback(error) if error?
callback(null, null, entityConfig, entity?) callback(null, null, entityConfig, entity?)
hasEntityCreationAccess = (user) ->
user.isAdmin or
user.staffAccess?['institutionManagement'] or
user.staffAccess?['publisherManagement']

View file

@ -2,7 +2,6 @@ UserMembershipAuthorization = require './UserMembershipAuthorization'
UserMembershipController = require './UserMembershipController' UserMembershipController = require './UserMembershipController'
SubscriptionGroupController = require '../Subscription/SubscriptionGroupController' SubscriptionGroupController = require '../Subscription/SubscriptionGroupController'
TeamInvitesController = require '../Subscription/TeamInvitesController' TeamInvitesController = require '../Subscription/TeamInvitesController'
AuthorizationMiddleware = require('../Authorization/AuthorizationMiddleware')
RateLimiterMiddleware = require('../Security/RateLimiterMiddleware') RateLimiterMiddleware = require('../Security/RateLimiterMiddleware')
module.exports = module.exports =
@ -69,8 +68,8 @@ module.exports =
# create new entitites # create new entitites
webRouter.get "/entities/:name/create/:id", webRouter.get "/entities/:name/create/:id",
UserMembershipAuthorization.requirePublisherManagementAccess, UserMembershipAuthorization.requireEntityCreationAccess,
UserMembershipController.new UserMembershipController.new
webRouter.post "/entities/:name/create/:id", webRouter.post "/entities/:name/create/:id",
UserMembershipAuthorization.requirePublisherManagementAccess, UserMembershipAuthorization.requireEntityCreationAccess,
UserMembershipController.create UserMembershipController.create

View file

@ -67,6 +67,15 @@ describe "UserMembershipAuthorization", ->
expect(path).to.match /create/ expect(path).to.match /create/
done() done()
it 'handle entity not found a non-admin can create', (done) ->
@user.staffAccess = { institutionManagement: true }
@UserMembershipHandler.getEntity.yields(null, null)
@UserMembershipHandler.getEntityWithoutAuthorizationCheck.yields(null, null)
@UserMembershipAuthorization.requirePublisherMetricsAccess @req, redirect: (path) =>
expect(path).to.extist
expect(path).to.match /create/
done()
it 'handle entity not found an admin cannot create', (done) -> it 'handle entity not found an admin cannot create', (done) ->
@user.isAdmin = true @user.isAdmin = true
@UserMembershipHandler.getEntity.yields(null, null) @UserMembershipHandler.getEntity.yields(null, null)