Skip to content

Commit

Permalink
Add 'USER_LOGIN_FIELDS_UNIQUE_CHECK_ENABLED' setting (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
apragacz committed May 29, 2023
1 parent defb4e9 commit 4e80776
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
11 changes: 9 additions & 2 deletions docs/detailed_configuration/settings_fields.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@

'{{ f.name }}'
~{{ '~' * f.name|length }}~
{% if f.type_signature %}
{% if f.type_signature|attr("__class__")|attr("__name__") == "type" %}
* Type: ``{{ f.type_signature.__name__ }}``
{% else %}
* Type: ``{{ f.type_signature|pprint }}``
{% endif %}
{% endif %}
{% if (f.default|pprint).split('\n')|length > 1 %}
Default:
* Default:

.. code:: python

{{ f.default|pprint|indent(width=4) }}

{% else %}
Default: ``{{ f.default|pprint }}``
* Default: ``{{ f.default|pprint }}``
{% endif %}
{% if f.help %}
{{ f.help }}
Expand Down
6 changes: 5 additions & 1 deletion rest_registration/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from rest_registration.utils.users import (
get_user_email_field_name,
get_user_login_field_names,
get_user_setting,
is_model_field_unique
)

Expand Down Expand Up @@ -249,7 +250,10 @@ def drf_compatible_django_auth_backend_check() -> bool:
ErrorCode.LOGIN_FIELDS_NOT_UNIQUE,
)
def login_fields_unique_check() -> bool:
return implies(_is_auth_installed(), _are_login_fields_unique)
return implies(
_is_auth_installed() and get_user_setting('LOGIN_FIELDS_UNIQUE_CHECK_ENABLED'),
_are_login_fields_unique,
)


def _are_login_fields_unique() -> bool:
Expand Down
31 changes: 31 additions & 0 deletions rest_registration/settings_fields.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import datetime
from collections import OrderedDict, namedtuple
from textwrap import dedent
from typing import List, Union

_Field = namedtuple('_Field', [
'name',
'default',
'type_signature',
'help',
'import_string',
])
Expand All @@ -15,16 +17,45 @@ class Field(_Field):
def __new__(
cls, name, *,
default=None,
type_signature=None,
help=None, # pylint: disable=redefined-builtin
import_string=False):
return super().__new__(
cls, name=name, default=default,
type_signature=type_signature,
help=help, import_string=import_string)


USER_SETTINGS_FIELDS = [
Field(
'USER_LOGIN_FIELDS',
type_signature=Union[List[str], None],
help=dedent("""\
Setting that defines the list of fields which can be treated as
"login" fields. An example of such fields would be ``username``, ``email``.
If not set / set to ``None``, it will fall back to one-element list
containing ``user_class.USERNAME_FIELD``,
This setting is used by default :ref:`login-authenticator-setting`
implementation and default
:ref:`send-reset-password-link-user-finder-setting` implementation.
To ensure integrity, Django-REST-Registration will check if each of
the listed fields is unique. If you want to disable that check,
please refer to :ref:`user-login-fields-unique-check-enabled-setting`.
""")
),
Field(
'USER_LOGIN_FIELDS_UNIQUE_CHECK_ENABLED',
type_signature=bool,
default=True,
help=dedent("""\
If enabled, each of the fields listed in :ref:`user-login-fields-setting`
will be checked if it is unique.
**Disable this setting only when you know what you're doing!**
""")
),
Field(
'USER_HIDDEN_FIELDS',
Expand Down
9 changes: 9 additions & 0 deletions tests/unit_tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ def test_when_one_non_unique_login_field_then_check_fails():
])


@override_rest_registration_settings({
'USER_LOGIN_FIELDS': ['email'],
'USER_LOGIN_FIELDS_UNIQUE_CHECK_ENABLED': False,
})
def test_ok_when_one_non_unique_login_field_but_check_disabled():
errors = simulate_checks()
assert_error_codes_match(errors, [])


@override_rest_registration_settings({
'USER_LOGIN_FIELDS': ['username', 'email'],
})
Expand Down

0 comments on commit 4e80776

Please sign in to comment.