Fix up more session access

This commit is contained in:
Shane Kilkelly 2016-09-22 14:30:34 +01:00
parent c959e0c65d
commit ff1c72ee14
4 changed files with 21 additions and 13 deletions

View file

@ -2,14 +2,15 @@ BetaProgramHandler = require './BetaProgramHandler'
UserLocator = require "../User/UserLocator"
Settings = require "settings-sharelatex"
logger = require 'logger-sharelatex'
AuthenticationController = require '../Authentication/AuthenticationController'
module.exports = BetaProgramController =
optIn: (req, res, next) ->
user_id = req?.session?.user?._id
user_id = AuthenticationController.getLoggedInUserId(req)
logger.log {user_id}, "user opting in to beta program"
if !user_id
if !user_id?
return next(new Error("no user id in session"))
BetaProgramHandler.optIn user_id, (err) ->
if err
@ -17,9 +18,9 @@ module.exports = BetaProgramController =
return res.redirect "/beta/participate"
optOut: (req, res, next) ->
user_id = req?.session?.user?._id
user_id = AuthenticationController.getLoggedInUserId(req)
logger.log {user_id}, "user opting out of beta program"
if !user_id
if !user_id?
return next(new Error("no user id in session"))
BetaProgramHandler.optOut user_id, (err) ->
if err
@ -27,7 +28,7 @@ module.exports = BetaProgramController =
return res.redirect "/beta/participate"
optInPage: (req, res, next)->
user_id = req.session?.user?._id
user_id = AuthenticationController.getLoggedInUserId(req)
logger.log {user_id}, "showing beta participation page for user"
UserLocator.findById user_id, (err, user)->
if err

View file

@ -1,6 +1,7 @@
User = require("../../models/User").User
AuthenticationController = require('../Authentication/AuthenticationController')
# TODO: module appears to be dead, consider removal
module.exports = RefererMiddleware =
getUserReferalId: (req, res, next) ->
if AuthenticationController.isUserLoggedIn()

View file

@ -147,12 +147,14 @@ module.exports = (app, webRouter, apiRouter)->
webRouter.use (req, res, next)->
res.locals.buildReferalUrl = (referal_medium) ->
url = Settings.siteUrl
if req.user? and req.referal_id?
url+="?r=#{req.user.referal_id}&rm=#{referal_medium}&rs=b" # Referal source = bonus
currentUser = AuthenticationController.getSessionUser(req)
if currentUser? and currentUser?.referal_id?
url+="?r=#{currentUser.referal_id}&rm=#{referal_medium}&rs=b" # Referal source = bonus
return url
res.locals.getReferalId = ->
if req.user? and req.referal_id?
return req.user.referal_id
currentUser = AuthenticationController.getSessionUser(req)
if currentUser? and currentUser?.referal_id?
return currentUser.referal_id
res.locals.getReferalTagLine = ->
tagLines = [
"Roar!"
@ -198,11 +200,12 @@ module.exports = (app, webRouter, apiRouter)->
next()
webRouter.use (req, res, next)->
if req.user?
currentUser = AuthenticationController.getSessionUser(req)
if currentUser?
res.locals.user =
email: req.user.email
first_name: req.user.first_name
last_name: req.user.last_name
email: currentUser.email
first_name: currentUser.first_name
last_name: currentUser.last_name
if req.session.justRegistered
res.locals.justRegistered = true
delete req.session.justRegistered

View file

@ -34,6 +34,9 @@ describe "BetaProgramController", ->
err: sinon.stub()
error: sinon.stub()
}
'../Authentication/AuthenticationController': @AuthenticationController = {
getLoggedInUserId: sinon.stub().returns(@user._id)
}
@res =
send: sinon.stub()
redirect: sinon.stub()