Skip to content

Commit ca76b29

Browse files
committed
Add non-empty constraint for UserSocialAuth uid
Any empty uids are surely erroneous, and better to have integrity errors than having users start sharing accounts when logging in.
1 parent 56861b7 commit ca76b29

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 5.1.7 on 2025-03-14 12:16
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
("social_django", "0016_alter_usersocialauth_extra_data"),
10+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
11+
]
12+
13+
operations = [
14+
migrations.AddConstraint(
15+
model_name="usersocialauth",
16+
constraint=models.CheckConstraint(
17+
check=models.Q(("uid", ""), _negated=True),
18+
name="user_social_auth_uid_required",
19+
),
20+
),
21+
]

social_django/models.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import Union
44

5+
import django
56
from django.conf import settings
67
from django.db import models
78
from django.db.utils import IntegrityError
@@ -43,6 +44,10 @@ def __str__(self):
4344

4445
class Meta:
4546
app_label = "social_django"
47+
if django.VERSION[0] < (6,):
48+
constraints = [models.CheckConstraint(check=~models.Q(uid=""), name="user_social_auth_uid_required")]
49+
else:
50+
constraints = [models.CheckConstraint(condition=~models.Q(uid=""), name="user_social_auth_uid_required")]
4651
abstract = True
4752

4853
@classmethod
@@ -70,7 +75,7 @@ def user_model(cls):
7075
class UserSocialAuth(AbstractUserSocialAuth):
7176
"""Social Auth association model"""
7277

73-
class Meta:
78+
class Meta(AbstractUserSocialAuth.Meta):
7479
"""Meta data"""
7580

7681
app_label = "social_django"

tests/test_views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_complete(self, mock_request):
4646
def test_disconnect(self, mock_request):
4747
user_model = get_user_model()
4848
user = user_model._default_manager.create_user(username="test", password="pwd")
49-
UserSocialAuth.objects.create(user=user, provider="facebook")
49+
UserSocialAuth.objects.create(user=user, provider="facebook", uid="some-mock-facebook-uid")
5050
self.client.login(username="test", password="pwd")
5151

5252
url = reverse("social:disconnect", kwargs={"backend": "facebook"})

0 commit comments

Comments
 (0)