forked from GithubBackups/healthchecks
Fix after-login redirects to "Check Details" and other pages.
This commit is contained in:
parent
b081631e90
commit
5aba9d6196
@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
|
|||||||
|
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
- Fix after-login redirects for users landing in the "Add Slack" page
|
- Fix after-login redirects (the "?next=" query parameter)
|
||||||
|
|
||||||
|
|
||||||
## 1.3.0 - 2018-11-21
|
## 1.3.0 - 2018-11-21
|
||||||
|
@ -2,6 +2,7 @@ from django.contrib.auth.models import User
|
|||||||
from django.core import mail
|
from django.core import mail
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from hc.accounts.models import Profile
|
from hc.accounts.models import Profile
|
||||||
|
from hc.api.models import Check
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
@ -77,14 +78,22 @@ class LoginTestCase(TestCase):
|
|||||||
alice.set_password("password")
|
alice.set_password("password")
|
||||||
alice.save()
|
alice.save()
|
||||||
|
|
||||||
|
check = Check.objects.create(user=alice)
|
||||||
|
|
||||||
form = {
|
form = {
|
||||||
"action": "login",
|
"action": "login",
|
||||||
"email": "alice@example.org",
|
"email": "alice@example.org",
|
||||||
"password": "password"
|
"password": "password"
|
||||||
}
|
}
|
||||||
|
|
||||||
r = self.client.post("/accounts/login/?next=/integrations/add_slack/", form)
|
samples = [
|
||||||
self.assertRedirects(r, "/integrations/add_slack/")
|
"/integrations/add_slack/",
|
||||||
|
"/checks/%s/details/" % check.code
|
||||||
|
]
|
||||||
|
|
||||||
|
for s in samples:
|
||||||
|
r = self.client.post("/accounts/login/?next=%s" % s, form)
|
||||||
|
self.assertRedirects(r, s)
|
||||||
|
|
||||||
def test_it_handles_bad_next_parameter(self):
|
def test_it_handles_bad_next_parameter(self):
|
||||||
alice = User(username="alice", email="alice@example.org")
|
alice = User(username="alice", email="alice@example.org")
|
||||||
|
@ -13,6 +13,7 @@ from django.core import signing
|
|||||||
from django.http import HttpResponseForbidden, HttpResponseBadRequest
|
from django.http import HttpResponseForbidden, HttpResponseBadRequest
|
||||||
from django.shortcuts import redirect, render
|
from django.shortcuts import redirect, render
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now
|
||||||
|
from django.urls import resolve, Resolver404
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from django.views.decorators.http import require_POST
|
from django.views.decorators.http import require_POST
|
||||||
from hc.accounts.forms import (ChangeEmailForm, EmailPasswordForm,
|
from hc.accounts.forms import (ChangeEmailForm, EmailPasswordForm,
|
||||||
@ -25,9 +26,21 @@ from hc.api.models import Channel, Check
|
|||||||
from hc.lib.badges import get_badge_url
|
from hc.lib.badges import get_badge_url
|
||||||
from hc.payments.models import Subscription
|
from hc.payments.models import Subscription
|
||||||
|
|
||||||
NEXT_WHITELIST = ("/checks/",
|
NEXT_WHITELIST = ("hc-checks",
|
||||||
"/integrations/add_slack/",
|
"hc-details",
|
||||||
"/integrations/add_pushover/")
|
"hc-log",
|
||||||
|
"hc-channels",
|
||||||
|
"hc-add-slack",
|
||||||
|
"hc-add-pushover")
|
||||||
|
|
||||||
|
|
||||||
|
def _is_whitelisted(path):
|
||||||
|
try:
|
||||||
|
match = resolve(path)
|
||||||
|
except Resolver404:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return match.url_name in NEXT_WHITELIST
|
||||||
|
|
||||||
|
|
||||||
def _make_user(email):
|
def _make_user(email):
|
||||||
@ -67,7 +80,7 @@ def _redirect_after_login(request):
|
|||||||
""" Redirect to the URL indicated in ?next= query parameter. """
|
""" Redirect to the URL indicated in ?next= query parameter. """
|
||||||
|
|
||||||
redirect_url = request.GET.get("next")
|
redirect_url = request.GET.get("next")
|
||||||
if redirect_url in NEXT_WHITELIST:
|
if _is_whitelisted(redirect_url):
|
||||||
return redirect(redirect_url)
|
return redirect(redirect_url)
|
||||||
|
|
||||||
return redirect("hc-checks")
|
return redirect("hc-checks")
|
||||||
@ -90,7 +103,7 @@ def login(request):
|
|||||||
profile = Profile.objects.for_user(magic_form.user)
|
profile = Profile.objects.for_user(magic_form.user)
|
||||||
|
|
||||||
redirect_url = request.GET.get("next")
|
redirect_url = request.GET.get("next")
|
||||||
if redirect_url in NEXT_WHITELIST:
|
if _is_whitelisted(redirect_url):
|
||||||
profile.send_instant_login_link(redirect_url=redirect_url)
|
profile.send_instant_login_link(redirect_url=redirect_url)
|
||||||
else:
|
else:
|
||||||
profile.send_instant_login_link()
|
profile.send_instant_login_link()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user