Просмотр исходного кода

use `getSessionUser` rather than `getLoggedInUser`

Shane Kilkelly 10 лет назад
Родитель
Сommit
cc5ddc92bb

+ 62 - 66
services/web/app/coffee/Features/Collaborators/CollaboratorsInviteController.coffee

@@ -24,27 +24,26 @@ module.exports = CollaboratorsInviteController =
 	inviteToProject: (req, res, next) ->
 		projectId = req.params.Project_id
 		email = req.body.email
-		AuthenticationController.getLoggedInUser req, (err, sendingUser) ->
-			return callback(err) if err?
-			sendingUserId = sendingUser._id
-			logger.log {projectId, email, sendingUserId}, "inviting to project"
-			LimitationsManager.canAddXCollaborators projectId, 1, (error, allowed) =>
-				return next(error) if error?
-				if !allowed
-					logger.log {projectId, email, sendingUserId}, "not allowed to invite more users to project"
-					return res.json {invite: null}
-				{email, privileges} = req.body
-				email = EmailHelper.parseEmail(email)
-				if !email? or email == ""
-					logger.log {projectId, email, sendingUserId}, "invalid email address"
-					return res.sendStatus(400)
-				CollaboratorsInviteHandler.inviteToProject projectId, sendingUser, email, privileges, (err, invite) ->
-					if err?
-						logger.err {projectId, email, sendingUserId}, "error creating project invite"
-						return next(err)
-					logger.log {projectId, email, sendingUserId}, "invite created"
-					EditorRealTimeController.emitToRoom(projectId, 'project:membership:changed', {invites: true})
-					return res.json {invite: invite}
+		sendingUser = AuthenticationController.getSessionUser(req)
+		sendingUserId = sendingUser._id
+		logger.log {projectId, email, sendingUserId}, "inviting to project"
+		LimitationsManager.canAddXCollaborators projectId, 1, (error, allowed) =>
+			return next(error) if error?
+			if !allowed
+				logger.log {projectId, email, sendingUserId}, "not allowed to invite more users to project"
+				return res.json {invite: null}
+			{email, privileges} = req.body
+			email = EmailHelper.parseEmail(email)
+			if !email? or email == ""
+				logger.log {projectId, email, sendingUserId}, "invalid email address"
+				return res.sendStatus(400)
+			CollaboratorsInviteHandler.inviteToProject projectId, sendingUser, email, privileges, (err, invite) ->
+				if err?
+					logger.err {projectId, email, sendingUserId}, "error creating project invite"
+					return next(err)
+				logger.log {projectId, email, sendingUserId}, "invite created"
+				EditorRealTimeController.emitToRoom(projectId, 'project:membership:changed', {invites: true})
+				return res.json {invite: invite}
 
 	revokeInvite: (req, res, next) ->
 		projectId = req.params.Project_id
@@ -61,13 +60,12 @@ module.exports = CollaboratorsInviteController =
 		projectId = req.params.Project_id
 		inviteId = req.params.invite_id
 		logger.log {projectId, inviteId}, "resending invite"
-		AuthenticationController.getLoggedInUser req, (err, sendingUser) ->
-			return callback(err) if err?
-			CollaboratorsInviteHandler.resendInvite projectId, sendingUser, inviteId, (err) ->
-				if err?
-					logger.err {projectId, inviteId}, "error resending invite"
-					return next(err)
-				res.sendStatus(201)
+		sendingUser = AuthenticationController.getSessionUser(req)
+		CollaboratorsInviteHandler.resendInvite projectId, sendingUser, inviteId, (err) ->
+			if err?
+				logger.err {projectId, inviteId}, "error resending invite"
+				return next(err)
+			res.sendStatus(201)
 
 	viewInvite: (req, res, next) ->
 		projectId = req.params.Project_id
@@ -76,54 +74,52 @@ module.exports = CollaboratorsInviteController =
 			logger.log {projectId, token}, "invite not valid, rendering not-valid page"
 			res.render "project/invite/not-valid", {title: "Invalid Invite"}
 		# check if the user is already a member of the project
