Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] ✨ [#65] Add group_mapping functionality #66

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading