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
@ -161,8 +161,8 @@ class Profile(CreatedModifiedModel):
|
|||||||
class UserSessionMembership(BaseModel):
|
class UserSessionMembership(BaseModel):
|
||||||
'''
|
'''
|
||||||
A lookup table for API session membership given user. Note, there is a
|
A lookup table for API session membership given user. Note, there is a
|
||||||
different session created by channels for websockets using the same
|
different session created by channels for websockets using the same
|
||||||
underlying model.
|
underlying model.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -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')
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -680,7 +679,7 @@ def create_access_token_user_if_missing(sender, **kwargs):
|
|||||||
post_save.connect(create_access_token_user_if_missing, sender=OAuth2AccessToken)
|
post_save.connect(create_access_token_user_if_missing, sender=OAuth2AccessToken)
|
||||||
|
|
||||||
|
|
||||||
# Connect the Instance Group to Activity Stream receivers.
|
# Connect the Instance Group to Activity Stream receivers.
|
||||||
post_save.connect(activity_stream_create, sender=InstanceGroup, dispatch_uid=str(InstanceGroup) + "_create")
|
post_save.connect(activity_stream_create, sender=InstanceGroup, dispatch_uid=str(InstanceGroup) + "_create")
|
||||||
pre_save.connect(activity_stream_update, sender=InstanceGroup, dispatch_uid=str(InstanceGroup) + "_update")
|
pre_save.connect(activity_stream_update, sender=InstanceGroup, dispatch_uid=str(InstanceGroup) + "_update")
|
||||||
pre_delete.connect(activity_stream_delete, sender=InstanceGroup, dispatch_uid=str(InstanceGroup) + "_delete")
|
pre_delete.connect(activity_stream_delete, sender=InstanceGroup, dispatch_uid=str(InstanceGroup) + "_delete")
|
||||||
|
Loading…
Reference in New Issue
Block a user