Add "[TokenAccess]" context to log lines

This commit is contained in:
Shane Kilkelly 2017-10-31 14:27:43 +00:00
parent 030ffbe231
commit a212fb1a05

View file

@ -14,53 +14,53 @@ module.exports = TokenAccessController =
readAndWriteToken: (req, res, next) -> readAndWriteToken: (req, res, next) ->
userId = AuthenticationController.getLoggedInUserId(req) userId = AuthenticationController.getLoggedInUserId(req)
token = req.params['read_and_write_token'] token = req.params['read_and_write_token']
logger.log {userId, token}, "requesting read-and-write token access" logger.log {userId, token}, "[TokenAccess] requesting read-and-write token access"
TokenAccessHandler.findProjectWithReadAndWriteToken token, (err, project) -> TokenAccessHandler.findProjectWithReadAndWriteToken token, (err, project) ->
if err? if err?
logger.err {err, token, userId}, logger.err {err, token, userId},
"error getting project by readAndWrite token" "[TokenAccess] error getting project by readAndWrite token"
return next(err) return next(err)
if !project? if !project?
logger.log {token, userId}, logger.log {token, userId},
"no project found for readAndWrite token" "[TokenAccess] no project found for readAndWrite token"
if !userId? if !userId?
logger.log {token}, logger.log {token},
"No project found with read-write token, anonymous user" "[TokenAccess] No project found with read-write token, anonymous user"
return next(new Errors.NotFoundError()) return next(new Errors.NotFoundError())
TokenAccessHandler TokenAccessHandler
.findPrivateOverleafProjectWithReadAndWriteToken token, (err, project) -> .findPrivateOverleafProjectWithReadAndWriteToken token, (err, project) ->
if err? if err?
logger.err {err, token, userId}, logger.err {err, token, userId},
"error getting project by readAndWrite token" "[TokenAccess] error getting project by readAndWrite token"
return next(err) return next(err)
if !project? if !project?
logger.log {token, userId}, logger.log {token, userId},
"no private-overleaf project found with readAndWriteToken" "[TokenAccess] no private-overleaf project found with readAndWriteToken"
return next(new Errors.NotFoundError()) return next(new Errors.NotFoundError())
logger.log {token, projectId: project._id}, "redirecting user to project" logger.log {token, projectId: project._id}, "[TokenAccess] redirecting user to project"
res.redirect(302, "/project/#{project._id}") res.redirect(302, "/project/#{project._id}")
else else
if !userId? if !userId?
if TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED if TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED
logger.log {token, projectId: project._id}, logger.log {token, projectId: project._id},
"allow anonymous read-and-write token access" "[TokenAccess] allow anonymous read-and-write token access"
TokenAccessHandler.grantSessionTokenAccess(req, project._id, token) TokenAccessHandler.grantSessionTokenAccess(req, project._id, token)
req._anonymousAccessToken = token req._anonymousAccessToken = token
return TokenAccessController._loadEditor(project._id, req, res, next) return TokenAccessController._loadEditor(project._id, req, res, next)
else else
logger.log {token, projectId: project._id}, logger.log {token, projectId: project._id},
"deny anonymous read-and-write token access" "[TokenAccess] deny anonymous read-and-write token access"
return next(new Errors.NotFoundError()) return next(new Errors.NotFoundError())
if project.owner_ref.toString() == userId if project.owner_ref.toString() == userId
logger.log {userId, projectId: project._id}, logger.log {userId, projectId: project._id},
"user is already project owner" "[TokenAccess] user is already project owner"
return TokenAccessController._loadEditor(project._id, req, res, next) return TokenAccessController._loadEditor(project._id, req, res, next)
logger.log {userId, projectId: project._id}, logger.log {userId, projectId: project._id},
"adding user to project with readAndWrite token" "[TokenAccess] adding user to project with readAndWrite token"
TokenAccessHandler.addReadAndWriteUserToProject userId, project._id, (err) -> TokenAccessHandler.addReadAndWriteUserToProject userId, project._id, (err) ->
if err? if err?
logger.err {err, token, userId, projectId: project._id}, logger.err {err, token, userId, projectId: project._id},
"error adding user to project with readAndWrite token" "[TokenAccess] error adding user to project with readAndWrite token"
return next(err) return next(err)
# TODO: check if this is still needed by the client # TODO: check if this is still needed by the client
setTimeout( () -> setTimeout( () ->
@ -74,33 +74,33 @@ module.exports = TokenAccessController =
readOnlyToken: (req, res, next) -> readOnlyToken: (req, res, next) ->
userId = AuthenticationController.getLoggedInUserId(req) userId = AuthenticationController.getLoggedInUserId(req)
token = req.params['read_only_token'] token = req.params['read_only_token']
logger.log {userId, token}, "requesting read-only token access" logger.log {userId, token}, "[TokenAccess] requesting read-only token access"
TokenAccessHandler.findProjectWithReadOnlyToken token, (err, project) -> TokenAccessHandler.findProjectWithReadOnlyToken token, (err, project) ->
if err? if err?
logger.err {err, token, userId}, logger.err {err, token, userId},
"error getting project by readOnly token" "[TokenAccess] error getting project by readOnly token"
return next(err) return next(err)
if !project? if !project?
logger.log {token, userId}, logger.log {token, userId},
"no project found for readAndWrite token" "[TokenAccess] no project found for readAndWrite token"
return next(new Errors.NotFoundError()) return next(new Errors.NotFoundError())
if !userId? if !userId?
logger.log {userId, projectId: project._id}, logger.log {userId, projectId: project._id},
"adding anonymous user to project with readOnly token" "[TokenAccess] adding anonymous user to project with readOnly token"
TokenAccessHandler.grantSessionTokenAccess(req, project._id, token) TokenAccessHandler.grantSessionTokenAccess(req, project._id, token)
req._anonymousAccessToken = token req._anonymousAccessToken = token
return TokenAccessController._loadEditor(project._id, req, res, next) return TokenAccessController._loadEditor(project._id, req, res, next)
else else
if project.owner_ref.toString() == userId if project.owner_ref.toString() == userId
logger.log {userId, projectId: project._id}, logger.log {userId, projectId: project._id},
"user is already project owner" "[TokenAccess] user is already project owner"
return TokenAccessController._loadEditor(project._id, req, res, next) return TokenAccessController._loadEditor(project._id, req, res, next)
logger.log {userId, projectId: project._id}, logger.log {userId, projectId: project._id},
"adding user to project with readOnly token" "[TokenAccess] adding user to project with readOnly token"
TokenAccessHandler.addReadOnlyUserToProject userId, project._id, (err) -> TokenAccessHandler.addReadOnlyUserToProject userId, project._id, (err) ->
if err? if err?
logger.err {err, token, userId, projectId: project._id}, logger.err {err, token, userId, projectId: project._id},
"error adding user to project with readAndWrite token" "[TokenAccess] error adding user to project with readAndWrite token"
return next(err) return next(err)
return TokenAccessController._loadEditor(project._id, req, res, next) return TokenAccessController._loadEditor(project._id, req, res, next)