Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log out a user when user is not active #35143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jingcheng16
Copy link
Contributor

@jingcheng16 jingcheng16 commented Sep 18, 2024

Product Description

When a user signed in, even if the user get deactivated, as long as the user is still in his session, he can still use commcarehq, which sounds unsafe.

This PR will make sure, after the user is deactivated, the next request he sent, will log him out.

I know it is not user friendly enough, so I created a follow up ticket: https://dimagi.atlassian.net/browse/SAAS-16009. We want to roll this out sooner to solve the issue for CRS.

Technical Summary

Ticket: https://dimagi.atlassian.net/browse/SAAS-15996

I modified the UsersMiddleWare that checks if a user is active during each request

logout function is used in Django documentation: https://docs.djangoproject.com/en/4.2/topics/auth/default/#how-to-log-a-user-out

Safety Assurance

Safety story

This ticket improved safety as we only allow activated user access hq.
Tested locally and on staging.

Automated test coverage

QA Plan

No QA.

Rollback instructions

  • This PR can be reverted after deploy with no further considerations

Labels & Review

  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

@jingcheng16 jingcheng16 added the product/prod-india-all-users Change will only be deployed to Dimagi "SaaS" environments label Sep 18, 2024
@jingcheng16 jingcheng16 marked this pull request as ready for review September 18, 2024 20:27
gherceg
gherceg previously approved these changes Sep 18, 2024
Copy link
Contributor

@gherceg gherceg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too concerned about the user experience since this doesn't change any existing behavior apart from ending a user's session early if they are no longer active. The bad user experience for deactivated users already exists which is that if they try to login, it fails with a message saying their credentials were incorrect is displayed, so it is fair to say that is outside of the scope of this change.

@@ -15,6 +16,9 @@ def process_view(self, request, view_func, view_args, view_kwargs):
if 'org' in view_kwargs:
request.org = view_kwargs['org']
if request.user and request.user.is_authenticated:
if not request.user.is_active:
logout(request)
return self.get_response(request)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is actually what we want in the long run based on docs, as it seems by calling self.get_response(request) we will continue to pass the request down the middleware chain. Since the user has been logged out, I imagine it hits a point where an authenticated session is required and returns a response then. This doesn't seem too risky, and the overall change is still an improvement so I don't consider this blocking, but would like to see this potentially addressed if needed in the future.

@gherceg gherceg dismissed their stale review September 18, 2024 21:43

Dismissing my own review as I think we need to understand sef.get_response(request) better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product/prod-india-all-users Change will only be deployed to Dimagi "SaaS" environments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants