core: allow changing of groups a user is in from user api

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-08-10 19:31:30 +02:00
parent de9d483b9f
commit ec95a2bddc
2 changed files with 24 additions and 2 deletions

View File

@ -20,6 +20,7 @@ from rest_framework.serializers import (
BooleanField, BooleanField,
ListSerializer, ListSerializer,
ModelSerializer, ModelSerializer,
PrimaryKeyRelatedField,
Serializer, Serializer,
ValidationError, ValidationError,
) )
@ -33,7 +34,7 @@ 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 SESSION_IMPERSONATE_ORIGINAL_USER, SESSION_IMPERSONATE_USER from authentik.core.middleware import SESSION_IMPERSONATE_ORIGINAL_USER, SESSION_IMPERSONATE_USER
from authentik.core.models import Token, TokenIntents, User from authentik.core.models import Group, Token, TokenIntents, User
from authentik.events.models import EventAction from authentik.events.models import EventAction
from authentik.stages.email.models import EmailStage from authentik.stages.email.models import EmailStage
from authentik.stages.email.tasks import send_mails from authentik.stages.email.tasks import send_mails
@ -49,7 +50,10 @@ class UserSerializer(ModelSerializer):
is_superuser = BooleanField(read_only=True) is_superuser = BooleanField(read_only=True)
avatar = CharField(read_only=True) avatar = CharField(read_only=True)
attributes = JSONField(validators=[is_dict], required=False) attributes = JSONField(validators=[is_dict], required=False)
groups = ListSerializer(child=GroupSerializer(), read_only=True, source="ak_groups") groups = PrimaryKeyRelatedField(
allow_empty=False, many=True, source="ak_groups", queryset=Group.objects.all()
)
groups_obj = ListSerializer(child=GroupSerializer(), read_only=True, source="ak_groups")
uid = CharField(read_only=True) uid = CharField(read_only=True)
class Meta: class Meta:
@ -63,6 +67,7 @@ class UserSerializer(ModelSerializer):
"last_login", "last_login",
"is_superuser", "is_superuser",
"groups", "groups",
"groups_obj",
"email", "email",
"avatar", "avatar",
"attributes", "attributes",

View File

@ -27634,6 +27634,11 @@ components:
type: string type: string
format: date-time format: date-time
nullable: true nullable: true
groups:
type: array
items:
type: string
format: uuid
email: email:
type: string type: string
format: email format: email
@ -29808,6 +29813,11 @@ components:
type: boolean type: boolean
readOnly: true readOnly: true
groups: groups:
type: array
items:
type: string
format: uuid
groups_obj:
type: array type: array
items: items:
$ref: '#/components/schemas/Group' $ref: '#/components/schemas/Group'
@ -29829,6 +29839,7 @@ components:
required: required:
- avatar - avatar
- groups - groups
- groups_obj
- is_superuser - is_superuser
- name - name
- pk - pk
@ -30105,6 +30116,11 @@ components:
type: string type: string
format: date-time format: date-time
nullable: true nullable: true
groups:
type: array
items:
type: string
format: uuid
email: email:
type: string type: string
format: email format: email
@ -30114,6 +30130,7 @@ components:
type: object type: object
additionalProperties: {} additionalProperties: {}
required: required:
- groups
- name - name
- username - username
UserSelf: UserSelf: