signup captcha support + privacy/terms support

This commit is contained in:
vabene1111 2021-06-05 16:40:28 +02:00
parent a970f0c00e
commit fd7e20a46b
8 changed files with 94 additions and 4 deletions

View File

@ -87,6 +87,15 @@ REVERSE_PROXY_AUTH=0
# when unset: 0 (false) # when unset: 0 (false)
# ENABLE_SIGNUP=0 # ENABLE_SIGNUP=0
# If signup is enabled you might want to add a captcha to it to prevent spam
# HCAPTCHA_SITEKEY=
# HCAPTCHA_SECRET=
# if signup is enabled you might want to provide urls to data protection policies or terms and conditions
# TERMS_URL=
# PRIVACY_URL=
# IMPRINT_URL=
# enable serving of prometheus metrics under the /metrics path # enable serving of prometheus metrics under the /metrics path
# ATTENTION: view is not secured (as per the prometheus default way) so make sure to secure it # ATTENTION: view is not secured (as per the prometheus default way) so make sure to secure it
# trough your web server (or leave it open of you dont care if the stats are exposed) # trough your web server (or leave it open of you dont care if the stats are exposed)

View File

@ -1,10 +1,12 @@
from django import forms from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.forms import widgets from django.forms import widgets
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django_scopes import scopes_disabled from django_scopes import scopes_disabled
from django_scopes.forms import SafeModelChoiceField, SafeModelMultipleChoiceField from django_scopes.forms import SafeModelChoiceField, SafeModelMultipleChoiceField
from emoji_picker.widgets import EmojiPickerTextInput from emoji_picker.widgets import EmojiPickerTextInput
from hcaptcha.fields import hCaptchaField
from .models import (Comment, Food, InviteLink, Keyword, MealPlan, Recipe, from .models import (Comment, Food, InviteLink, Keyword, MealPlan, Recipe,
RecipeBook, RecipeBookEntry, Storage, Sync, Unit, User, RecipeBook, RecipeBookEntry, Storage, Sync, Unit, User,
@ -68,6 +70,21 @@ class UserPreferenceForm(forms.ModelForm):
} }
class AllAuthSignupForm(forms.Form):
captcha = hCaptchaField()
terms = forms.BooleanField(label=_('Accept Terms and Privacy'))
def __init__(self, **kwargs):
super(AllAuthSignupForm, self).__init__(**kwargs)
if settings.PRIVACY_URL == '' and settings.TERMS_URL == '':
self.fields.pop('terms')
if settings.HCAPTCHA_SECRET == '':
self.fields.pop('captcha')
def signup(self, request, user):
pass
class UserNameForm(forms.ModelForm): class UserNameForm(forms.ModelForm):
prefix = 'name' prefix = 'name'

View File

@ -0,0 +1,12 @@
from django.conf import settings
def context_settings(request):
return {
'EMAIL_ENABLED': settings.EMAIL_HOST != '',
'SIGNUP_ENABLED': settings.ENABLE_SIGNUP,
'CAPTCHA_ENABLED': settings.HCAPTCHA_SITEKEY != '',
'TERMS_URL': settings.TERMS_URL,
'PRIVACY_URL': settings.PRIVACY_URL,
'IMPRINT_URL': settings.IMPRINT_URL,
}

View File

@ -28,7 +28,7 @@
<button class="btn btn-primary" type="submit">{% trans "Sign In" %}</button> <button class="btn btn-primary" type="submit">{% trans "Sign In" %}</button>
<a class="btn btn-success" href="{% url 'account_signup' %}">{% trans "Sign Up" %}</a> <a class="btn btn-success" href="{% url 'account_signup' %}">{% trans "Sign Up" %}</a>
{% if settings.EMAIL_HOST != '' %} {% if EMAIL_ENABLED %}
<a class="btn btn-secondary" <a class="btn btn-secondary"
href="{% url 'account_reset_password' %}">{% trans "Reset Password" %}</a> href="{% url 'account_reset_password' %}">{% trans "Reset Password" %}</a>
{% endif %} {% endif %}

View File

