Skip to content

Commit b5f93b3

Browse files
author
Michał Kopydłowski
committed
Add method_decorator to use custom decorator on a view.
1 parent bc3d90a commit b5f93b3

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

trench/decorators.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from django.contrib.auth import REDIRECT_FIELD_NAME
22
from django.contrib.auth.decorators import user_passes_test
33

4-
from trench.command.authenticate_second_factor import authenticate_second_step_command
5-
64

75
def mfa_login_required(
86
function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None
@@ -12,13 +10,11 @@ def mfa_login_required(
1210
to the log-in page if necessary.
1311
"""
1412

15-
def test(user):
16-
# return user.is_verified() or (user.is_authenticated and not user_has_device(user))
17-
return authenticate_second_step_command
13+
def is_user_authenticated(user):
14+
return user.is_authenticated
1815

1916
actual_decorator = user_passes_test(
20-
lambda u: u.is_authenticated,
21-
# test,
17+
is_user_authenticated,
2218
login_url=login_url,
2319
redirect_field_name=redirect_field_name,
2420
)

trench/views/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from django.contrib.auth.decorators import login_required
21
from django.contrib.auth.models import User
32
from django.db.models import QuerySet
3+
from django.utils.decorators import method_decorator
44
from django.utils.translation import gettext_lazy as _
55

66
from abc import ABC, abstractmethod
@@ -212,7 +212,6 @@ class MFAMethodRequestCodeView(APIView):
212212
permission_classes = (IsAuthenticated,)
213213

214214
@staticmethod
215-
@login_required
216215
def post(request: Request) -> Response:
217216
serializer = MFAMethodCodeSerializer(data=request.data)
218217
serializer.is_valid(raise_exception=True)
@@ -228,6 +227,10 @@ def post(request: Request) -> Response:
228227
except MFAValidationError as cause:
229228
return ErrorResponse(error=cause)
230229

230+
@method_decorator(mfa_login_required)
231+
def dispatch(self, *args, **kwargs):
232+
return super().dispatch(*args, **kwargs)
233+
231234

232235
class MFAPrimaryMethodChangeView(APIView):
233236
permission_classes = (IsAuthenticated,)

0 commit comments

Comments
 (0)