Skip to content

Commit

Permalink
providers/scim: optimize sending all members within a group
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Langhammer <[email protected]>
  • Loading branch information
BeryJu committed Jun 4, 2024
1 parent 6cf418a commit f0cbe59
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions authentik/providers/scim/clients/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ def to_schema(self, obj: Group, connection: SCIMGroup) -> SCIMGroupSchema:
if not scim_group.externalId:
scim_group.externalId = str(obj.pk)

users = list(obj.users.order_by("id").values_list("id", flat=True))
connections = SCIMUser.objects.filter(provider=self.provider, user__pk__in=users)
members = []
for user in connections:
members.append(
GroupMember(
value=user.scim_id,
if not self._config.patch.supported:
users = list(obj.users.order_by("id").values_list("id", flat=True))
connections = SCIMUser.objects.filter(provider=self.provider, user__pk__in=users)
members = []
for user in connections:
members.append(

Check warning on line 59 in authentik/providers/scim/clients/groups.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/scim/clients/groups.py#L59

Added line #L59 was not covered by tests
GroupMember(
value=user.scim_id,
)
)
)
if members:
scim_group.members = members
if members:
scim_group.members = members

Check warning on line 65 in authentik/providers/scim/clients/groups.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/scim/clients/groups.py#L65

Added line #L65 was not covered by tests
else:
del scim_group.members
return scim_group

def delete(self, obj: Group):
Expand All @@ -88,21 +91,26 @@ def create(self, group: Group):
scim_id = response.get("id")
if not scim_id or scim_id == "":
raise StopSync("SCIM Response with missing or invalid `id`")
return SCIMGroup.objects.create(provider=self.provider, group=group, scim_id=scim_id)
connection = SCIMGroup.objects.create(provider=self.provider, group=group, scim_id=scim_id)
users = list(group.users.order_by("id").values_list("id", flat=True))
self._patch_add_users(group, users)
return connection

def update(self, group: Group, connection: SCIMGroup):
"""Update existing group"""
scim_group = self.to_schema(group, connection)
scim_group.id = connection.scim_id
try:
return self._request(
self._request(
"PUT",
f"/Groups/{connection.scim_id}",
json=scim_group.model_dump(
mode="json",
exclude_unset=True,
),
)
users = list(group.users.order_by("id").values_list("id", flat=True))
return self._patch_add_users(group, users)

Check warning on line 113 in authentik/providers/scim/clients/groups.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/scim/clients/groups.py#L112-L113

Added lines #L112 - L113 were not covered by tests
except NotFoundSyncException:
# Resource missing is handled by self.write, which will re-create the group
raise
Expand Down

0 comments on commit f0cbe59

Please sign in to comment.