-		AuthenticationController.getLoggedInUser req, (err, currentUser) ->
-			return callback(err) if err?
-			CollaboratorsHandler.isUserMemberOfProject currentUser._id, projectId, (err, isMember, _privilegeLevel) ->
+		currentUser = AuthenticationController.getSessionUser(req)
+		CollaboratorsHandler.isUserMemberOfProject currentUser._id, projectId, (err, isMember, _privilegeLevel) ->
+			if err?
+				logger.err {err, projectId}, "error checking if user is member of project"
+				return next(err)
+			if isMember
+				logger.log {projectId, userId: currentUser._id}, "user is already a member of this project, redirecting"
+				return res.redirect "/project/#{projectId}"
+			# get the invite
+			CollaboratorsInviteHandler.getInviteByToken projectId, token, (err, invite) ->
 				if err?
-					logger.err {err, projectId}, "error checking if user is member of project"
+					logger.err {projectId, token}, "error getting invite by token"
 					return next(err)
-				if isMember
-					logger.log {projectId, userId: currentUser._id}, "user is already a member of this project, redirecting"
-					return res.redirect "/project/#{projectId}"
-				# get the invite
-				CollaboratorsInviteHandler.getInviteByToken projectId, token, (err, invite) ->
+				# check if invite is gone, or otherwise non-existent
+				if !invite?
+					logger.log {projectId, token}, "no invite found for this token"
+					return _renderInvalidPage()
+				# check the user who sent the invite exists
+				UserGetter.getUser {_id: invite.sendingUserId}, {email: 1, first_name: 1, last_name: 1}, (err, owner) ->
 					if err?
-						logger.err {projectId, token}, "error getting invite by token"
+						logger.err {err, projectId}, "error getting project owner"
 						return next(err)
-					# check if invite is gone, or otherwise non-existent
-					if !invite?
-						logger.log {projectId, token}, "no invite found for this token"
+					if !owner?
+						logger.log {projectId}, "no project owner found"
 						return _renderInvalidPage()
-					# check the user who sent the invite exists
-					UserGetter.getUser {_id: invite.sendingUserId}, {email: 1, first_name: 1, last_name: 1}, (err, owner) ->
+					# fetch the project name
+					ProjectGetter.getProject projectId, {}, (err, project) ->
 						if err?
-							logger.err {err, projectId}, "error getting project owner"
+							logger.err {err, projectId}, "error getting project"
 							return next(err)
-						if !owner?
-							logger.log {projectId}, "no project owner found"
+						if !project?
+							logger.log {projectId}, "no project found"
 							return _renderInvalidPage()
-						# fetch the project name
-						ProjectGetter.getProject projectId, {}, (err, project) ->
-							if err?
-								logger.err {err, projectId}, "error getting project"
-								return next(err)
-							if !project?
-								logger.log {projectId}, "no project found"
-								return _renderInvalidPage()
-							# finally render the invite
-							res.render "project/invite/show", {invite, project, owner, title: "Project Invite"}
+						# finally render the invite
+						res.render "project/invite/show", {invite, project, owner, title: "Project Invite"}
 
 	acceptInvite: (req, res, next) ->
 		projectId = req.params.Project_id
 		inviteId = req.params.invite_id
 		{token} = req.body
-		AuthenticationController.getLoggedInUser req, (err, currentUser) ->
-			return callback(err) if err?
-			logger.log {projectId, inviteId, userId: currentUser._id}, "accepting invite"
-			CollaboratorsInviteHandler.acceptInvite projectId, inviteId, token, currentUser, (err) ->
-				if err?
-					logger.err {projectId, inviteId}, "error accepting invite by token"
-					return next(err)
-				EditorRealTimeController.emitToRoom projectId, 'project:membership:changed', {invites: true, members: true}
-				AnalyticsManger.recordEvent(currentUser._id, "project-invite-accept", {inviteId:inviteId, projectId:projectId})
-				res.redirect "/project/#{projectId}"
+		currentUser = AuthenticationController.getSessionUser(req)
+		logger.log {projectId, inviteId, userId: currentUser._id}, "accepting invite"
+		CollaboratorsInviteHandler.acceptInvite projectId, inviteId, token, currentUser, (err) ->
+			if err?
+				logger.err {projectId, inviteId}, "error accepting invite by token"
+				return next(err)
+			EditorRealTimeController.emitToRoom projectId, 'project:membership:changed', {invites: true, members: true}
+			AnalyticsManger.recordEvent(currentUser._id, "project-invite-accept", {inviteId:inviteId, projectId:projectId})
+			res.redirect "/project/#{projectId}"

