Skip to content

Commit

Permalink
override group adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-bellenghi committed Sep 12, 2024
1 parent a4f91f0 commit 90439b1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/redturtle/volto/restapi/serializer/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@
<adapter factory=".collection.SerializeCollectionToJson" />
</configure>

<adapter factory=".group.SerializeGroupToJson" />

</configure>
39 changes: 39 additions & 0 deletions src/redturtle/volto/restapi/serializer/group.py
Original file line number Diff line number Diff line change
@@ -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


0 comments on commit 90439b1

Please sign in to comment.