2017-05-10 05:05:48 -04:00
|
|
|
RedisWrapper = require('../../infrastructure/RedisWrapper')
|
|
|
|
rclient = RedisWrapper.client('sudomode')
|
|
|
|
logger = require('logger-sharelatex')
|
2018-09-19 06:53:00 -04:00
|
|
|
AuthenticationManager = require '../Authentication/AuthenticationManager'
|
2018-09-20 09:59:30 -04:00
|
|
|
Settings = require 'settings-sharelatex'
|
|
|
|
V1Handler = require '../V1/V1Handler'
|
|
|
|
UserGetter = require '../User/UserGetter'
|
2017-05-10 05:05:48 -04:00
|
|
|
|
|
|
|
|
2017-05-10 06:35:47 -04:00
|
|
|
TIMEOUT_IN_SECONDS = 60 * 60
|
2017-05-10 05:05:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = SudoModeHandler =
|
|
|
|
|
|
|
|
_buildKey: (userId) ->
|
|
|
|
"SudoMode:{#{userId}}"
|
|
|
|
|
2018-09-19 06:53:00 -04:00
|
|
|
authenticate: (email, password, callback=(err, user)->) ->
|
2018-09-20 09:59:30 -04:00
|
|
|
if Settings.overleaf?
|
|
|
|
V1Handler.authWithV1 email, password, (err, isValid, v1Profile) ->
|
|
|
|
if !isValid
|
|
|
|
return callback(null, null)
|
|
|
|
UserGetter.getUser {'overleaf.id': v1Profile.id}, callback
|
|
|
|
else
|
|
|
|
AuthenticationManager.authenticate {email}, password, callback
|
2018-09-19 06:53:00 -04:00
|
|
|
|
2017-05-10 05:05:48 -04:00
|
|
|
activateSudoMode: (userId, callback=(err)->) ->
|
|
|
|
if !userId?
|
2017-05-10 06:36:05 -04:00
|
|
|
return callback(new Error('[SudoMode] user must be supplied'))
|
2017-05-10 05:05:48 -04:00
|
|
|
duration = TIMEOUT_IN_SECONDS
|
|
|
|
logger.log {userId, duration}, "[SudoMode] activating sudo mode for user"
|
|
|
|
rclient.set SudoModeHandler._buildKey(userId), '1', 'EX', duration, callback
|
|
|
|
|
2017-05-15 06:53:52 -04:00
|
|
|
clearSudoMode: (userId, callback=(err)->) ->
|
|
|
|
if !userId?
|
|
|
|
return callback(new Error('[SudoMode] user must be supplied'))
|
|
|
|
logger.log {userId}, "[SudoMode] clearing sudo mode for user"
|
|
|
|
rclient.del SudoModeHandler._buildKey(userId), callback
|
|
|
|
|
2017-05-10 05:05:48 -04:00
|
|
|
isSudoModeActive: (userId, callback=(err, isActive)->) ->
|
|
|
|
if !userId?
|
2017-05-10 06:36:05 -04:00
|
|
|
return callback(new Error('[SudoMode] user must be supplied'))
|
2017-05-10 05:05:48 -04:00
|
|
|
rclient.get SudoModeHandler._buildKey(userId), (err, result) ->
|
|
|
|
if err?
|
|
|
|
return callback(err)
|
|
|
|
callback(null, result == '1')
|