+ 51 - 53
services/web/app/coffee/Features/Project/ProjectController.coffee

@@ -91,13 +91,12 @@ module.exports = ProjectController =
 		logger.log project_id:project_id, projectName:projectName, "cloning project"
 		if !AuthenticationController.isUserLoggedIn()?
 			return res.send redir:"/register"
-		AuthenticationController.getLoggedInUser req, (err, currentUser) ->
-			return next(err) if err?
-			projectDuplicator.duplicate currentUser, project_id, projectName, (err, project)->
-				if err?
-					logger.error err:err, project_id: project_id, user_id: currentUser._id, "error cloning project"
-					return next(err)
-				res.send(project_id:project._id)
+		currentUser = AuthenticationController.getSessionUser(req)
+		projectDuplicator.duplicate currentUser, project_id, projectName, (err, project)->
+			if err?
+				logger.error err:err, project_id: project_id, user_id: currentUser._id, "error cloning project"
+				return next(err)
+			res.send(project_id:project._id)
 
 
 	newProject: (req, res)->
@@ -135,52 +134,51 @@ module.exports = ProjectController =
 	projectListPage: (req, res, next)->
 		timer = new metrics.Timer("project-list")
 		user_id = AuthenticationController.getLoggedInUserId(req)
-		AuthenticationController.getLoggedInUser req, (err, currentUser) ->
-			return next(err) if err?
-			async.parallel {
-				tags: (cb)->
-					TagsHandler.getAllTags user_id, cb
-				notifications: (cb)->
-					NotificationsHandler.getUserNotifications user_id, cb
-				projects: (cb)->
-					ProjectGetter.findAllUsersProjects user_id, 'name lastUpdated publicAccesLevel archived owner_ref', cb
-				hasSubscription: (cb)->
-					LimitationsManager.userHasSubscriptionOrIsGroupMember currentUser, cb
-				user: (cb) ->
-					User.findById user_id, "featureSwitches", cb
-				}, (err, results)->
-					if err?
-						logger.err err:err, "error getting data for project list page"
-						return next(err)
-					logger.log results:results, user_id:user_id, "rendering project list"
-					tags = results.tags[0]
-					notifications = require("underscore").map results.notifications, (notification)->
-						notification.html = req.i18n.translate(notification.templateKey, notification.messageOpts)
-						return notification
-					projects = ProjectController._buildProjectList results.projects[0], results.projects[1], results.projects[2]
-					user = results.user
-					ProjectController._injectProjectOwners projects, (error, projects) ->
-						return next(error) if error?
-
-						viewModel = {
-							title:'your_projects'
-							priority_title: true
-							projects: projects
-							tags: tags
-							notifications: notifications or []
-							user: user
-							hasSubscription: results.hasSubscription[0]
-						}
-
-						if Settings?.algolia?.app_id? and Settings?.algolia?.read_only_api_key?
-							viewModel.showUserDetailsArea = true
-							viewModel.algolia_api_key = Settings.algolia.read_only_api_key
-							viewModel.algolia_app_id = Settings.algolia.app_id
-						else
-							viewModel.showUserDetailsArea = false
-
-						res.render 'project/list', viewModel
-						timer.done()
+		currentUser = AuthenticationController.getSessionUser(req)
+		async.parallel {
+			tags: (cb)->
+				TagsHandler.getAllTags user_id, cb
+			notifications: (cb)->
+				NotificationsHandler.getUserNotifications user_id, cb
+			projects: (cb)->
+				ProjectGetter.findAllUsersProjects user_id, 'name lastUpdated publicAccesLevel archived owner_ref', cb
+			hasSubscription: (cb)->
+				LimitationsManager.userHasSubscriptionOrIsGroupMember currentUser, cb
+			user: (cb) ->
+				User.findById user_id, "featureSwitches", cb
+			}, (err, results)->
+				if err?
+					logger.err err:err, "error getting data for project list page"
+					return next(err)
+				logger.log results:results, user_id:user_id, "rendering project list"
+				tags = results.tags[0]
+				notifications = require("underscore").map results.notifications, (notification)->
+					notification.html = req.i18n.translate(notification.templateKey, notification.messageOpts)
+					return notification
+				projects = ProjectController._buildProjectList results.projects[0], results.projects[1], results.projects[2]
+				user = results.user
+				ProjectController._injectProjectOwners projects, (error, projects) ->
+					return next(error) if error?
+
+					viewModel = {
+						title:'your_projects'
+						priority_title: true
+						projects: projects
+						tags: tags
+						notifications: notifications or []
+						user: user
+						hasSubscription: results.hasSubscription[0]
+					}
+
+					if Settings?.algolia?.app_id? and Settings?.algolia?.read_only_api_key?
+						viewModel.showUserDetailsArea = true
+						viewModel.algolia_api_key = Settings.algolia.read_only_api_key
+						viewModel.algolia_app_id = Settings.algolia.app_id
+					else
+						viewModel.showUserDetailsArea = false
+
+					res.render 'project/list', viewModel
+					timer.done()
 
 
 	loadEditor: (req, res, next)->

