Rafał Pitoń 8 лет назад
Родитель
Сommit
86aaf8b8ef

+ 6 - 0
misago/project_template/project_name/settings.py

@@ -65,9 +65,15 @@ CACHES = {
 AUTH_PASSWORD_VALIDATORS = [
 AUTH_PASSWORD_VALIDATORS = [
     {
     {
         'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
         'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
+        'OPTIONS': {
+            'user_attributes': ['username', 'email'],
+        }
     },
     },
     {
     {
         'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
         'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
+        'OPTIONS': {
+            'min_length': 7,
+        }
     },
     },
     {
     {
         'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
         'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',

+ 26 - 1
misago/users/api/auth.py

@@ -3,7 +3,7 @@ from django.core.exceptions import ValidationError
 from django.utils.translation import ugettext as _
 from django.utils.translation import ugettext as _
 from django.views.decorators.csrf import csrf_protect
 from django.views.decorators.csrf import csrf_protect
 
 
-from rest_framework import status
+from rest_framework import status, viewsets
 from rest_framework.decorators import api_view, permission_classes
 from rest_framework.decorators import api_view, permission_classes
 from rest_framework.response import Response
 from rest_framework.response import Response
 
 
@@ -56,6 +56,31 @@ def session_user(request):
 
 
 
 
 """
 """
+GET /auth/criteria/ will return password and username criteria for accounts
+"""
+@api_view(['GET'])
+def get_criteria(request):
+    criteria = {
+        'username': {
+            'min_length': settings.username_length_min,
+            'max_length': settings.username_length_max,
+        },
+        'password': [],
+    }
+
+    for validator in settings.AUTH_PASSWORD_VALIDATORS:
+        validator_dict = {
+            'name': validator['NAME'].split('.')[-1]
+        }
+
+        validator_dict.update(validator.get('OPTIONS', {}))
+
+        criteria['password'].append(validator_dict)
+
+    return Response(criteria)
+
+
+"""
 POST /auth/send-activation/ with CSRF token and email
 POST /auth/send-activation/ with CSRF token and email
 will mail account activation link to requester
 will mail account activation link to requester
 """
 """

+ 1 - 1
misago/users/forms/auth.py

@@ -51,7 +51,7 @@ class MisagoAuthMixin(object):
 class AuthenticationForm(MisagoAuthMixin, BaseAuthenticationForm):
 class AuthenticationForm(MisagoAuthMixin, BaseAuthenticationForm):
     """
     """
     Base class for authenticating users, Floppy-forms and
     Base class for authenticating users, Floppy-forms and
-    Misago login field comliant
+    Misago login field compliant
     """
     """
     username = forms.CharField(
     username = forms.CharField(
         label=_("Username or e-mail"),
         label=_("Username or e-mail"),

+ 7 - 0
misago/users/tests/test_auth_api.py

@@ -166,6 +166,13 @@ class GatewayTests(TestCase):
         self.assertIsNone(user_json['id'])
         self.assertIsNone(user_json['id'])
 
 
 
 
+class UserCredentialsTests(TestCase):
+    def test_edge_returns_response(self):
+        """api edge has no showstoppers"""
+        response = self.client.get('/api/auth/criteria/')
+        self.assertEqual(response.status_code, 200)
+
+
 class SendActivationAPITests(TestCase):
 class SendActivationAPITests(TestCase):
     def setUp(self):
     def setUp(self):
         User = get_user_model()
         User = get_user_model()

+ 1 - 0
misago/users/urls/api.py

@@ -10,6 +10,7 @@ from ..api.users import UserViewSet
 
 
 urlpatterns = [
 urlpatterns = [
     url(r'^auth/$', auth.gateway, name='auth'),
     url(r'^auth/$', auth.gateway, name='auth'),
+    url(r'^auth/criteria/$', auth.get_criteria, name='auth-criteria'),
     url(r'^auth/send-activation/$', auth.send_activation, name='send-activation'),
     url(r'^auth/send-activation/$', auth.send_activation, name='send-activation'),
     url(r'^auth/send-password-form/$', auth.send_password_form, name='send-password-form'),
     url(r'^auth/send-password-form/$', auth.send_password_form, name='send-password-form'),
     url(r'^auth/change-password/(?P<pk>\d+)/(?P<token>[a-zA-Z0-9]+)/$', auth.change_forgotten_password, name='change-forgotten-password'),
     url(r'^auth/change-password/(?P<pk>\d+)/(?P<token>[a-zA-Z0-9]+)/$', auth.change_forgotten_password, name='change-forgotten-password'),