move call for creating ip matched notifcation to project controller

This commit is contained in:
hugh-obrien 2018-09-05 15:28:26 +01:00
parent 5605e1c5c3
commit 8ef90a0dcb
7 changed files with 32 additions and 42 deletions

View file

@ -73,7 +73,6 @@ module.exports = AuthenticationController =
finishLogin: (user, req, res, next) ->
redir = AuthenticationController._getRedirectFromSession(req) || "/project"
AuthenticationController._loginAsyncHandlers(req, user)
AuthenticationController.ipMatchCheck(req, user)
AuthenticationController.afterLoginSessionSetup req, user, (err) ->
if err?
return next(err)
@ -114,6 +113,7 @@ module.exports = AuthenticationController =
UserHandler.setupLoginData(user, ()->)
LoginRateLimiter.recordSuccessfulLogin(user.email)
AuthenticationController._recordSuccessfulLogin(user._id)
AuthenticationController.ipMatchCheck(req, user)
Analytics.recordEvent(user._id, "user-logged-in", {ip:req.ip})
Analytics.identifyUser(user._id, req.sessionID)
logger.log email: user.email, user_id: user._id.toString(), "successful log in"

View file

@ -46,7 +46,7 @@ module.exports =
timeout: 20 * 1000
}, (error, response, body) ->
return error if error?
return null if response.statusCode == 204
return null unless response.statusCode == 200
messageOpts =
university_id: body.id

View file

@ -1,31 +1,12 @@
NotificationsHandler = require("./NotificationsHandler")
NotificationsBuilder = require("./NotificationsBuilder")
AuthenticationController = require("../Authentication/AuthenticationController")
Settings = require 'settings-sharelatex'
UserGetter = require("../User/UserGetter")
logger = require("logger-sharelatex")
async = require "async"
_ = require("underscore")
module.exports =
getAllUnreadNotifications: (req, res)->
ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress
user_id = AuthenticationController.getLoggedInUserId(req)
# in v2 add notifications for matching university IPs
if Settings.overleaf?
UserGetter.getUser user_id, { 'lastLoginIp': 1 }, (error, user) ->
if ip != user.lastLoginIp
async.series ([
() ->
NotificationsBuilder.ipMatcherAffiliation(user_id, ip).create((err) ->
return err
)
])
NotificationsHandler.getUserNotifications user_id, (err, unreadNotifications)->
unreadNotifications = _.map unreadNotifications, (notification)->
notification.html = req.i18n.translate(notification.templateKey, notification.messageOpts)

View file

@ -26,6 +26,8 @@ TokenAccessHandler = require '../TokenAccess/TokenAccessHandler'
CollaboratorsHandler = require '../Collaborators/CollaboratorsHandler'
Modules = require '../../infrastructure/Modules'
ProjectEntityHandler = require './ProjectEntityHandler'
UserGetter = require("../User/UserGetter")
NotificationsBuilder = require("../Notifications/NotificationsBuilder")
crypto = require 'crypto'
{ V1ConnectionError } = require '../Errors/Errors'
Features = require('../../infrastructure/Features')
@ -209,6 +211,16 @@ module.exports = ProjectController =
user = results.user
warnings = ProjectController._buildWarningsList results.v1Projects
# in v2 add notifications for matching university IPs
if Settings.overleaf?
ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress
UserGetter.getUser user_id, { 'lastLoginIp': 1 }, (error, user) ->
if ip != user.lastLoginIp
NotificationsBuilder.ipMatcherAffiliation(user._id, ip).create((err) ->
return err
)
ProjectController._injectProjectOwners projects, (error, projects) ->
return next(error) if error?

View file