+ 3 - 4
services/web/app/coffee/Features/Referal/ReferalMiddleware.coffee

@@ -4,9 +4,8 @@ AuthenticationController = require('../Authentication/AuthenticationController')
 module.exports = RefererMiddleware =
 	getUserReferalId: (req, res, next) ->
 		if AuthenticationController.isUserLoggedIn()?
-			AuthenticationController.getLoggedInUser req, (error, user) ->
-				return next(error) if error?
-				req.user.referal_id = user.referal_id
-				next()
+			user = AuthenticationController.getSessionUser(req)
+			req.user.referal_id = user.referal_id
+			next()
 		else
 			next()

+ 161 - 173
services/web/app/coffee/Features/Subscription/SubscriptionController.coffee

@@ -32,168 +32,159 @@ module.exports = SubscriptionController =
 
 	#get to show the recurly.js page
 	paymentPage: (req, res, next) ->
-		AuthenticationController.getLoggedInUser req, (error, user) =>
-			return next(error) if error?
-			plan = PlansLocator.findLocalPlanInSettings(req.query.planCode)
-			LimitationsManager.userHasSubscription user, (err, hasSubscription)->
-				return next(err) if err?
-				if hasSubscription or !plan?
-					res.redirect "/user/subscription"
-				else
-					currency = req.query.currency?.toUpperCase()
-					GeoIpLookup.getCurrencyCode req.query?.ip || req.ip, (err, recomendedCurrency, countryCode)->
-						return next(err) if err?
-						if recomendedCurrency? and !currency?
-							currency = recomendedCurrency
-						RecurlyWrapper.sign {
-							subscription:
-								plan_code : req.query.planCode
-								currency: currency
-							account_code: user._id
-						}, (error, signature) ->
-							return next(error) if error?
-							res.render "subscriptions/new",
-								title      : "subscribe"
-								plan_code: req.query.planCode
+		user = AuthenticationController.getSessionUser(req)
+		plan = PlansLocator.findLocalPlanInSettings(req.query.planCode)
+		LimitationsManager.userHasSubscription user, (err, hasSubscription)->
+			return next(err) if err?
+			if hasSubscription or !plan?
+				res.redirect "/user/subscription"
+			else
+				currency = req.query.currency?.toUpperCase()
+				GeoIpLookup.getCurrencyCode req.query?.ip || req.ip, (err, recomendedCurrency, countryCode)->
+					return next(err) if err?
+					if recomendedCurrency? and !currency?
+						currency = recomendedCurrency
+					RecurlyWrapper.sign {
+						subscription:
+							plan_code : req.query.planCode
+							currency: currency
+						account_code: user._id
+					}, (error, signature) ->
+						return next(error) if error?
+						res.render "subscriptions/new",
+							title      : "subscribe"
+							plan_code: req.query.planCode
+							currency: currency
+							countryCode:countryCode
+							plan:plan
+							showStudentPlan: req.query.ssp
+							recurlyConfig: JSON.stringify
 								currency: currency
