-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SCIM fields to User and populate (#2062)
- Loading branch information
1 parent
e394d1d
commit 6c9a8d2
Showing
9 changed files
with
188 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""Users admin""" | ||
|
||
from django.contrib import admin | ||
from django.contrib.auth.admin import UserAdmin as ContribUserAdmin | ||
from hijack.contrib.admin import HijackUserAdminMixin | ||
|
||
from users.models import User | ||
|
||
|
||
@admin.register(User) | ||
class UserAdmin(ContribUserAdmin, HijackUserAdminMixin): | ||
"""Admin for User""" | ||
|
||
readonly_fields = ( | ||
*ContribUserAdmin.readonly_fields, | ||
"scim_id", | ||
"scim_username", | ||
"scim_external_id", | ||
) | ||
|
||
fieldsets = ( | ||
*ContribUserAdmin.fieldsets, | ||
("SCIM", {"fields": ("scim_id", "scim_username", "scim_external_id")}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Generated by Django 4.2.19 on 2025-02-19 18:01 | ||
|
||
import django.utils.timezone | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("users", "0003_rename_user_table"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="user", | ||
name="created_on", | ||
field=models.DateTimeField( | ||
auto_now_add=True, db_index=True, default=django.utils.timezone.now | ||
), | ||
preserve_default=False, | ||
), | ||
migrations.AddField( | ||
model_name="user", | ||
name="scim_external_id", | ||
field=models.CharField( | ||
blank=True, | ||
db_index=True, | ||
default=None, | ||
help_text="A string that is an identifier for the resource as defined by the provisioning client.", # noqa: E501 | ||
max_length=254, | ||
null=True, | ||
verbose_name="SCIM External ID", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="user", | ||
name="scim_id", | ||
field=models.CharField( | ||
blank=True, | ||
default=None, | ||
help_text="A unique identifier for a SCIM resource as defined by the service provider.", # noqa: E501 | ||
max_length=254, | ||
null=True, | ||
unique=True, | ||
verbose_name="SCIM ID", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="user", | ||
name="scim_username", | ||
field=models.CharField( | ||
blank=True, | ||
db_index=True, | ||
default=None, | ||
help_text="A service provider's unique identifier for the user", | ||
max_length=254, | ||
null=True, | ||
verbose_name="SCIM Username", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="user", | ||
name="updated_on", | ||
field=models.DateTimeField(auto_now=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Generated by Django 4.2.19 on 2025-02-19 16:16 | ||
import logging | ||
|
||
from django.db import migrations, models | ||
|
||
BATCH_SIZE = 10_000 | ||
|
||
log = logging.getLogger() | ||
|
||
|
||
def _set_scim_and_timestamps(apps, schema_editor): | ||
User = apps.get_model("users", "User") | ||
Profile = apps.get_model("profiles", "Profile") | ||
|
||
query = User.objects.filter( | ||
id__in=User.objects.filter(scim_id__isnull=True).only("id")[:BATCH_SIZE] | ||
) | ||
|
||
while num_updates := query.update( | ||
# this uses the user.id to avoid conflicts with new users | ||
scim_id=models.F("id"), | ||
scim_external_id=models.Subquery( | ||
Profile.objects.filter(user_id=models.OuterRef("id")).values( | ||
"scim_external_id" | ||
)[:1] | ||
), | ||
scim_username=models.Subquery( | ||
Profile.objects.filter(user_id=models.OuterRef("id")).values( | ||
"scim_username" | ||
)[:1] | ||
), | ||
# created_on previously got a default of timestamp.now | ||
# so we update it with the correct date | ||
created_on=models.F("date_joined"), | ||
# this would've been null | ||
updated_on=models.Subquery( | ||
Profile.objects.filter(user_id=models.OuterRef("id")).values("updated_at")[ | ||
:1 | ||
] | ||
), | ||
): | ||
log.info("Updated %s user records", num_updates) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
""" | ||
This is a separate migration from 0004 because for performance reasons | ||
we don't want to update the entire table in a transaction but we DO | ||
want the schema changes in 0004 in a transaction. | ||
""" | ||
|
||
atomic = False | ||
|
||
dependencies = [ | ||
("users", "0004_add_scim_and_timestamp_fields"), | ||
] | ||
|
||
# we don't bother to undo these changes becaus eif we're rolling back the columns | ||
# just get dropped in the previous migration | ||
operations = [ | ||
migrations.RunPython(_set_scim_and_timestamps, migrations.RunPython.noop) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.19 on 2025-02-24 20:23 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("users", "0005_set_user_scim_id"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunSQL( | ||
sql="DROP VIEW auth_user;", | ||
reverse_sql="CREATE VIEW auth_user AS SELECT * FROM users_user;", | ||
elidable=True, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
"""Users models""" | ||
|
||
from django.contrib.auth.models import AbstractUser | ||
from django_scim.models import AbstractSCIMUserMixin | ||
|
||
from main.models import TimestampedModel | ||
|
||
class User(AbstractUser): | ||
|
||
class User(AbstractUser, AbstractSCIMUserMixin, TimestampedModel): | ||
"""Custom model for users""" |