Browse Source

Fix after-login redirects to "Check Details" and other pages.

pull/211/head
Pēteris Caune 6 years ago
parent
commit
5aba9d6196
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 30 additions and 8 deletions
  1. +1
    -1
      CHANGELOG.md
  2. +11
    -2
      hc/accounts/tests/test_login.py
  3. +18
    -5
      hc/accounts/views.py

+ 1
- 1
CHANGELOG.md View File

@ -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


+ 11
- 2
hc/accounts/tests/test_login.py View File

@ -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": "[email protected]", "email": "[email protected]",
"password": "password" "password": "password"
} }
r = self.client.post("/accounts/login/?next=/integrations/add_slack/", form)
self.assertRedirects(r, "/integrations/add_slack/")
samples = [
"/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="[email protected]") alice = User(username="alice", email="[email protected]")


+ 18
- 5
hc/accounts/views.py View File

@ -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/",
"/integrations/add_slack/",
"/integrations/add_pushover/")
NEXT_WHITELIST = ("hc-checks",
"hc-details",
"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…
Cancel
Save