-
Notifications
You must be signed in to change notification settings - Fork 530
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
Feature: Added an account disabled page #3391
base: main
Are you sure you want to change the base?
Changes from all commits
d76b3f9
3ac7caa
3ef5bd3
4feaf25
84ce15c
88316bd
ac0fcc1
b153657
bddfb0f
4ce40e6
919d0fd
08710a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
from raygun4py.middleware.django import Provider | ||
|
||
from django.conf import settings | ||
from django.contrib.auth.middleware import get_user | ||
from django.contrib.auth.models import User | ||
from django.core.cache import cache | ||
from django.core.exceptions import PermissionDenied | ||
from django.http import Http404, HttpResponseForbidden | ||
|
@@ -149,3 +151,42 @@ def __call__(self, request): | |
cache.set(observed_key, (1, now), self.observation_period) | ||
|
||
return response | ||
|
||
|
||
class AccountDisabledMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
# Manually fetch user from the session | ||
request.user = get_user(request) | ||
user = request.user | ||
|
||
# If user is authenticated, check if they are inactive | ||
if user.is_authenticated: | ||
if not user.is_active: | ||
return render( | ||
request, | ||
"account_disabled.html", | ||
{"DEFAULT_FROM_EMAIL": settings.DEFAULT_FROM_EMAIL}, | ||
status=403, | ||
) | ||
else: | ||
# For non-authenticated users, check the session manually | ||
user_id = request.session.get("_auth_user_id") | ||
if user_id: | ||
try: | ||
user = User.objects.get(pk=user_id) | ||
if not user.is_active: | ||
return render( | ||
request, | ||
"account_disabled.html", | ||
{"DEFAULT_FROM_EMAIL": settings.DEFAULT_FROM_EMAIL}, | ||
status=403, | ||
) | ||
except User.DoesNotExist: | ||
pass # If the user ID is invalid, ignore it | ||
Comment on lines
+175
to
+188
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly does this code do? If the user is not authenticated, we shouldn't do anything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mathjazz , I was having trouble running the code without this patch, mainly when the user is not active and wishes to login, it does not pass through the did some digging and found that one of the requirements for a user to be authenticated is that the user must be active I tried checking if both So I updated the patch to have two parts just in case:
the path highlighted presents the second part of the patch, |
||
|
||
# Continue processing the request | ||
response = self.get_response(request) | ||
return response |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{% extends "404.html" %} | ||
|
||
{% block title %}Account Disabled{% endblock %} | ||
{% block description %} | ||
Your account has been disabled. If you believe this is a mistake or need further assistance, please contact us at {{ DEFAULT_FROM_EMAIL }}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make the email address a |
||
{% endblock %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add the new line at the end of the file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -328,6 +328,7 @@ def _default_from_email(): | |
"corsheaders.middleware.CorsMiddleware", | ||
"django.middleware.common.CommonMiddleware", | ||
"django.contrib.sessions.middleware.SessionMiddleware", | ||
"pontoon.base.middleware.AccountDisabledMiddleware", | ||
"django.contrib.auth.middleware.AuthenticationMiddleware", | ||
"pontoon.base.middleware.ThrottleIpMiddleware", | ||
"django.middleware.csrf.CsrfViewMiddleware", | ||
|
@@ -753,7 +754,10 @@ def _default_from_email(): | |
# cache. | ||
if os.environ.get("MEMCACHE_SERVERS") is not None: | ||
CACHES = { | ||
"default": {"BACKEND": "django_bmemcached.memcached.BMemcached", "OPTIONS": {}} | ||
"default": { | ||
"BACKEND": "django_bmemcached.memcached.BMemcached", | ||
"OPTIONS": {}, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Please restore this change. :) |
||
} | ||
else: | ||
CACHES = { | ||
|
@@ -1168,7 +1172,10 @@ def account_username(user): | |
) | ||
# Used for Community Builder badge | ||
BADGES_PROMOTION_THRESHOLDS = list( | ||
map(int, os.environ.get("BADGES_PROMOTION_THRESHOLDS", "1, 2, 5").split(",")) | ||
map( | ||
int, | ||
os.environ.get("BADGES_PROMOTION_THRESHOLDS", "1, 2, 5").split(","), | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Please restore this change. :) |
||
) | ||
|
||
DEFAULT_AUTO_FIELD = "django.db.models.AutoField" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove this line? I didn't check, but the
context_processor
should passsettings
to all templates:https://github.com/mozilla/pontoon/blob/08710a279ab2f2c52b7f1a1a1035b6778c5f7f95/pontoon/base/context_processors.py
We'd need to change
DEFAULT_FROM_EMAIL
tosettings.DEFAULT_FROM_EMAIL
inaccount_disabled.html
though.