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):
|
||||
'''
|
||||
A lookup table for API session membership given user. Note, there is a
|
||||
different session created by channels for websockets using the same
|
||||
underlying model.
|
||||
different session created by channels for websockets using the same
|
||||
underlying model.
|
||||
'''
|
||||
|
||||
class Meta:
|
||||
@ -177,14 +177,14 @@ class UserSessionMembership(BaseModel):
|
||||
created = models.DateTimeField(default=None, editable=False)
|
||||
|
||||
@staticmethod
|
||||
def get_memberships_over_limit(user, now=None):
|
||||
def get_memberships_over_limit(user_id, now=None):
|
||||
if settings.SESSIONS_PER_USER == -1:
|
||||
return []
|
||||
if now is None:
|
||||
now = tz_now()
|
||||
query_set = UserSessionMembership.objects\
|
||||
.select_related('session')\
|
||||
.filter(user=user)\
|
||||
.filter(user_id=user_id)\
|
||||
.order_by('-created')
|
||||
non_expire_memberships = [x for x in query_set if x.session.expire_date > now]
|
||||
return non_expire_memberships[settings.SESSIONS_PER_USER:]
|
||||
|
@ -652,20 +652,19 @@ def save_user_session_membership(sender, **kwargs):
|
||||
return
|
||||
if not session:
|
||||
return
|
||||
user = session.get_decoded().get(SESSION_KEY, None)
|
||||
if not user:
|
||||
user_id = session.get_decoded().get(SESSION_KEY, None)
|
||||
if not user_id:
|
||||
return
|
||||
user = User.objects.get(pk=user)
|
||||
if UserSessionMembership.objects.filter(user=user, session=session).exists():
|
||||
if UserSessionMembership.objects.filter(user=user_id, session=session).exists():
|
||||
return
|
||||
UserSessionMembership(user=user, session=session, created=timezone.now()).save()
|
||||
expired = UserSessionMembership.get_memberships_over_limit(user)
|
||||
UserSessionMembership(user_id=user_id, session=session, created=timezone.now()).save()
|
||||
expired = UserSessionMembership.get_memberships_over_limit(user_id)
|
||||
for membership in expired:
|
||||
Session.objects.filter(session_key__in=[membership.session_id]).delete()
|
||||
membership.delete()
|
||||
if len(expired):
|
||||
consumers.emit_channel_notification(
|
||||
'control-limit_reached_{}'.format(user.pk),
|
||||
'control-limit_reached_{}'.format(user_id),
|
||||
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)
|
||||
|
||||
|
||||
# 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")
|
||||
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")
|
||||
|
Loading…
Reference in New Issue
Block a user