-								countryCode:countryCode
-								plan:plan
-								showStudentPlan: req.query.ssp
-								recurlyConfig: JSON.stringify
-									currency: currency
-									subdomain: Settings.apis.recurly.subdomain
-								showCouponField: req.query.scf
-								showVatField: req.query.svf
-								couponCode:      req.query.cc or ""
+								subdomain: Settings.apis.recurly.subdomain
+							showCouponField: req.query.scf
+							showVatField: req.query.svf
+							couponCode:      req.query.cc or ""
 
 
 
 	userSubscriptionPage: (req, res, next) ->
-		AuthenticationController.getLoggedInUser req, (error, user) =>
-			return next(error) if error?
-			LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
-				return next(err) if err?
-				groupLicenceInviteUrl = SubscriptionDomainHandler.getDomainLicencePage(user)
-				if subscription?.customAccount
-					logger.log user: user, "redirecting to custom account page"
-					res.redirect "/user/subscription/custom_account"
-				else if groupLicenceInviteUrl? and !hasSubOrIsGroupMember
-					logger.log user:user, "redirecting to group subscription invite page"
-					res.redirect groupLicenceInviteUrl
-				else if !hasSubOrIsGroupMember
-					logger.log user: user, "redirecting to plans"
-					res.redirect "/user/subscription/plans"
-				else
-					SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription, groupSubscriptions) ->
-						return next(error) if error?
-						logger.log user: user, subscription:subscription, hasSubOrIsGroupMember:hasSubOrIsGroupMember, groupSubscriptions:groupSubscriptions, "showing subscription dashboard"
-						plans = SubscriptionViewModelBuilder.buildViewModel()
-						res.render "subscriptions/dashboard",
-							title: "your_subscription"
-							recomendedCurrency: subscription?.currency
-							taxRate:subscription?.taxRate
-							plans: plans
-							subscription: subscription || {}
-							groupSubscriptions: groupSubscriptions
-							subscriptionTabActive: true
-							user:user
-							saved_billing_details: req.query.saved_billing_details?
+		user = AuthenticationController.getSessionUser(req)
+		LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
+			return next(err) if err?
+			groupLicenceInviteUrl = SubscriptionDomainHandler.getDomainLicencePage(user)
+			if subscription?.customAccount
+				logger.log user: user, "redirecting to custom account page"
+				res.redirect "/user/subscription/custom_account"
+			else if groupLicenceInviteUrl? and !hasSubOrIsGroupMember
+				logger.log user:user, "redirecting to group subscription invite page"
+				res.redirect groupLicenceInviteUrl
+			else if !hasSubOrIsGroupMember
+				logger.log user: user, "redirecting to plans"
+				res.redirect "/user/subscription/plans"
+			else
+				SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription, groupSubscriptions) ->
+					return next(error) if error?
+					logger.log user: user, subscription:subscription, hasSubOrIsGroupMember:hasSubOrIsGroupMember, groupSubscriptions:groupSubscriptions, "showing subscription dashboard"
+					plans = SubscriptionViewModelBuilder.buildViewModel()
+					res.render "subscriptions/dashboard",
+						title: "your_subscription"
+						recomendedCurrency: subscription?.currency
+						taxRate:subscription?.taxRate
+						plans: plans
+						subscription: subscription || {}
+						groupSubscriptions: groupSubscriptions
+						subscriptionTabActive: true
+						user:user
+						saved_billing_details: req.query.saved_billing_details?
 
 	userCustomSubscriptionPage: (req, res, next)->
