Skip to content

Commit

Permalink
Amélioration de la connexion via les réseaux sociaux (#6429)
Browse files Browse the repository at this point in the history
* (Connexion OAuth) On n'enlève plus les lettres accentuées des pseudos
* (Connexion OAuth) On récupère l'adresse de courriel des comptes Facebook
  • Loading branch information
Situphen authored Mar 11, 2024
1 parent 2910ebf commit 2da324a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
14 changes: 13 additions & 1 deletion zds/member/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _

from zds.utils.misc import contains_utf8mb4
from zds.utils.misc import contains_utf8mb4, remove_utf8mb4
from zds.member.models import BannedEmailProvider, Profile


Expand Down Expand Up @@ -70,6 +70,15 @@ def __call__(self, value, check_username_available=True):
validate_zds_email = ZdSEmailValidator()


def clean_username_social_auth(username):
"""
Clean username of accounts created using social auth.
"""
# These three conditions are the same as the first three in the "validate_zds_username" function below.
# If you modify one of them here, make sure you do the same there!
return remove_utf8mb4(username).replace(",", "").replace("/", "")


def validate_zds_username(value, check_username_available=True):
"""
Check if username is used by another user
Expand All @@ -88,6 +97,9 @@ def validate_zds_username(value, check_username_available=True):
msg = None
user_count = User.objects.filter(username=value).count()
skeleton_user_count = Profile.objects.filter(username_skeleton=Profile.find_username_skeleton(value)).count()

# These first three conditions are the same as those in the "clean_username_social_auth" function above.
# If you modify one of them here, make sure you do the same there!
if "," in value:
msg = _("Le nom d'utilisateur ne peut contenir de virgules")
elif "/" in value:
Expand Down
3 changes: 3 additions & 0 deletions zds/settings/abstract_base/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

social_auth_config = config.get("social_auth", {})

SOCIAL_AUTH_CLEAN_USERNAME_FUNCTION = "zds.member.validators.clean_username_social_auth"

SOCIAL_AUTH_RAISE_EXCEPTIONS = False

SOCIAL_AUTH_FACEBOOK_SCOPE = ["email"]
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {"fields": "name,email"}

SOCIAL_AUTH_PIPELINE = (
"social_core.pipeline.social_auth.social_details",
Expand Down
13 changes: 10 additions & 3 deletions zds/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,21 @@ def convert_camel_to_underscore(camel_case):
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()


def contains_utf8mb4(s):
def remove_utf8mb4(s):
"""
This string contains at least one character of more than 3 bytes
Remove characters of more than 3 bytes.
"""
if not isinstance(s, str):
s = str(s, "utf-8")
re_pattern = re.compile("[^\u0000-\uD7FF\uE000-\uFFFF]", re.UNICODE)
return s != re_pattern.sub("\uFFFD", s)
return re_pattern.sub("", s)


def contains_utf8mb4(s):
"""
Check if this string contains at least one character of more than 3 bytes.
"""
return s != remove_utf8mb4(s)


def check_essential_accounts():
Expand Down
12 changes: 10 additions & 2 deletions zds/utils/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
from django.test import TestCase
from zds.member.tests.factories import ProfileFactory, StaffProfileFactory, UserFactory
from zds.tutorialv2.tests.factories import PublishedContentFactory
from zds.utils.misc import contains_utf8mb4, check_essential_accounts
from zds.utils.misc import contains_utf8mb4, check_essential_accounts, remove_utf8mb4
from zds.utils.models import Alert
from zds.utils.context_processor import get_header_notifications


class Misc(TestCase):
def test_utf8mb4(self):
def test_remove_utf8mb4(self):
self.assertEqual("abc", remove_utf8mb4("abc"))
self.assertEqual("abc", remove_utf8mb4("abc"))
self.assertEqual("abc€", remove_utf8mb4("abc€"))
self.assertEqual("abc€", remove_utf8mb4("abc€"))
self.assertEqual("atbc€", remove_utf8mb4("a🐙tbc€"))
self.assertEqual("atbc€", remove_utf8mb4("a🐙tbc€"))

def test_contains_utf8mb4(self):
self.assertFalse(contains_utf8mb4("abc"))
self.assertFalse(contains_utf8mb4("abc"))
self.assertFalse(contains_utf8mb4("abc€"))
Expand Down

0 comments on commit 2da324a

Please sign in to comment.