Make it possible to list and invalidate active sessions

This commit is contained in:
2018-07-19 15:14:00 +02:00
parent 8d71edae5e
commit cb9a62cd88
4 changed files with 68 additions and 1 deletions

View File

@@ -27,6 +27,7 @@ from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy_utils.types.choice import ChoiceType
from .cache import cache
from .utils import force_locale
db = SQLAlchemy()
@@ -219,6 +220,21 @@ class User(db.Model, UserMixin):
return current_app.timezone
@property
def session_list_key(self):
"""The cache key of this users session list
"""
return f'open_sessions:{self.id}'
@property
def active_sessions(self):
return cache.get(self.session_list_key) or []
@active_sessions.setter
def active_sessions(self, value):
cache.set(self.session_list_key, list(value))
def __repr__(self):
return f'<User {self.id}({self.username})>'