Skip to content

Commit

Permalink
Provide own groups if requesting own user data (#1582)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksuess authored Feb 14, 2023
1 parent 0ba390d commit e0f9441
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
1 change: 1 addition & 0 deletions news/1581.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Request of own user data provides joined groups @ksuess
27 changes: 14 additions & 13 deletions src/plone/restapi/serializer/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,19 @@ class SerializeUserToJson(BaseSerializer):
def __call__(self):
data = super().__call__()
user = self.context
gtool = getToolByName(self.context, "portal_groups")
groupIds = user.getGroups()
groups = [gtool.getGroupById(grp) for grp in groupIds]
groups = [{"id": grp.id, "title": grp.title or grp.id} for grp in groups]
batch = HypermediaBatch(self.request, groups)
groups_data = {
"@id": batch.canonical_url,
"items_total": batch.items_total,
"items": sorted(batch, key=lambda x: x["title"]),
}
if batch.links:
groups_data["batching"] = batch.links
gtool = getToolByName(self.context, "portal_groups", None)
if gtool:
groupIds = user.getGroups()
groups = [gtool.getGroupById(grp) for grp in groupIds]
groups = [{"id": grp.id, "title": grp.title or grp.id} for grp in groups]

data["groups"] = groups_data
batch = HypermediaBatch(self.request, groups)
groups_data = {
"@id": batch.canonical_url,
"items_total": batch.items_total,
"items": sorted(batch, key=lambda x: x["title"]),
}
if batch.links:
groups_data["batching"] = batch.links
data["groups"] = groups_data
return data
5 changes: 1 addition & 4 deletions src/plone/restapi/services/users/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from plone.app.workflow.browser.sharing import merge_search_results
from plone.namedfile.utils import stream_data
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.interfaces import ISerializeToJsonSummary
from plone.restapi.services import Service
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import normalizeString
Expand Down Expand Up @@ -210,9 +209,7 @@ def reply(self):
if not user:
self.request.response.setStatus(404)
return
serializer = queryMultiAdapter(
(user, self.request), ISerializeToJsonSummary
)
serializer = queryMultiAdapter((user, self.request), ISerializeToJson)
return serializer()
else:
self.request.response.setStatus(401)
Expand Down
10 changes: 10 additions & 0 deletions src/plone/restapi/tests/http-examples/users_authorized_get.resp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ Content-Type: application/json
"description": "Professor of Linguistics",
"email": "[email protected]",
"fullname": "Noam Avram Chomsky",
"groups": {
"@id": "http://localhost:55001/plone/@users/noam",
"items": [
{
"id": "AuthenticatedUsers",
"title": "AuthenticatedUsers"
}
],
"items_total": 1
},
"home_page": "web.mit.edu/chomsky",
"id": "noam",
"location": "Cambridge, MA",
Expand Down
10 changes: 10 additions & 0 deletions src/plone/restapi/tests/http-examples/users_get.resp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ Content-Type: application/json
"description": "Professor of Linguistics",
"email": "[email protected]",
"fullname": "Noam Avram Chomsky",
"groups": {
"@id": "http://localhost:55001/plone/@users/noam",
"items": [
{
"id": "AuthenticatedUsers",
"title": "AuthenticatedUsers"
}
],
"items_total": 1
},
"home_page": "web.mit.edu/chomsky",
"id": "noam",
"location": "Cambridge, MA",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ Content-Type: application/json
"description": null,
"email": "[email protected]",
"fullname": null,
"groups": {
"@id": "http://localhost:55001/plone/@users/noam",
"items": [
{
"id": "AuthenticatedUsers",
"title": "AuthenticatedUsers"
}
],
"items_total": 1
},
"home_page": null,
"id": "noam",
"location": null,
Expand Down

0 comments on commit e0f9441

Please sign in to comment.