mirror of
https://github.com/ansible/awx.git
synced 2024-11-01 08:21:15 +03:00
Avoid unnecessary user get expiring session memberships
This commit is contained in:
parent
ab1e45d6c4
commit
1223148116
@ -177,14 +177,14 @@ class UserSessionMembership(BaseModel):
|
|||||||
created = models.DateTimeField(default=None, editable=False)
|
created = models.DateTimeField(default=None, editable=False)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_memberships_over_limit(user, now=None):
|
def get_memberships_over_limit(user_id, now=None):
|
||||||
if settings.SESSIONS_PER_USER == -1:
|
if settings.SESSIONS_PER_USER == -1:
|
||||||
return []
|
return []
|
||||||
if now is None:
|
if now is None:
|
||||||
now = tz_now()
|
now = tz_now()
|
||||||
query_set = UserSessionMembership.objects\
|
query_set = UserSessionMembership.objects\
|
||||||
.select_related('session')\
|
.select_related('session')\
|
||||||
.filter(user=user)\
|
.filter(user_id=user_id)\
|
||||||
.order_by('-created')
|
.order_by('-created')
|
||||||
non_expire_memberships = [x for x in query_set if x.session.expire_date > now]
|
non_expire_memberships = [x for x in query_set if x.session.expire_date > now]
|
||||||
return non_expire_memberships[settings.SESSIONS_PER_USER:]
|
return non_expire_memberships[settings.SESSIONS_PER_USER:]
|
||||||
|
@ -652,20 +652,19 @@ def save_user_session_membership(sender, **kwargs):
|
|||||||
return
|
return
|
||||||
if not session:
|
if not session:
|
||||||
return
|
return
|
||||||
user = session.get_decoded().get(SESSION_KEY, None)
|
user_id = session.get_decoded().get(SESSION_KEY, None)
|
||||||
if not user:
|
if not user_id:
|
||||||
return
|
return
|
||||||
user = User.objects.get(pk=user)
|
if UserSessionMembership.objects.filter(user=user_id, session=session).exists():
|
||||||
if UserSessionMembership.objects.filter(user=user, session=session).exists():
|
|
||||||
return
|
return
|
||||||
UserSessionMembership(user=user, session=session, created=timezone.now()).save()
|
UserSessionMembership(user_id=user_id, session=session, created=timezone.now()).save()
|
||||||
expired = UserSessionMembership.get_memberships_over_limit(user)
|
expired = UserSessionMembership.get_memberships_over_limit(user_id)
|
||||||
for membership in expired:
|
for membership in expired:
|
||||||
Session.objects.filter(session_key__in=[membership.session_id]).delete()
|
Session.objects.filter(session_key__in=[membership.session_id]).delete()
|
||||||
membership.delete()
|
membership.delete()
|
||||||
if len(expired):
|
if len(expired):
|
||||||
consumers.emit_channel_notification(
|
consumers.emit_channel_notification(
|
||||||
'control-limit_reached_{}'.format(user.pk),
|
'control-limit_reached_{}'.format(user_id),
|
||||||
dict(group_name='control', reason='limit_reached')
|
dict(group_name='control', reason='limit_reached')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user