from borg.localrole.interfaces import ILocalRoleProvider
from ploneintranet.news.content import INewsSection
from zope.component import adapter
from zope.interface import implementer


@implementer(ILocalRoleProvider)
@adapter(INewsSection)
class NewsFromFeedCreatorLocalRoleAdapter:
    """This adapter is invoked by the borg_localroles plugin to grant the
    roles needed to create a news item from a feed.
    """

    _granted_roles = ["Contributor", "Reader", "Reviewer"]

    def __init__(self, context):
        self.context = context

    def getRoles(self, principalid):
        if not principalid:
            return []
        news_from_feed_creator = getattr(self.context, "news_from_feed_creator", None)
        if principalid == news_from_feed_creator:
            return self._granted_roles
        return []

    def getAllRoles(self):
        news_from_feed_creator = getattr(self.context, "news_from_feed_creator", None)
        if not news_from_feed_creator:
            return []
        return [(news_from_feed_creator, self._granted_roles)]
