-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add option to avoid sso on local (#773)
Co-authored-by: Cameron Lamb <[email protected]>
- Loading branch information
1 parent
9fcfa58
commit cf6d341
Showing
18 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
{% load wagtailuserbar %} | ||
|
||
<!-- Load dev_tools --> | ||
{% load dev_tools %} | ||
<!-- Add the dialog --> | ||
{% dev_tools_dialog %} | ||
|
||
<footer class="govuk-footer " role="contentinfo"> | ||
<div class="dwds-container"> | ||
<div class="govuk-footer__meta"> | ||
|
@@ -44,6 +49,9 @@ <h2 class="govuk-visually-hidden">Support links</h2> | |
<a class="govuk-footer__link" | ||
href="mailto:[email protected]">Feedback</a> | ||
</span> | ||
|
||
<!-- Add a way to open the dialog --> | ||
{% if DEV_TOOLS_ENABLED %}<button onclick="openDevToolsDialog()">Dev tools</button>{% endif %} | ||
</div> | ||
{% endif %} | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# dev_tools | ||
|
||
## Installation | ||
|
||
Add `dev_tools` to your `INSTALLED_APPS` setting. | ||
|
||
```python | ||
INSTALLED_APPS = [ | ||
... | ||
"dev_tools" | ||
] | ||
``` | ||
|
||
Add `dev_tools.context_processors.dev_tools` to your `TEMPLATES` setting. | ||
|
||
```python | ||
TEMPLATES = [ | ||
... | ||
{ | ||
... | ||
"OPTIONS": { | ||
"context_processors": [ | ||
... | ||
"dev_tools.context_processors.dev_tools", | ||
], | ||
}, | ||
}, | ||
] | ||
``` | ||
|
||
Add `dev_tools.middleware.DevToolsLoginRequiredMiddleware` to your `MIDDLEWARE` setting. | ||
|
||
```python | ||
# You should only add this middleware in dev environments where you have also set `DEV_TOOLS_ENABLED=True`. | ||
MIDDLEWARE = [ | ||
... | ||
"dev_tools.middleware.DevToolsLoginRequiredMiddleware", | ||
] | ||
``` | ||
|
||
Add `dev_tools.urls` to your `urlpatterns`. | ||
|
||
```python | ||
urlpatterns = [ | ||
... | ||
path("dev-tools/", include("dev_tools.urls")), | ||
] | ||
``` | ||
|
||
Add the following settings. | ||
|
||
```python | ||
# You should disable this in production! | ||
DEV_TOOLS_ENABLED = True | ||
DEV_TOOLS_LOGIN_URL = None | ||
DEV_TOOLS_DEFAULT_USER = None | ||
|
||
# Optional - if you want to be automatically logged in as a default user. | ||
|
||
# Use the dev_tools login view. | ||
DEV_TOOLS_LOGIN_URL = "dev_tools:login" | ||
# Primary key of the default user. | ||
DEV_TOOLS_DEFAULT_USER = 1 | ||
``` | ||
|
||
Add `dev_tools_dialog` to your base template: | ||
|
||
```html | ||
<!-- Load dev_tools --> | ||
{% load dev_tools %} | ||
<!-- Add the dialog --> | ||
{% dev_tools_dialog %} | ||
<!-- Add a way to open the dialog --> | ||
<button onclick="openDevToolsDialog()">Dev tools</button> | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class DevToolsConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "dev_tools" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.conf import settings | ||
|
||
|
||
def dev_tools(request): | ||
return { | ||
"DEV_TOOLS_ENABLED": settings.DEV_TOOLS_ENABLED, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django import forms | ||
from django.contrib.auth import get_user_model | ||
|
||
|
||
User = get_user_model() | ||
|
||
|
||
def get_user_choices(): | ||
return [ | ||
(None, "AnonymousUser"), | ||
*[(x.id, str(x)) for x in User.objects.all()], | ||
] | ||
|
||
|
||
class ChangeUserForm(forms.Form): | ||
user = forms.ChoiceField(choices=get_user_choices, required=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from django.conf import settings | ||
from django.shortcuts import redirect | ||
from django.urls import resolve | ||
|
||
|
||
EXCLUDED_APP_NAMES = ("admin", "dev_tools") | ||
|
||
|
||
class DevToolsLoginRequiredMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
assert settings.DEV_TOOLS_ENABLED | ||
|
||
def __call__(self, request): | ||
assert hasattr(request, "user") | ||
|
||
if ( | ||
not request.user.is_authenticated | ||
and resolve(request.path).app_name not in EXCLUDED_APP_NAMES | ||
): | ||
return redirect(self.get_login_url()) | ||
|
||
response = self.get_response(request) | ||
|
||
return response | ||
|
||
def get_login_url(self): | ||
return settings.DEV_TOOLS_LOGIN_URL or settings.LOGIN_URL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<dialog id="dev-tools-dialog"> | ||
<form action="{% url 'dev_tools:change-user' %}?next={{ request.get_full_path }}" | ||
method="post" | ||
novalidate> | ||
{% csrf_token %} | ||
{{ change_user_form }} | ||
<button type="submit">Change user</button> | ||
</form> | ||
|
||
<form method="dialog"> | ||
<button>Close</button> | ||
</form> | ||
</dialog> | ||
|
||
<script> | ||
function openDevToolsDialog() { | ||
document.querySelector("#dev-tools-dialog").showModal(); | ||
} | ||
</script> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from django import template | ||
from django.conf import settings | ||
from django.template.loader import render_to_string | ||
|
||
from dev_tools.forms import ChangeUserForm | ||
|
||
|
||
register = template.Library() | ||
|
||
|
||
@register.simple_tag(takes_context=True) | ||
def dev_tools_dialog(context): | ||
if not hasattr(settings, "DEV_TOOLS_ENABLED") or not ( | ||
settings.DEBUG and settings.DEV_TOOLS_ENABLED | ||
): | ||
return "" | ||
|
||
request = context["request"] | ||
|
||
context = { | ||
"change_user_form": ChangeUserForm(initial={"user": request.user.pk}), | ||
} | ||
|
||
return render_to_string("dev_tools/dialog.html", context=context, request=request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.urls import path | ||
|
||
from dev_tools.views import change_user_view, login_view | ||
|
||
|
||
app_name = "dev_tools" | ||
|
||
urlpatterns = [ | ||
path("login", login_view, name="login"), | ||
path("change-user", change_user_view, name="change-user"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from functools import wraps | ||
|
||
from django.conf import settings | ||
from django.contrib import messages | ||
from django.contrib.auth import get_user_model, login, logout | ||
from django.core.exceptions import SuspiciousOperation, ValidationError | ||
from django.shortcuts import redirect | ||
from django.views.decorators.http import require_http_methods | ||
|
||
from dev_tools.forms import ChangeUserForm | ||
|
||
|
||
User = get_user_model() | ||
|
||
|
||
def check_dev_tools_enabled(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
if not settings.DEV_TOOLS_ENABLED: | ||
raise SuspiciousOperation("Dev tools are not enabled") | ||
|
||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
@require_http_methods(["GET"]) | ||
@check_dev_tools_enabled | ||
def login_view(request): | ||
assert settings.DEV_TOOLS_DEFAULT_USER | ||
|
||
user = User.objects.get(pk=settings.DEV_TOOLS_DEFAULT_USER) | ||
login(request, user) | ||
messages.success(request, f"Automatically logged in as {user}") | ||
|
||
return redirect(settings.LOGIN_REDIRECT_URL) | ||
|
||
|
||
@require_http_methods(["POST"]) | ||
@check_dev_tools_enabled | ||
def change_user_view(request): | ||
next_url = request.GET.get("next", settings.LOGIN_REDIRECT_URL) | ||
|
||
form = ChangeUserForm(data=request.POST) | ||
|
||
if not form.is_valid(): | ||
raise ValidationError("Invalid change user form") | ||
|
||
if form.cleaned_data["user"]: | ||
new_user = User.objects.get(pk=form.cleaned_data["user"]) | ||
|
||
login(request, new_user) | ||
messages.success(request, f"Logged in as {new_user}") | ||
else: | ||
logout(request) | ||
messages.success(request, "Logged out") | ||
|
||
if is_valid_redirect_url(next_url): | ||
return redirect(next_url) | ||
redirect(settings.LOGIN_REDIRECT_URL) | ||
|
||
|
||
def is_valid_redirect_url(url: str) -> bool: | ||
if url[0] != "/" and "trade.gov.uk" not in url: | ||
return False | ||
return True |