Skip to content

Commit

Permalink
✨ [#65] Add group_mapping functionality
Browse files Browse the repository at this point in the history
issue: #65
  • Loading branch information
stevenbal committed Dec 19, 2023
1 parent 7d57717 commit 972d825
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions mozilla_django_oidc_db/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class OpenIDConnectConfigAdmin(DynamicArrayMixin, SingletonModelAdmin):
"sync_groups",
"sync_groups_glob_pattern",
"default_groups",
"group_mapping",
"make_users_staff",
)
},
Expand Down
8 changes: 8 additions & 0 deletions mozilla_django_oidc_db/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ def update_user_groups(self, user, claims):
groups_claim,
)
claim_groups = []

if self.config.group_mapping:
new_claim_groups = set()
for group_name, map_to in self.config.group_mapping:
if group_name in claim_groups:
new_claim_groups.add(map_to)
claim_groups = list(new_claim_groups)

if sorted(claim_groups) != sorted(django_groups):
existing_groups = list(
Group.objects.filter(name__in=claim_groups).iterator()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.2.18 on 2023-12-19 14:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
(
"mozilla_django_oidc_db",
"0011_alter_openidconnectconfig_userinfo_claims_source",
),
]

operations = [
migrations.AddField(
model_name="openidconnectconfig",
name="group_mapping",
field=models.JSONField(
default=list,
help_text="Mapping from group names to local groups in the application",
verbose_name="group mapping",
),
),
]
5 changes: 5 additions & 0 deletions mozilla_django_oidc_db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ class OpenIDConnectConfig(CachingMixin, OpenIDConnectConfigBase):
"The default groups to which every user logging in with OIDC will be assigned"
),
)
group_mapping = models.JSONField(
_("group mapping"),
default=list,
help_text=("Mapping from group names to local groups in the application"),
)

make_users_staff = models.BooleanField(
_("make users staff"),
Expand Down

0 comments on commit 972d825

Please sign in to comment.