2015-08-19 20:53:55 +02:00
|
|
|
|
2017-03-08 11:46:12 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from django.conf import settings
|
2015-08-19 20:53:55 +02:00
|
|
|
from django.contrib.auth import SESSION_KEY, get_user_model
|
2016-06-04 19:52:48 +02:00
|
|
|
from django.contrib.sessions.models import Session
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-03-08 11:46:12 +01:00
|
|
|
from importlib import import_module
|
2018-05-10 19:13:36 +02:00
|
|
|
from typing import List, Mapping, Optional
|
2016-06-04 20:38:42 +02:00
|
|
|
|
2017-03-08 11:59:59 +01:00
|
|
|
from zerver.models import Realm, UserProfile, get_user_profile_by_id
|
2017-03-08 11:46:12 +01:00
|
|
|
|
|
|
|
session_engine = import_module(settings.SESSION_ENGINE)
|
2015-08-19 20:53:55 +02:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_session_dict_user(session_dict: Mapping[str, int]) -> Optional[int]:
|
2015-08-19 20:53:55 +02:00
|
|
|
# Compare django.contrib.auth._get_user_session_key
|
|
|
|
try:
|
|
|
|
return get_user_model()._meta.pk.to_python(session_dict[SESSION_KEY])
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_session_user(session: Session) -> Optional[int]:
|
2015-08-19 20:53:55 +02:00
|
|
|
return get_session_dict_user(session.get_decoded())
|
2017-03-08 11:46:12 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def user_sessions(user_profile: UserProfile) -> List[Session]:
|
2017-03-08 11:46:12 +01:00
|
|
|
return [s for s in Session.objects.all()
|
|
|
|
if get_session_user(s) == user_profile.id]
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def delete_session(session: Session) -> None:
|
2017-05-07 17:11:41 +02:00
|
|
|
session_engine.SessionStore(session.session_key).delete() # type: ignore # import_module
|
2017-03-08 11:46:12 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def delete_user_sessions(user_profile: UserProfile) -> None:
|
2017-03-08 11:46:12 +01:00
|
|
|
for session in Session.objects.all():
|
|
|
|
if get_session_user(session) == user_profile.id:
|
|
|
|
delete_session(session)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def delete_realm_user_sessions(realm: Realm) -> None:
|
2017-03-08 11:46:12 +01:00
|
|
|
realm_user_ids = [user_profile.id for user_profile in
|
|
|
|
UserProfile.objects.filter(realm=realm)]
|
2017-04-15 04:03:56 +02:00
|
|
|
for session in Session.objects.filter(expire_date__gte=timezone_now()):
|
2017-03-08 11:46:12 +01:00
|
|
|
if get_session_user(session) in realm_user_ids:
|
|
|
|
delete_session(session)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def delete_all_user_sessions() -> None:
|
2017-03-08 11:46:12 +01:00
|
|
|
for session in Session.objects.all():
|
|
|
|
delete_session(session)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def delete_all_deactivated_user_sessions() -> None:
|
2017-03-08 11:46:12 +01:00
|
|
|
for session in Session.objects.all():
|
|
|
|
user_profile_id = get_session_user(session)
|
|
|
|
if user_profile_id is None:
|
2018-11-15 00:28:35 +01:00
|
|
|
continue # nocoverage # to debug
|
2017-03-08 11:46:12 +01:00
|
|
|
user_profile = get_user_profile_by_id(user_profile_id)
|
|
|
|
if not user_profile.is_active or user_profile.realm.deactivated:
|
|
|
|
logging.info("Deactivating session for deactivated user %s" % (user_profile.email,))
|
|
|
|
delete_session(session)
|