WIP: display sessions on a page

This commit is contained in:
Shane Kilkelly 2016-10-06 15:49:47 +01:00
parent 95a66bbf00
commit 894c549ec6
4 changed files with 51 additions and 0 deletions

View file

@ -1,5 +1,6 @@
UserLocator = require("./UserLocator")
UserGetter = require("./UserGetter")
UserSessionsManager = require("./UserSessionsManager")
ErrorController = require("../Errors/ErrorController")
logger = require("logger-sharelatex")
Settings = require("settings-sharelatex")
@ -63,3 +64,11 @@ module.exports =
user: user,
languages: Settings.languages,
accountSettingsTabActive: true
sessionsPage: (req, res, next) ->
user_id = AuthenticationController.getLoggedInUserId(req)
logger.log user: user_id, "loading settings page"
UserSessionsManager.getAllUserSessions user_id, (err, sessions) ->
res.render 'user/sessions',
title: "sessions"
sessions: sessions

View file

@ -3,6 +3,7 @@ redis = require('redis-sharelatex')
logger = require("logger-sharelatex")
Async = require('async')
_ = require('underscore')
crypto = require('crypto')
rclient = redis.createClient(Settings.redis.web)
@ -55,6 +56,31 @@ module.exports = UserSessionsManager =
UserSessionsManager._checkSessions(user, () ->)
callback()
getAllUserSessions: (user_id, callback=(err, sessionKeys)->) ->
sessionSetKey = UserSessionsManager._sessionSetKey({_id: user_id})
rclient.smembers sessionSetKey, (err, sessionKeys) ->
if err?
logger.err {user_id}, "error getting all session keys for user from redis"
return callback(err)
rclient.mget sessionKeys, (err, sessions) ->
if err?
logger.err {user_id}, "error getting all sessions for user from redis"
return callback(err)
hashedSessionKeys = sessionKeys.map (key) ->
crypto.createHash('md5').update(key).digest('hex')
expiries = sessions.map (s) ->
if s == null
return null
s = JSON.parse(s)
s?.user?.session_created or s?.passport?.user?.session_created
pairs = _.zip(hashedSessionKeys, expiries)
result = []
for pair in pairs
result.push {id: pair[0], expires: pair[1]}
console.log ">> result:", result
return callback(null, result)
revokeAllUserSessions: (user, retain, callback=(err)->) ->
if !retain
retain = []

View file

@ -88,6 +88,8 @@ module.exports = class Router
webRouter.post '/user/settings', AuthenticationController.requireLogin(), UserController.updateUserSettings
webRouter.post '/user/password/update', AuthenticationController.requireLogin(), UserController.changePassword
webRouter.get '/user/sessions', AuthenticationController.requireLogin(), UserPagesController.sessionsPage
webRouter.delete '/user/newsletter/unsubscribe', AuthenticationController.requireLogin(), UserController.unsubscribe
webRouter.delete '/user', AuthenticationController.requireLogin(), UserController.deleteUser

View file

@ -0,0 +1,14 @@
extends ../layout
block content
.content.content-alt
.container
.row
.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
.card
.page-header
h1 #{translate("your_sessions")}
.sessions-list
each session in sessions
div.session
| #{session.id} - #{session.expires}