Skip to content

Commit 0086ec2

Browse files
committed
Create/Remove users associated with groups
https://pulp.plan.io/issues/7310 closes #7310
1 parent feb4506 commit 0086ec2

File tree

5 files changed

+230
-62
lines changed

5 files changed

+230
-62
lines changed

CHANGES/7310.feature

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Extended endpoint `/pulp/api/v3/groups/:pk/users` to add and remove users from a group.
2+
3+
NOTE: this endpoint is in tech-preview and may change in backwards incompatible ways in the future.

CHANGES/7311.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The `/pulp/api/v3/groups/:pk/permissions` endpoint is available for posting and deleting
2-
permissions.
1+
Extended endpoints `/pulp/api/v3/groups/:pk/model_permissions` and
2+
`/pulp/api/v3/groups/:pk/object_permissions` to add and remove permissions from a group.
33

44
NOTE: this endpoint is in tech-preview and may change in backwards incompatible ways in the future.

pulpcore/app/serializers/user.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from django.contrib.auth import get_user_model
44
from django.contrib.auth.models import Group, Permission
5+
from django.urls import reverse
6+
from guardian.models.models import GroupObjectPermission
57
from rest_framework import serializers
68

79
from pulpcore.app.serializers import IdentityField
@@ -27,15 +29,39 @@ def to_representation(self, obj):
2729
return serializer.data.get("pulp_href")
2830

2931

30-
class ObjectPermissionSerializer(serializers.Serializer):
31-
"""Serializer for user/group object permission."""
32+
class PermissionSerializer(serializers.Serializer):
33+
"""Serializer for User/Group object permission."""
3234

35+
pulp_href = serializers.SerializerMethodField(read_only=True)
36+
id = serializers.SerializerMethodField(read_only=True)
3337
permission = PermissionField(source="*", read_only=True)
3438
obj = ContentObjectField(help_text=_("Content object."), source="*", read_only=True)
3539

40+
def get_id(self, obj):
41+
"""Get model/object permission id."""
42+
return obj.id
43+
44+
def get_pulp_href(self, obj):
45+
"""Get model/object permission pulp_href."""
46+
group_pk = self.context.get("group_pk")
47+
48+
if group_pk and isinstance(obj, Permission):
49+
return reverse("model_permissions-detail", args=[group_pk, obj.pk])
50+
51+
if group_pk and isinstance(obj, GroupObjectPermission):
52+
return reverse("object_permissions-detail", args=[group_pk, obj.pk])
53+
54+
def to_representation(self, obj):
55+
representation = super().to_representation(obj)
56+
57+
if not self.context.get("group_pk"):
58+
representation.pop("pulp_href")
59+
60+
return representation
61+
3662

3763
class UserGroupSerializer(serializers.ModelSerializer):
38-
"""Serializer for user groups."""
64+
"""Serializer for Groups that belong to an User."""
3965

4066
name = serializers.CharField(help_text=_("Name."), max_length=150,)
4167
pulp_href = IdentityField(view_name="groups-detail")
@@ -46,7 +72,7 @@ class Meta:
4672

4773

4874
class UserSerializer(serializers.ModelSerializer):
49-
"""Serializer for user."""
75+
"""Serializer for User."""
5076

5177
pulp_href = IdentityField(view_name="users-detail")
5278
id = serializers.IntegerField(read_only=True)
@@ -83,7 +109,7 @@ class Meta:
83109

84110

85111
class GroupUserSerializer(serializers.ModelSerializer):
86-
"""Serializer for group users."""
112+
"""Serializer for Users that belong to a Group."""
87113

88114
username = serializers.CharField(
89115
help_text=_("Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only."),
@@ -97,7 +123,7 @@ class Meta:
97123

98124

99125
class GroupSerializer(serializers.ModelSerializer):
100-
"""Serializer for group."""
126+
"""Serializer for Group."""
101127

102128
pulp_href = IdentityField(view_name="groups-detail")
103129
id = serializers.IntegerField(read_only=True)
@@ -106,17 +132,3 @@ class GroupSerializer(serializers.ModelSerializer):
106132
class Meta:
107133
model = Group
108134
fields = ("name", "pulp_href", "id")
109-
110-
111-
class PermissionSerializer(serializers.ModelSerializer):
112-
"""Serializer for group."""
113-
114-
pulp_href = IdentityField(view_name="permissions-detail")
115-
id = serializers.IntegerField(read_only=True)
116-
name = serializers.CharField(help_text=_("Name"), max_length=255)
117-
codename = serializers.CharField(help_text=_("Codename"), max_length=100)
118-
content_type_id = serializers.IntegerField(help_text=_("Content type id"))
119-
120-
class Meta:
121-
model = Permission
122-
fields = ("pulp_href", "id", "name", "codename", "content_type_id")

pulpcore/app/viewsets/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@
4949
)
5050
from .task import TaskViewSet, TaskGroupViewSet, WorkerViewSet # noqa
5151
from .upload import UploadViewSet # noqa
52-
from .user import GroupViewSet, PermissionViewSet, UserViewSet # noqa
52+
from .user import ( # noqa
53+
GroupViewSet,
54+
GroupUserViewSet,
55+
GroupModelPermissionViewSet,
56+
GroupObjectPermissionViewSet,
57+
UserViewSet,
58+
)

0 commit comments

Comments
 (0)