From 90439b1de6155bb9ec05ec91b3bfc37fb4b8dc50 Mon Sep 17 00:00:00 2001 From: Luca Bellenghi Date: Thu, 12 Sep 2024 16:57:50 +0200 Subject: [PATCH] override group adapters --- .../volto/restapi/serializer/configure.zcml | 2 + .../volto/restapi/serializer/group.py | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/redturtle/volto/restapi/serializer/group.py diff --git a/src/redturtle/volto/restapi/serializer/configure.zcml b/src/redturtle/volto/restapi/serializer/configure.zcml index f27a79e..e0c6359 100644 --- a/src/redturtle/volto/restapi/serializer/configure.zcml +++ b/src/redturtle/volto/restapi/serializer/configure.zcml @@ -30,4 +30,6 @@ + + diff --git a/src/redturtle/volto/restapi/serializer/group.py b/src/redturtle/volto/restapi/serializer/group.py new file mode 100644 index 0000000..5559aa3 --- /dev/null +++ b/src/redturtle/volto/restapi/serializer/group.py @@ -0,0 +1,39 @@ +from plone.restapi.batching import HypermediaBatch +from plone.restapi.interfaces import ISerializeToJson +from Products.PlonePAS.interfaces.group import IGroupData +from zope.component import adapter +from zope.interface import implementer +from redturtle.volto.interfaces import IRedturtleVoltoLayer +from Products.PluggableAuthService.interfaces.plugins import IRolesPlugin +from plone.restapi.serializer.group import BaseSerializer +from plone import api + + +@implementer(ISerializeToJson) +@adapter(IGroupData, IRedturtleVoltoLayer) +class SerializeGroupToJson(BaseSerializer): + def __call__(self): + data = super().__call__() + group = self.context + if group.id == 'AuthenticatedUsers': + data['roles'] = [] + acl = api.portal.get_tool('acl_users') + rolemakers = acl.plugins.listPlugins(IRolesPlugin) + for rolemaker_id, rolemaker in rolemakers: + roles = rolemaker.getRolesForPrincipal(group) or () + data['roles'].extend(roles) + + members = group.getGroupMemberIds() + batch = HypermediaBatch(self.request, members) + members_data = { + "@id": batch.canonical_url, + "items_total": batch.items_total, + "items": sorted(batch), + } + if batch.links: + members_data["batching"] = batch.links + + data["members"] = members_data + return data + +