Skip to content

Commit e0f9441

Browse files
authored
Provide own groups if requesting own user data (#1582)
1 parent 0ba390d commit e0f9441

File tree

6 files changed

+46
-17
lines changed

6 files changed

+46
-17
lines changed

news/1581.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Request of own user data provides joined groups @ksuess

src/plone/restapi/serializer/user.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,19 @@ class SerializeUserToJson(BaseSerializer):
6868
def __call__(self):
6969
data = super().__call__()
7070
user = self.context
71-
gtool = getToolByName(self.context, "portal_groups")
72-
groupIds = user.getGroups()
73-
groups = [gtool.getGroupById(grp) for grp in groupIds]
74-
groups = [{"id": grp.id, "title": grp.title or grp.id} for grp in groups]
75-
batch = HypermediaBatch(self.request, groups)
76-
groups_data = {
77-
"@id": batch.canonical_url,
78-
"items_total": batch.items_total,
79-
"items": sorted(batch, key=lambda x: x["title"]),
80-
}
81-
if batch.links:
82-
groups_data["batching"] = batch.links
71+
gtool = getToolByName(self.context, "portal_groups", None)
72+
if gtool:
73+
groupIds = user.getGroups()
74+
groups = [gtool.getGroupById(grp) for grp in groupIds]
75+
groups = [{"id": grp.id, "title": grp.title or grp.id} for grp in groups]
8376

84-
data["groups"] = groups_data
77+
batch = HypermediaBatch(self.request, groups)
78+
groups_data = {
79+
"@id": batch.canonical_url,
80+
"items_total": batch.items_total,
81+
"items": sorted(batch, key=lambda x: x["title"]),
82+
}
83+
if batch.links:
84+
groups_data["batching"] = batch.links
85+
data["groups"] = groups_data
8586
return data

src/plone/restapi/services/users/get.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from plone.app.workflow.browser.sharing import merge_search_results
55
from plone.namedfile.utils import stream_data
66
from plone.restapi.interfaces import ISerializeToJson
7-
from plone.restapi.interfaces import ISerializeToJsonSummary
87
from plone.restapi.services import Service
98
from Products.CMFCore.utils import getToolByName
109
from Products.CMFPlone.utils import normalizeString
@@ -210,9 +209,7 @@ def reply(self):
210209
if not user:
211210
self.request.response.setStatus(404)
212211
return
213-
serializer = queryMultiAdapter(
214-
(user, self.request), ISerializeToJsonSummary
215-
)
212+
serializer = queryMultiAdapter((user, self.request), ISerializeToJson)
216213
return serializer()
217214
else:
218215
self.request.response.setStatus(401)

src/plone/restapi/tests/http-examples/users_authorized_get.resp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ Content-Type: application/json
66
"description": "Professor of Linguistics",
77
"email": "[email protected]",
88
"fullname": "Noam Avram Chomsky",
9+
"groups": {
10+
"@id": "http://localhost:55001/plone/@users/noam",
11+
"items": [
12+
{
13+
"id": "AuthenticatedUsers",
14+
"title": "AuthenticatedUsers"
15+
}
16+
],
17+
"items_total": 1
18+
},
919
"home_page": "web.mit.edu/chomsky",
1020
"id": "noam",
1121
"location": "Cambridge, MA",

src/plone/restapi/tests/http-examples/users_get.resp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ Content-Type: application/json
66
"description": "Professor of Linguistics",
77
"email": "[email protected]",
88
"fullname": "Noam Avram Chomsky",
9+
"groups": {
10+
"@id": "http://localhost:55001/plone/@users/noam",
11+
"items": [
12+
{
13+
"id": "AuthenticatedUsers",
14+
"title": "AuthenticatedUsers"
15+
}
16+
],
17+
"items_total": 1
18+
},
919
"home_page": "web.mit.edu/chomsky",
1020
"id": "noam",
1121
"location": "Cambridge, MA",

src/plone/restapi/tests/http-examples/users_update_portrait_get.resp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ Content-Type: application/json
66
"description": null,
77
"email": "[email protected]",
88
"fullname": null,
9+
"groups": {
10+
"@id": "http://localhost:55001/plone/@users/noam",
11+
"items": [
12+
{
13+
"id": "AuthenticatedUsers",
14+
"title": "AuthenticatedUsers"
15+
}
16+
],
17+
"items_total": 1
18+
},
919
"home_page": null,
1020
"id": "noam",
1121
"location": null,

0 commit comments

Comments
 (0)