-		AuthenticationController.getLoggedInUser req, (error, user) ->
-			return next(error) if error?
-			LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
-				return next(err) if err?
-				if !subscription?
-					err = new Error("subscription null for custom account, user:#{user?._id}")
-					logger.warn err:err, "subscription is null for custom accounts page"
-					return next(err)
-				res.render "subscriptions/custom_account",
-					title: "your_subscription"
-					subscription: subscription
+		user = AuthenticationController.getSessionUser(req)
+		LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
+			return next(err) if err?
+			if !subscription?
+				err = new Error("subscription null for custom account, user:#{user?._id}")
+				logger.warn err:err, "subscription is null for custom accounts page"
+				return next(err)
+			res.render "subscriptions/custom_account",
+				title: "your_subscription"
+				subscription: subscription
 
 
 	editBillingDetailsPage: (req, res, next) ->
-		AuthenticationController.getLoggedInUser req, (error, user) ->
-			return next(error) if error?
-			LimitationsManager.userHasSubscription user, (err, hasSubscription)->
-				return next(err) if err?
-				if !hasSubscription
-					res.redirect "/user/subscription"
-				else
-					RecurlyWrapper.sign {
-						account_code: user._id
-					}, (error, signature) ->
-						return next(error) if error?
-						res.render "subscriptions/edit-billing-details",
-							title      : "update_billing_details"
-							recurlyConfig: JSON.stringify
-								currency: "USD"
-								subdomain: Settings.apis.recurly.subdomain
-							signature  : signature
-							successURL : "#{Settings.siteUrl}/user/subscription/billing-details/update"
-							user       :
-								id : user._id
+		user = AuthenticationController.getSessionUser(req)
+		LimitationsManager.userHasSubscription user, (err, hasSubscription)->
+			return next(err) if err?
+			if !hasSubscription
+				res.redirect "/user/subscription"
+			else
+				RecurlyWrapper.sign {
+					account_code: user._id
+				}, (error, signature) ->
+					return next(error) if error?
+					res.render "subscriptions/edit-billing-details",
+						title      : "update_billing_details"
+						recurlyConfig: JSON.stringify
+							currency: "USD"
+							subdomain: Settings.apis.recurly.subdomain
+						signature  : signature
+						successURL : "#{Settings.siteUrl}/user/subscription/billing-details/update"
+						user       :
+							id : user._id
 
 	updateBillingDetails: (req, res, next) ->
 		res.redirect "/user/subscription?saved_billing_details=true"
 
 	createSubscription: (req, res, next)->
-		AuthenticationController.getLoggedInUser req, (error, user) ->
-			return callback(error) if error?
-			recurly_token_id = req.body.recurly_token_id
-			subscriptionDetails = req.body.subscriptionDetails
-			logger.log recurly_token_id: recurly_token_id, user_id:user._id, subscriptionDetails:subscriptionDetails, "creating subscription"
-			SubscriptionHandler.createSubscription user, subscriptionDetails, recurly_token_id, (err)->
-				if err?
-					logger.err err:err, user_id:user._id, "something went wrong creating subscription"
-					return res.sendStatus 500
-				res.sendStatus 201
+		user = AuthenticationController.getSessionUser(req)
+		recurly_token_id = req.body.recurly_token_id
+		subscriptionDetails = req.body.subscriptionDetails
+		logger.log recurly_token_id: recurly_token_id, user_id:user._id, subscriptionDetails:subscriptionDetails, "creating subscription"
+		SubscriptionHandler.createSubscription user, subscriptionDetails, recurly_token_id, (err)->
+			if err?
+				logger.err err:err, user_id:user._id, "something went wrong creating subscription"
+				return res.sendStatus 500
+			res.sendStatus 201
 
 	successful_subscription: (req, res, next)->
-		AuthenticationController.getLoggedInUser req, (error, user) =>
+		user = AuthenticationController.getSessionUser(req)
+		SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription) ->
 			return next(error) if error?
-			SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription) ->
-				return next(error) if error?
-				res.render "subscriptions/successful_subscription",
-					title: "thank_you"
-					subscription:subscription
+			res.render "subscriptions/successful_subscription",
+				title: "thank_you"
+				subscription:subscription
 
 	cancelSubscription: (req, res, next) ->
