Skip to content

Commit 29dd8bc

Browse files
committed
Add roles from presenters adapter to be used with event session
1 parent 215cb7b commit 29dd8bc

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

backend/src/collective/techevent/behaviors/configure.zcml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,16 @@
2626
provides=".session.IEventSession"
2727
/>
2828

29+
<plone:behavior
30+
name="collective.techevent.presenter_roles"
31+
title="Tech Event: Apply presenter roles"
32+
description=""
33+
provides=".presenter.IPresenterLocalRoles"
34+
/>
35+
36+
<adapter
37+
factory=".presenter.PresenterRoleProvider"
38+
name="collective.techevent.presenter_roles"
39+
/>
40+
2941
</configure>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from borg.localrole.interfaces import ILocalRoleProvider
2+
from plone import api
3+
from z3c.relationfield import RelationValue
4+
from zope.component import adapter
5+
from zope.interface import implementer
6+
from zope.interface import Interface
7+
8+
9+
class IPresenterLocalRoles(Interface):
10+
"""Marker interface for the local roles of a presenter."""
11+
12+
13+
@implementer(ILocalRoleProvider)
14+
@adapter(IPresenterLocalRoles)
15+
class PresenterRoleProvider:
16+
def __init__(self, context):
17+
self.context = context
18+
19+
def getRoles(self, user_id):
20+
"""
21+
Return the roles assigned to the given user ID in the context of presenters for the current event session.
22+
"""
23+
mtool = api.portal.get_tool("portal_membership")
24+
user = mtool.getMemberById(user_id)
25+
if not user:
26+
return []
27+
roles = set()
28+
presenters = getattr(self.context, "presenters", [])
29+
for presenter_relation in presenters:
30+
if (
31+
isinstance(presenter_relation, RelationValue)
32+
and presenter_relation.to_object
33+
):
34+
presenter = presenter_relation.to_object
35+
roles.update(user.getRolesInContext(presenter))
36+
return list(roles)
37+
38+
def getAllRoles(self):
39+
"""
40+
Retrieve all roles assigned to users in the context of presenters for the current event session.
41+
42+
This method merges roles from all presenters for each user. It does not consider additional role adapters.
43+
"""
44+
roles = {}
45+
presenters = getattr(self.context, "presenters", [])
46+
for presenter_relation in presenters:
47+
if (
48+
isinstance(presenter_relation, RelationValue)
49+
and presenter_relation.to_object
50+
):
51+
presenter = presenter_relation.to_object
52+
local_roles = presenter.get_local_roles()
53+
for user_id, user_roles in local_roles:
54+
if user_id not in roles:
55+
roles[user_id] = set()
56+
roles[user_id].update(user_roles)
57+
return [(user_id, tuple(user_roles)) for user_id, user_roles in roles.items()]

0 commit comments

Comments
 (0)