@ -15,7 +15,7 @@ describe "AuthenticationController", ->
tk.freeze(Date.now())
@AuthenticationController = SandboxedModule.require modulePath, requires:
"./AuthenticationManager": @AuthenticationManager = {}
"../User/UserUpdater" : @UserUpdater = {}
"../User/UserUpdater" : @UserUpdater = {updateUser:sinon.stub()}
"metrics-sharelatex": @Metrics = { inc: sinon.stub() }
"../Security/LoginRateLimiter": @LoginRateLimiter = { processLoginRequest:sinon.stub(), recordSuccessfulLogin:sinon.stub() }
"../User/UserHandler": @UserHandler = {setupLoginData:sinon.stub()}
@ -603,7 +603,6 @@ describe "AuthenticationController", ->
@AuthenticationController._loginAsyncHandlers = sinon.stub()
@AuthenticationController.afterLoginSessionSetup = sinon.stub().callsArgWith(2, null)
@AuthenticationController._clearRedirectFromSession = sinon.stub()
@UserUpdater.updateUser = sinon.stub()
@req.headers = {accept: 'application/json, whatever'}
@res.json = sinon.stub()
@res.redirect = sinon.stub()

View file

@ -13,16 +13,7 @@ describe 'NotificationsController', ->
@handler =
getUserNotifications: sinon.stub().callsArgWith(1)
markAsRead: sinon.stub().callsArgWith(2)
@builder =
ipMatcherAffiliation: sinon.stub().returns({create: sinon.stub()})
@userGetter =
getUser: sinon.stub().callsArgWith 2, null, {lastLoginIp: '192.170.18.2'}
@settings =
apis: { v1: { url: 'v1.url', user: '', pass: '' } }
@req =
headers: {}
connection:
remoteAddress: "192.170.18.1"
params:
notification_id:notification_id
session:
@ -34,9 +25,6 @@ describe 'NotificationsController', ->
getLoggedInUserId: sinon.stub().returns(@req.session.user._id)
@controller = SandboxedModule.require modulePath, requires:
"./NotificationsHandler":@handler
"./NotificationsBuilder":@builder
"../User/UserGetter": @userGetter
"settings-sharelatex":@settings
"underscore":@underscore =
map:(arr)-> return arr
'logger-sharelatex':
@ -52,13 +40,7 @@ describe 'NotificationsController', ->
@handler.getUserNotifications.calledWith(user_id).should.equal true
done()
it 'should send a remove request when notification read', (done)->
it 'should send a delete request when a delete has been received to mark a notification', (done)->
@controller.markNotificationAsRead @req, send:=>
@handler.markAsRead.calledWith(user_id, notification_id).should.equal true
done()
it 'should call the builder with the user ip if v2', (done)->
@settings.overleaf = true
@controller.getAllUnreadNotifications @req, send:(body)=>
@builder.ipMatcherAffiliation.calledWith(user_id, @req.connection.remoteAddress).should.equal true
done()

View file

@ -69,6 +69,10 @@ describe "ProjectController", ->
@CollaboratorsHandler =
userIsTokenMember: sinon.stub().callsArgWith(2, null, false)
@ProjectEntityHandler = {}
@NotificationBuilder =
ipMatcherAffiliation: sinon.stub().returns({create: sinon.stub()})
@UserGetter =
getUser: sinon.stub().callsArgWith 2, null, {lastLoginIp: '192.170.18.2'}
@Modules =
hooks:
fire: sinon.stub()
@ -105,11 +109,16 @@ describe "ProjectController", ->
"./ProjectEntityHandler": @ProjectEntityHandler
"../Errors/Errors": Errors
"../../infrastructure/Features": @Features
"../Notifications/NotificationsBuilder":@NotificationBuilder
"../User/UserGetter": @UserGetter
@projectName = "£12321jkj9ujkljds"
@req =
params:
Project_id: @project_id
headers: {}
connection:
remoteAddress: "192.170.18.1"
session:
user: @user
body:
@ -301,6 +310,13 @@ describe "ProjectController", ->
done()
@ProjectController.projectListPage @req, @res
it "should create trigger ip matcher notifications", (done)->
@settings.overleaf = true
@res.render = (pageName, opts)=>
@NotificationBuilder.ipMatcherAffiliation.called.should.equal true
done()
@ProjectController.projectListPage @req, @res
it "should send the projects", (done)->
@res.render = (pageName, opts)=>
opts.projects.length.should.equal (@projects.length + @collabertions.length + @readOnly.length + @tokenReadAndWrite.length + @tokenReadOnly.length)