Skip to content

Commit

Permalink
Merge pull request #527 from MTES-MCT/fix-user-casing
Browse files Browse the repository at this point in the history
Retire la possibilité de créer des utilisateurs avec la même adresse email mais avec un casing différent
  • Loading branch information
alexisig authored Nov 26, 2024
2 parents 88879aa + 693b08f commit 3035247
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
34 changes: 34 additions & 0 deletions users/migrations/0012_user_user_email_ci_uniqueness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 4.2.13 on 2024-11-26 11:07

from django.db import migrations, models
import django.db.models.functions.text
from django.db.utils import IntegrityError


def remove_duplicate_emails(apps, schema_editor):
User = apps.get_model("users", "User")
users = User.objects.all()

for user in users:
user.email = user.email.lower()
try:
user.save()
except IntegrityError:
user.delete()


class Migration(migrations.Migration):
atomic = False
dependencies = [
("users", "0011_alter_user_organism"),
]

operations = [
migrations.RunPython(remove_duplicate_emails),
migrations.AddConstraint(
model_name="user",
constraint=models.UniqueConstraint(
django.db.models.functions.text.Lower("email"), name="user_email_ci_uniqueness"
),
),
]
12 changes: 12 additions & 0 deletions users/migrations/0016_merge_20241126_1219.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 4.2.13 on 2024-11-26 11:19

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("users", "0012_user_user_email_ci_uniqueness"),
("users", "0015_auto_20240930_1148"),
]

operations = []
8 changes: 8 additions & 0 deletions users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models.constraints import UniqueConstraint
from django.db.models.functions import Lower
from django.utils import timezone

from utils.validators import is_alpha_validator
Expand Down Expand Up @@ -92,3 +94,9 @@ def save(self, *args, **kwargs):
class Meta:
verbose_name = "Utilisateur"
ordering = ["email"]
constraints = [
UniqueConstraint(
Lower("email"),
name="user_email_ci_uniqueness",
),
]
22 changes: 22 additions & 0 deletions users/tests/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.db.utils import IntegrityError
from django.test import TestCase

from users.models import User


class TestUsers(TestCase):
def test_user_with_the_same_email_cannot_be_created(self):
def create_two_users_with_same_email():
User.objects.create(email="[email protected]")
User.objects.create(email="[email protected]")

with self.assertRaises(IntegrityError):
create_two_users_with_same_email()

def test_user_with_the_same_email_but_different_casing_cannot_be_created(self):
def create_two_users_with_same_email_but_different_casing():
User.objects.create(email="[email protected]")
User.objects.create(email="[email protected]")

with self.assertRaises(IntegrityError):
create_two_users_with_same_email_but_different_casing()

0 comments on commit 3035247

Please sign in to comment.