|
| 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