@ -13,7 +13,7 @@
{% include "account/snippets/already_logged_in.html" %} {% include "account/snippets/already_logged_in.html" %}
{% endif %} {% endif %}
{% if settings.EMAIL_HOST != '' %} {% if EMAIL_ENABLED %}
<p>{% trans "Forgotten your password? Enter your e-mail address below, and we'll send you an e-mail allowing you to reset it." %}</p> <p>{% trans "Forgotten your password? Enter your e-mail address below, and we'll send you an e-mail allowing you to reset it." %}</p>
<form method="POST" action="{% url 'account_reset_password' %}" class="password_reset"> <form method="POST" action="{% url 'account_reset_password' %}" class="password_reset">

View File

@ -1,5 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load crispy_forms_filters %} {% load crispy_forms_filters %}
{% load crispy_forms_filters %}
{% load i18n %} {% load i18n %}
{% block title %}{% trans 'Register' %}{% endblock %} {% block title %}{% trans 'Register' %}{% endblock %}
@ -10,7 +11,45 @@
<form method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
{{ form|crispy }}
<div class="form-group">
{{ form.username |as_crispy_field }}
</div>
<div class="form-group">
{{ form.email |as_crispy_field }}
</div>
<div class="form-group">
{{ form.password1 |as_crispy_field }}
</div>
<div class="form-group">
{{ form.password2 |as_crispy_field }}
</div>
{% if TERMS_URL != '' or PRIVACY_URL != '' %}
<div class="form-group">
{{ form.terms |as_crispy_field }}
<small>
{% trans 'I accept the follwoing' %}
{% if TERMS_URL != '' %}
<a href="{{ TERMS_URL }}" target="_blank" rel="noreferrer nofollow">{% trans 'Terms and Conditions' %}</a>
{% endif %}
{% if TERMS_URL != '' or PRIVACY_URL != '' %}
{% trans 'and' %}
{% endif %}
{% if PRIVACY_URL != '' %}
<a href="{{ PRIVACY_URL }}" target="_blank" rel="noreferrer nofollow">{% trans 'Privacy Policy' %}</a>
{% endif %}
</small>
</div>
{% endif %}
{% if CAPTCHA_ENABLED %}
<div class="form-group">
{{ form.captcha.errors }}
{{ form.captcha }}
</div>
{% endif %}
<button class="btn btn-success" type="submit"><i class="fas fa-save"></i> {% trans 'Create User' %}</button> <button class="btn btn-success" type="submit"><i class="fas fa-save"></i> {% trans 'Create User' %}</button>
</form> </form>

View File

@ -64,6 +64,16 @@ CRISPY_TEMPLATE_PACK = 'bootstrap4'
DJANGO_TABLES2_TEMPLATE = 'cookbook/templates/generic/table_template.html' DJANGO_TABLES2_TEMPLATE = 'cookbook/templates/generic/table_template.html'
DJANGO_TABLES2_PAGE_RANGE = 8 DJANGO_TABLES2_PAGE_RANGE = 8
HCAPTCHA_SITEKEY = '' # os.getenv('HCAPTCHA_SITEKEY', '')
HCAPTCHA_SECRET = '' # os.getenv('HCAPTCHA_SECRET', '')
ACCOUNT_SIGNUP_FORM_CLASS = 'cookbook.forms.AllAuthSignupForm'
TERMS_URL = os.getenv('TERMS_URL', '')
PRIVACY_URL = os.getenv('PRIVACY_URL', '')
IMPRINT_URL = os.getenv('IMPRINT_URL', '')
MESSAGE_TAGS = { MESSAGE_TAGS = {
messages.ERROR: 'danger' messages.ERROR: 'danger'
} }
@ -92,6 +102,7 @@ INSTALLED_APPS = [
'django_cleanup.apps.CleanupConfig', 'django_cleanup.apps.CleanupConfig',
'webpack_loader', 'webpack_loader',
'django_js_reverse', 'django_js_reverse',
'hcaptcha',
'allauth', 'allauth',
'allauth.account', 'allauth.account',
'allauth.socialaccount', 'allauth.socialaccount',
@ -185,6 +196,7 @@ TEMPLATES = [
'django.contrib.auth.context_processors.auth', 'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages', 'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media', 'django.template.context_processors.media',
'cookbook.helper.context_processors.context_settings',
], ],
}, },
}, },

View File

@ -38,4 +38,5 @@ pytest-django==4.3.0
django-cors-headers==3.7.0 django-cors-headers==3.7.0
django-storages==1.11.1 django-storages==1.11.1
boto3==1.17.84 boto3==1.17.84
django-prometheus==2.1.0 django-prometheus==2.1.0
django-hCaptcha==0.1.0