feat: move face setiing to profile

pull/14546/head
Aaron3S 2024-11-28 18:06:37 +08:00
parent 35a1655905
commit 4036420d0e
4 changed files with 24 additions and 19 deletions

View File

@ -1,7 +1,4 @@
from django.core.cache import cache
from authentication.mfa.base import BaseMFA
from django.shortcuts import reverse
from django.utils.translation import gettext_lazy as _
from authentication.mixins import MFAFaceMixin
@ -39,10 +36,10 @@ class MFAFace(BaseMFA, MFAFaceMixin):
and settings.FACE_RECOGNITION_ENABLED
def get_enable_url(self) -> str:
return reverse('authentication:user-face-enable')
return '/ui/#/profile/index'
def get_disable_url(self) -> str:
return reverse('authentication:user-face-disable')
return '/ui/#/profile/index'
def disable(self):
assert self.is_authenticated()
@ -54,4 +51,8 @@ class MFAFace(BaseMFA, MFAFaceMixin):
@staticmethod
def help_text_of_enable():
return _("Frontal Face Recognition")
return _("Bind face to enable")
@staticmethod
def help_text_of_disable():
return _("Unbind face to disable")

View File

@ -15,6 +15,10 @@ logger = get_logger(__file__)
class FaceMixin:
face_vector = None
@property
def is_face_code_set(self):
return self.face_vector is not None
def get_face_vector(self) -> list[float]:
if not self.face_vector:
raise ValidationError("Face vector is not set.")

View File

@ -140,6 +140,10 @@ class UserSerializer(
label=_("Can public key authentication"),
read_only=True,
)
is_face_code_set = serializers.BooleanField(
label=_("Is face code set"),
read_only=True,
)
password = EncryptedField(
label=_("Password"),
required=False,
@ -205,6 +209,7 @@ class UserSerializer(
"can_public_key_auth",
"mfa_enabled",
"need_update_password",
"is_face_code_set",
]
# 包含不太常用的字段,可以没有
fields_verbose = (

View File

@ -1,5 +1,3 @@
from django.contrib.auth import logout as auth_logout
from django.shortcuts import redirect
from django.views.generic import FormView
from django import forms
from django.utils.translation import gettext_lazy as _
@ -53,15 +51,14 @@ class UserFaceEnableView(MFAFaceMixin, UserFaceCaptureView):
form.add_error("code", str(e))
return super().form_invalid(form)
auth_logout(self.request)
return super().form_valid(form)
def get_success_url(self):
message_data = {
'title': _('Face recognition enable success'),
'message': _('Face recognition enable success, return login page'),
'interval': 5,
'redirect_url': reverse('authentication:login'),
'title': _('Face binding successful'),
'message': _('Face binding successful'),
'interval': 2,
'redirect_url': '/ui/#/profile/index'
}
url = FlashMessageUtil.gen_message_url(message_data)
return url
@ -77,16 +74,14 @@ class UserFaceDisableView(UserFaceCaptureView):
except (errors.MFAFailedError, errors.BlockMFAError) as e:
form.add_error('code', e.msg)
return super().form_invalid(form)
auth_logout(self.request)
return super().form_valid(form)
def get_success_url(self):
message_data = {
'title': _('Face recognition disable success'),
'message': _('Face recognition disable success, return login page'),
'interval': 5,
'redirect_url': reverse('authentication:login'),
'title': _('Face unbinding successful'),
'message': _('Face unbinding successful'),
'interval': 2,
'redirect_url': '/ui/#/profile/index'
}
url = FlashMessageUtil.gen_message_url(message_data)
return url