-		AuthenticationController.getLoggedInUser req, (error, user) ->
-			return next(error) if error?
-			logger.log user_id:user._id, "canceling subscription"
-			SubscriptionHandler.cancelSubscription user, (err)->
-				if err?
-					logger.err err:err, user_id:user._id, "something went wrong canceling subscription"
-					return next(err)
-				res.redirect "/user/subscription"
+		user = AuthenticationController.getSessionUser(req)
+		logger.log user_id:user._id, "canceling subscription"
+		SubscriptionHandler.cancelSubscription user, (err)->
+			if err?
+				logger.err err:err, user_id:user._id, "something went wrong canceling subscription"
+				return next(err)
+			res.redirect "/user/subscription"
 
 	updateSubscription: (req, res, next)->
 		_origin = req?.query?.origin || null
-		AuthenticationController.getLoggedInUser req, (error, user) ->
-			return next(error) if error?
-			planCode = req.body.plan_code
-			if !planCode?
-				err = new Error('plan_code is not defined')
-				logger.err {user_id: user._id, err, planCode, origin: _origin, body: req.body}, "[Subscription] error in updateSubscription form"
+		user = AuthenticationController.getSessionUser(req)
+		planCode = req.body.plan_code
+		if !planCode?
+			err = new Error('plan_code is not defined')
+			logger.err {user_id: user._id, err, planCode, origin: _origin, body: req.body}, "[Subscription] error in updateSubscription form"
+			return next(err)
+		logger.log planCode: planCode, user_id:user._id, "updating subscription"
+		SubscriptionHandler.updateSubscription user, planCode, null, (err)->
+			if err?
+				logger.err err:err, user_id:user._id, "something went wrong updating subscription"
 				return next(err)
-			logger.log planCode: planCode, user_id:user._id, "updating subscription"
-			SubscriptionHandler.updateSubscription user, planCode, null, (err)->
-				if err?
-					logger.err err:err, user_id:user._id, "something went wrong updating subscription"
-					return next(err)
-				res.redirect "/user/subscription"
+			res.redirect "/user/subscription"
 
 	reactivateSubscription: (req, res, next)->
-		AuthenticationController.getLoggedInUser req, (error, user) ->
-			return next(error) if error?
-			logger.log user_id:user._id, "reactivating subscription"
-			SubscriptionHandler.reactivateSubscription user, (err)->
-				if err?
-					logger.err err:err, user_id:user._id, "something went wrong reactivating subscription"
-					return next(err)
-				res.redirect "/user/subscription"
+		user = AuthenticationController.getSessionUser(req)
+		logger.log user_id:user._id, "reactivating subscription"
+		SubscriptionHandler.reactivateSubscription user, (err)->
+			if err?
+				logger.err err:err, user_id:user._id, "something went wrong reactivating subscription"
+				return next(err)
+			res.redirect "/user/subscription"
 
 	recurlyCallback: (req, res, next)->
 		logger.log data: req.body, "received recurly callback"
@@ -207,47 +198,44 @@ module.exports = SubscriptionController =
 			res.sendStatus 200
 
 	renderUpgradeToAnnualPlanPage: (req, res, next)->
-		AuthenticationController.getLoggedInUser req, (error, user) ->
-			return next(error) if error?
-			LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
-				return next(err) if err?
-				planCode = subscription?.planCode.toLowerCase()
-				if planCode?.indexOf("annual") != -1
-					planName = "annual"
-				else if planCode?.indexOf("student") != -1
-					planName = "student"
-				else if planCode?.indexOf("collaborator") != -1
-					planName = "collaborator"
-				if !hasSubscription
-					return res.redirect("/user/subscription/plans")
-				logger.log planName:planName, user_id:user._id, "rendering upgrade to annual page"
-				res.render "subscriptions/upgradeToAnnual",
-					title: "Upgrade to annual"
-					planName: planName
+		user = AuthenticationController.getSessionUser(req)
+		LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
+			return next(err) if err?
+			planCode = subscription?.planCode.toLowerCase()
+			if planCode?.indexOf("annual") != -1
+				planName = "annual"
+			else if planCode?.indexOf("student") != -1
+				planName = "student"
+			else if planCode?.indexOf("collaborator") != -1
+				planName = "collaborator"
+			if !hasSubscription
+				return res.redirect("/user/subscription/plans")
+			logger.log planName:planName, user_id:user._id, "rendering upgrade to annual page"
+			res.render "subscriptions/upgradeToAnnual",
+				title: "Upgrade to annual"
+				planName: planName
 
 	processUpgradeToAnnualPlan: (req, res, next)->
