Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions geonode/people/forms/recaptcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#
#########################################################################
from django import forms
from django.contrib.admin.forms import AdminAuthenticationForm
from allauth.account.forms import LoginForm

try:
Expand All @@ -37,3 +38,9 @@ class AllauthRecaptchaLoginForm(LoginForm):

def login(self, *args, **kwargs):
return super(AllauthRecaptchaLoginForm, self).login(*args, **kwargs)


class AdminReCaptchaAuthenticationForm(AdminAuthenticationForm):
"""Django admin login form with reCAPTCHA (enabled in geonode/urls.py when RECAPTCHA_ENABLED=True)."""

captcha = ReCaptchaField(label=False)
69 changes: 69 additions & 0 deletions geonode/templates/admin/login_recaptcha.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{% extends "admin/login.html" %}
{% comment %}
Django admin login with reCAPTCHA, used when RECAPTCHA_ENABLED=True (see geonode/urls.py).
Same as the Django 5.2 admin/login.html, plus the captcha field. The form.media (reCAPTCHA JS)
is already included by the parent template.
{% endcomment %}
{% load i18n static %}

{% block extrastyle %}{{ block.super }}
<style>
#login-form .captcha-row {
display: flex;
flex-direction: column;
align-items: center;
}
</style>
{% endblock %}

{% block content %}
{% if form.errors and not form.non_field_errors %}
<p class="errornote">
{% blocktranslate count counter=form.errors.items|length %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktranslate %}
</p>
{% endif %}

{% if form.non_field_errors %}
{% for error in form.non_field_errors %}
<p class="errornote">{{ error }}</p>
{% endfor %}
{% endif %}

<div id="content-main">

{% if user.is_authenticated %}
<p class="errornote">
{% blocktranslate trimmed %}
You are authenticated as {{ username }}, but are not authorized to
access this page. Would you like to login to a different account?
{% endblocktranslate %}
</p>
{% endif %}

<form action="{{ app_path }}" method="post" id="login-form">{% csrf_token %}
<div class="form-row">
{{ form.username.errors }}
{{ form.username.label_tag }} {{ form.username }}
</div>
<div class="form-row">
{{ form.password.errors }}
{{ form.password.label_tag }} {{ form.password }}
<input type="hidden" name="next" value="{{ next }}">
</div>
<div class="form-row captcha-row">
{{ form.captcha.errors }}
{{ form.captcha }}
</div>
{% url 'admin_password_reset' as password_reset_url %}
{% if password_reset_url %}
<div class="password-reset-link">
<a href="{{ password_reset_url }}">{% translate 'Forgotten your login credentials?' %}</a>
</div>
{% endif %}
<div class="submit-row">
<input type="submit" value="{% translate 'Log in' %}">
</div>
</form>

</div>
{% endblock %}
7 changes: 7 additions & 0 deletions geonode/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@
path("select2/", include("django_select2.urls")),
]

# Django admin login with reCAPTCHA (same flag as the GeoNode login and signup forms)
if settings.RECAPTCHA_ENABLED:
from geonode.people.forms.recaptcha import AdminReCaptchaAuthenticationForm

admin.site.login_form = AdminReCaptchaAuthenticationForm
admin.site.login_template = "admin/login_recaptcha.html"

urlpatterns += i18n_patterns(
re_path(r"^admin/", admin.site.urls, name="admin"),
)
Expand Down
Loading