core: simplify group serializer for user API endpoint (#3899)

* core/api: Adding simple group serializer to improve user retrieval performance

Due to the exhaustive use of the user_obj the performance suffers
greatly if the users are assigned to large groups. This simple fix adds
a new serializer that does not expose the user_obj within a group.

* core/api: Update schema

Update to the schema based on the new SimpleGroupSerializer

* core/api: Fix black and pylint

* make naming consistent, remove unnecessary fields

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
Co-authored-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Daniel 2022-11-09 11:19:40 +01:00 committed by GitHub
parent a2a4dbe266
commit 85c790728f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 3 deletions

View File

@ -46,7 +46,6 @@ from structlog.stdlib import get_logger
from authentik.admin.api.metrics import CoordinateSerializer from authentik.admin.api.metrics import CoordinateSerializer
from authentik.api.decorators import permission_required from authentik.api.decorators import permission_required
from authentik.core.api.groups import GroupSerializer
from authentik.core.api.used_by import UsedByMixin from authentik.core.api.used_by import UsedByMixin
from authentik.core.api.utils import LinkSerializer, PassiveSerializer, is_dict from authentik.core.api.utils import LinkSerializer, PassiveSerializer, is_dict
from authentik.core.middleware import ( from authentik.core.middleware import (
@ -74,6 +73,26 @@ from authentik.tenants.models import Tenant
LOGGER = get_logger() LOGGER = get_logger()
class UserGroupSerializer(ModelSerializer):
"""Simplified Group Serializer for user's groups"""
attributes = JSONField(required=False)
parent_name = CharField(source="parent.name", read_only=True)
class Meta:
model = Group
fields = [
"pk",
"num_pk",
"name",
"is_superuser",
"parent",
"parent_name",
"attributes",
]
class UserSerializer(ModelSerializer): class UserSerializer(ModelSerializer):
"""User Serializer""" """User Serializer"""
@ -83,7 +102,7 @@ class UserSerializer(ModelSerializer):
groups = PrimaryKeyRelatedField( groups = PrimaryKeyRelatedField(
allow_empty=True, many=True, source="ak_groups", queryset=Group.objects.all() allow_empty=True, many=True, source="ak_groups", queryset=Group.objects.all()
) )
groups_obj = ListSerializer(child=GroupSerializer(), read_only=True, source="ak_groups") groups_obj = ListSerializer(child=UserGroupSerializer(), read_only=True, source="ak_groups")
uid = CharField(read_only=True) uid = CharField(read_only=True)
username = CharField(max_length=150) username = CharField(max_length=150)

View File

@ -37477,7 +37477,7 @@ components:
groups_obj: groups_obj:
type: array type: array
items: items:
$ref: '#/components/schemas/Group' $ref: '#/components/schemas/UserGroup'
readOnly: true readOnly: true
email: email:
type: string type: string
@ -37579,6 +37579,59 @@ components:
- username - username
- upn - upn
type: string type: string
UserGroup:
type: object
description: Simplified Group Serializer for user's groups
properties:
pk:
type: string
format: uuid
readOnly: true
title: Group uuid
num_pk:
type: integer
readOnly: true
name:
type: string
maxLength: 80
is_superuser:
type: boolean
description: Users added to this group will be superusers.
parent:
type: string
format: uuid
nullable: true
parent_name:
type: string
readOnly: true
attributes:
type: object
additionalProperties: {}
required:
- name
- num_pk
- parent_name
- pk
UserGroupRequest:
type: object
description: Simplified Group Serializer for user's groups
properties:
name:
type: string
minLength: 1
maxLength: 80
is_superuser:
type: boolean
description: Users added to this group will be superusers.
parent:
type: string
format: uuid
nullable: true
attributes:
type: object
additionalProperties: {}
required:
- name
UserLoginStage: UserLoginStage:
type: object type: object
description: UserLoginStage Serializer description: UserLoginStage Serializer