mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-17 05:41:25 +02:00
Use LDAP backend to check password
This commit is contained in:
@@ -10,6 +10,7 @@ from django.utils.translation import gettext as _
|
||||
from rest_framework import serializers
|
||||
|
||||
from modoboa.core import models
|
||||
from modoboa.parameters import tools as param_tools
|
||||
|
||||
logger = logging.getLogger("modoboa.auth")
|
||||
|
||||
@@ -28,7 +29,16 @@ class AccountPasswordSerializer(serializers.ModelSerializer):
|
||||
|
||||
def validate_password(self, value):
|
||||
"""Check password."""
|
||||
if not self.instance.check_password(value):
|
||||
authentication_type = param_tools.get_global_parameter("authentication_type")
|
||||
if authentication_type != "ldap":
|
||||
check = self.instance.check_password(value)
|
||||
else:
|
||||
from django_auth_ldap.backend import LDAPBackend
|
||||
|
||||
check = LDAPBackend().authenticate(
|
||||
self.context["request"], self.instance.username, value
|
||||
)
|
||||
if not check:
|
||||
raise serializers.ValidationError("Password not correct")
|
||||
return value
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ class AccountViewSet(core_v1_viewsets.AccountViewSet):
|
||||
)
|
||||
def set_me_password(self, request):
|
||||
serializer = core_v1_serializers.AccountPasswordSerializer(
|
||||
instance=request.user, data=request.data
|
||||
instance=request.user, data=request.data, context={"request": request}
|
||||
)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
|
||||
@@ -157,6 +157,5 @@ class ProfileTestCase(LDAPTestCaseMixin, ModoAPITestCase):
|
||||
{"password": "test", "new_password": "Toto1234"},
|
||||
format="json",
|
||||
)
|
||||
print(response.json())
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.authenticate(username, "Toto1234", False)
|
||||
|
||||
Reference in New Issue
Block a user