-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2187] Implemented email-verification views, refactored middleware's
- Loading branch information
Bart van der Schoor
committed
Mar 19, 2024
1 parent
5d2b4b4
commit 4f8fce8
Showing
11 changed files
with
230 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,39 @@ | ||
import logging | ||
from django.urls import reverse_lazy | ||
|
||
from django.conf import settings | ||
from django.http import HttpResponseRedirect | ||
from django.urls import NoReverseMatch, reverse | ||
from open_inwoner.cms.utils.page_display import profile_page_is_published | ||
from open_inwoner.configurations.models import SiteConfiguration | ||
from open_inwoner.utils.middleware import BaseForcedRedirectMiddleware | ||
|
||
from furl import furl | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class NecessaryFieldsMiddleware: | ||
class NecessaryFieldsMiddleware(BaseForcedRedirectMiddleware): | ||
""" | ||
Redirect the user to a view to fill in necessary fields | ||
""" | ||
|
||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
try: | ||
necessary_fields_url = reverse("profile:registration_necessary") | ||
except NoReverseMatch: | ||
logger.warning( | ||
"cannot reverse 'profile:registration_necessary' URL: apphook not active" | ||
) | ||
return self.get_response(request) | ||
|
||
if request.path.startswith(settings.MEDIA_URL) or request.path.startswith( | ||
settings.PRIVATE_MEDIA_URL | ||
): | ||
return self.get_response(request) | ||
redirect_url = reverse_lazy("profile:registration_necessary") | ||
|
||
def requires_redirect(self, request) -> bool: | ||
user = request.user | ||
if user.is_authenticated: | ||
return ( | ||
user.is_authenticated | ||
and user.require_necessary_fields() | ||
and profile_page_is_published() | ||
) | ||
|
||
# If the user is currently not editing their information, but it is required | ||
# redirect to that view. | ||
|
||
try: | ||
digid_logout = reverse("digid:logout") | ||
digid_slo_redirect = reverse("digid:slo-redirect") | ||
except NoReverseMatch: | ||
# temporary fix to make tests pass in case reverse fails | ||
digid_logout = "/digid/logout/" | ||
digid_slo_redirect = "/digid/slo/redirect/" | ||
if ( | ||
not request.path.startswith( | ||
( | ||
necessary_fields_url, | ||
reverse("logout"), | ||
digid_logout, | ||
digid_slo_redirect, | ||
reverse("kvk:branches"), | ||
) | ||
) | ||
and request.user.require_necessary_fields() | ||
): | ||
redirect = furl(reverse("profile:registration_necessary")) | ||
if request.path != settings.LOGIN_REDIRECT_URL: | ||
redirect.set({"next": request.path}) | ||
return HttpResponseRedirect(redirect.url) | ||
class EmailVerificationMiddleware(BaseForcedRedirectMiddleware): | ||
""" | ||
Redirect the user to a view to verify email | ||
""" | ||
|
||
return self.get_response(request) | ||
redirect_url = reverse_lazy("profile:email_verification_user") | ||
extra_pass_prefixes = (reverse_lazy("mail:verification"),) | ||
|
||
def requires_redirect(self, request) -> bool: | ||
user = request.user | ||
return ( | ||
user.is_authenticated | ||
and not user.has_verified_email() | ||
and profile_page_is_published() | ||
and SiteConfiguration.get_solo().email_verification_required | ||
) |
24 changes: 24 additions & 0 deletions
24
src/open_inwoner/accounts/templates/accounts/email_verification.html
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 @@ | ||
{% extends 'master.html' %} | ||
{% load i18n static form_tags card_tags grid_tags solo_tags %} | ||
|
||
{% block content %} | ||
<div class="registration-grid"> | ||
{% render_grid %} | ||
{% render_column span=9 %} | ||
{% render_card tinted=True %} | ||
{% get_solo 'configurations.SiteConfiguration' as config %} | ||
<h1 class="h1">{% trans "E-mailadres bevestigen" %}</h1><br> | ||
{% if config.email_verification_text %}<p class="p">{{ config.email_verification_text|urlize|linebreaksbr }}</p><br>{% endif %} | ||
<form method="POST" id="email-verification-form" action="{{ request.get_full_path }}" class="form" novalidate> | ||
{% csrf_token %} | ||
{# {% for field in form.fields %}#} | ||
{# {% autorender_field form field %}#} | ||
{# {% endfor %}#} | ||
{% trans "Verficatie email verzenden" as button_text %} | ||
{% form_actions primary_icon='arrow_forward' primary_text=button_text %} | ||
</form> | ||
{% endrender_card %} | ||
{% endrender_column %} | ||
{% endrender_grid %} | ||
</div> | ||
{% endblock content %} |
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,52 +1,27 @@ | ||
import logging | ||
|
||
from django.conf import settings | ||
from django.http import HttpResponseRedirect | ||
from django.urls import NoReverseMatch, reverse | ||
|
||
from furl import furl | ||
|
||
from open_inwoner.kvk.branches import kvk_branch_selected_done | ||
from open_inwoner.utils.middleware import BaseForcedRedirectMiddleware | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class KvKLoginMiddleware: | ||
class KvKLoginMiddleware(BaseForcedRedirectMiddleware): | ||
"""Redirect authenticated eHerkenning users to select a company branch""" | ||
|
||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
def requires_redirect(self, request): | ||
user = request.user | ||
return ( | ||
user.is_authenticated | ||
and user.is_eherkenning_user | ||
and not kvk_branch_selected_done(request.session) | ||
) | ||
|
||
# pass through | ||
if ( | ||
not user.is_authenticated | ||
or not user.is_eherkenning_user | ||
or kvk_branch_selected_done(request.session) | ||
or request.path.startswith(settings.MEDIA_URL) | ||
or request.path.startswith(settings.PRIVATE_MEDIA_URL) | ||
): | ||
return self.get_response(request) | ||
|
||
# let the user logout and avoid redirect circles | ||
def get_redirect_url(self, request): | ||
try: | ||
logout = reverse("logout") | ||
eherkenning_logout = reverse("eherkenning:logout") | ||
branches = reverse("kvk:branches") | ||
return reverse("kvk:branches") | ||
except NoReverseMatch: | ||
logout = "/accounts/logout/" | ||
eherkenning_logout = "/eherkenning/logout/" | ||
branches = "/kvk/branches/" | ||
|
||
if request.path.startswith((logout, eherkenning_logout, branches)): | ||
return self.get_response(request) | ||
|
||
# redirect to company branch choice | ||
redirect = furl(reverse("kvk:branches")) | ||
if request.path != settings.LOGIN_REDIRECT_URL: | ||
redirect.set({"next": request.path}) | ||
redirect.args.update(request.GET) | ||
|
||
return HttpResponseRedirect(redirect.url) | ||
# temporary fallback for tests | ||
return "/kvk/branches/" |
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,14 +1,14 @@ | ||
from django.urls import path | ||
|
||
from open_inwoner.mail.views import EmailVerificationView | ||
from open_inwoner.mail.views import EmailVerificationTokenView | ||
|
||
app_name = "mail" | ||
|
||
|
||
urlpatterns = [ | ||
path( | ||
"verification/", | ||
EmailVerificationView.as_view(), | ||
EmailVerificationTokenView.as_view(), | ||
name="verification", | ||
), | ||
] |
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
Oops, something went wrong.