-		AuthenticationController.getLoggedInUser req, (error, user) ->
-			return next(error) if error?
-			{planName} = req.body
-			coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
-			annualPlanName = "#{planName}-annual"
-			logger.log user_id:user._id, planName:annualPlanName, "user is upgrading to annual billing with discount"
-			SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
-				if err?
-					logger.err err:err, user_id:user._id, "error updating subscription"
-					return next(err)
-				res.sendStatus 200
+		user = AuthenticationController.getSessionUser(req)
+		{planName} = req.body
+		coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
+		annualPlanName = "#{planName}-annual"
+		logger.log user_id:user._id, planName:annualPlanName, "user is upgrading to annual billing with discount"
+		SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
+			if err?
+				logger.err err:err, user_id:user._id, "error updating subscription"
+				return next(err)
+			res.sendStatus 200
 
 	extendTrial: (req, res, next)->
-		AuthenticationController.getLoggedInUser req, (error, user) ->
-			return next(error) if error?
-			LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
-				return next(err) if err?
-				SubscriptionHandler.extendTrial subscription, 14, (err)->
-					if err?
-						res.send 500
-					else
-						res.send 200
+		user = AuthenticationController.getSessionUser(req)
+		LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
+			return next(err) if err?
+			SubscriptionHandler.extendTrial subscription, 14, (err)->
+				if err?
+					res.send 500
+				else
+					res.send 200
 
 	recurlyNotificationParser: (req, res, next) ->
 		xml = ""

+ 11 - 11
services/web/app/coffee/Features/Subscription/SubscriptionGroupController.coffee

@@ -78,18 +78,18 @@ module.exports =
 
 	beginJoinGroup: (req, res)->
 		subscription_id = req.params.subscription_id
-		AuthenticationController.getLoggedInUser req, (err, currentUser) ->
+		currentUser = AuthenticationController.getSessionUser(req)
+		if !currentUser?
+			logger.err {subscription_id}, "error getting current user"
+			return res.sendStatus 500
+		licence = SubscriptionDomainHandler.findDomainLicenceBySubscriptionId(subscription_id)
+		if !licence?
+			return ErrorsController.notFound(req, res)
+		SubscriptionGroupHandler.sendVerificationEmail subscription_id, licence.name, currentUser.email, (err)->
 			if err?
-				logger.err {subscription_id}, "error getting current user"
-				return res.sendStatus 500
-			licence = SubscriptionDomainHandler.findDomainLicenceBySubscriptionId(subscription_id)
-			if !licence?
-				return ErrorsController.notFound(req, res)
-			SubscriptionGroupHandler.sendVerificationEmail subscription_id, licence.name, currentUser.email, (err)->
-				if err?
-					res.sendStatus 500
-				else
-					res.sendStatus 200
+				res.sendStatus 500
+			else
+				res.sendStatus 200
 
 	completeJoin: (req, res)->
 		subscription_id = req.params.subscription_id

+ 7 - 7
services/web/app/coffee/Features/User/UserController.coffee

@@ -85,13 +85,13 @@ module.exports = UserController =
 		metrics.inc "user.logout"
 		logger.log user: req?.session?.user, "logging out"
 		sessionId = req.sessionID
-		AuthenticationController.getLoggedInUser req, (err, user) ->
-			req.logout?()  # passport logout
-			req.session.destroy (err)->
-				if err
-					logger.err err: err, 'error destorying session'
-				UserSessionsManager.untrackSession(user, sessionId)
-				res.redirect '/login'
+		user = AuthenticationController.getSessionUser(req)
+		req.logout?()  # passport logout
+		req.session.destroy (err)->
+			if err
+				logger.err err: err, 'error destorying session'
+			UserSessionsManager.untrackSession(user, sessionId)
+			res.redirect '/login'
 
 	register : (req, res, next = (error) ->)->
 		email = req.body.email