Beispiel #1
0
    def __getattr__(self, key):
        # Try to find a sub-group of the given id
        group = self.get(key)
        if group is not None:
            return group

        # Try to find a preference of the given name
        if self.__schema__ and key in self.__schema__:
            marker = object()
            value = self.data.get(key, marker)
            if value is not marker:
                return value

            # There is currently no local entry, so let's go to the next
            # provider and lookup the group and value there.
            nextProvider = component.queryNextUtility(
                self.provider, interfaces.IDefaultPreferenceProvider)

            # No more providers found, so return the schema's default
            if nextProvider is None: 
                return self.__schema__[key].default

            nextGroup = nextProvider.getDefaultPreferenceGroup(self.__id__)
            return getattr(nextGroup, key, self.__schema__[key].default)

        # Nothing found, raise an attribute error
        raise AttributeError("'%s' is not a preference or sub-group." % key)
Beispiel #2
0
    def translate(self, msgid, mapping=None, context=None,
                  target_language=None, default=None):
        """See interface `ITranslationDomain`"""
        if target_language is None and context is not None:
            avail_langs = self.getAvailableLanguages()
            # Let's negotiate the language to translate to. :)
            negotiator = zapi.getUtility(INegotiator, context=self)
            target_language = negotiator.getLanguage(avail_langs, context)

        # Get the translation. Default is the source text itself.
        catalog_names = self._catalogs.get(target_language, [])

        for name in catalog_names:
            catalog = super(TranslationDomain, self).__getitem__(name)
            text = catalog.queryMessage(msgid)
            if text is not None:
                break
        else:
            # If nothing found, delegate to a translation server higher up the
            # tree.
            domain = queryNextUtility(ITranslationDomain, self.domain)
            if domain is not None:
                return domain.translate(msgid, mapping, context,
                                        target_language, default=default)
            if default is None:
                default = msgid
            text = default

        # Now we need to do the interpolation
        return interpolate(text, mapping)
Beispiel #3
0
    def getQueriables(self):
        """Returns an iteratable of queriables.

        Queriables are responsible for providing interfaces to search for
        principals by a set of given parameters (can be different for the
        various queriables). This method will walk up through all of the
        authentication utilities to look for queriables.

        >>> class DummyUtility1:
        ...     implements(IAuthentication)
        ...     __parent__ = None
        ...     def __repr__(self): return 'dummy1'
        >>> dummy1 = DummyUtility1()

        >>> class DummyUtility2:
        ...     implements(ISourceQueriables, IAuthentication)
        ...     __parent__ = None
        ...     def getQueriables(self):
        ...         return ('1', 1), ('2', 2), ('3', 3)
        >>> dummy2 = DummyUtility2()

        >>> class DummyUtility3(DummyUtility2):
        ...     implements(IAuthentication)
        ...     def getQueriables(self):
        ...         return ('4', 4),
        >>> dummy3 = DummyUtility3()

        >>> from zope.app.component.testing import testingNextUtility
        >>> testingNextUtility(dummy1, dummy2, IAuthentication)
        >>> testingNextUtility(dummy2, dummy3, IAuthentication)

        >>> temp = zapi.getUtility
        >>> zapi.getUtility = lambda iface: dummy1

        >>> source = PrincipalSource()
        >>> list(source.getQueriables())
        [(u'0', dummy1), (u'1.1', 1), (u'1.2', 2), (u'1.3', 3), (u'2.4', 4)]

        >>> zapi.getUtility = temp
        """
        i = 0
        auth = zapi.getUtility(IAuthentication)
        yielded = []
        while True:
            queriables = ISourceQueriables(auth, None)
            if queriables is None:
                yield unicode(i), auth
            else:
                for qid, queriable in queriables.getQueriables():
                    # ensure that we dont return same yielded utility more
                    # then once
                    if queriable not in yielded:
                        yield unicode(i)+'.'+unicode(qid), queriable
                        yielded.append(queriable)
            auth = queryNextUtility(auth, IAuthentication)
            if auth is None:
                break
            i += 1
Beispiel #4
0
 def __getitem__(self, key):
     try:
         return self.data[key]
     except KeyError:
         # We failed locally: delegate to a higher-level utility.
         utility = queryNextUtility(self, IPrincipalAnnotationUtility)
         if utility is not None:
             annotations = utility.getAnnotationsById(self.principalId)
             return annotations[key]
         raise
Beispiel #5
0
 def getPrincipal(self, id):
     if not id.startswith(self.prefix):
         next = queryNextUtility(self, IAuthentication)
         if next is None:
             raise PrincipalLookupError(id)
         return next.getPrincipal(id)
     id = id[len(self.prefix):]
     for name, authplugin in self.getAuthenticatorPlugins():
         info = authplugin.principalInfo(id)
         if info is None:
             continue
         info.credentialsPlugin = None
         info.authenticatorPlugin = authplugin
         principal = interfaces.IFoundPrincipalFactory(info)(self)
         principal.id = self.prefix + info.id
         return principal
     next = queryNextUtility(self, IAuthentication)
     if next is not None:
         return next.getPrincipal(self.prefix + id)
     raise PrincipalLookupError(id)
Beispiel #6
0
 def getPrincipal(self, id):
     if not id.startswith(self.prefix):
         next = queryNextUtility(self, IAuthentication)
         if next is None:
             raise PrincipalLookupError(id)
         return next.getPrincipal(id)
     id = id[len(self.prefix):]
     for name in self.authenticatorPlugins:
         authplugin = component.queryUtility(
             interfaces.IAuthenticatorPlugin, name, context=self)
         if authplugin is None:
             continue
         info = authplugin.principalInfo(id)
         if info is None:
             continue
         principal = interfaces.IFoundPrincipalFactory(info)(self)
         principal.id = self.prefix + info.id
         return principal
     next = queryNextUtility(self, IAuthentication)
     if next is not None:
         return next.getPrincipal(self.prefix + id)
     raise PrincipalLookupError(id)
Beispiel #7
0
    def logout(self, request):
        challengeProtocol = None

        for name, credplugin in self.getCredentialsPlugins():
            protocol = getattr(credplugin, 'challengeProtocol', None)
            if challengeProtocol is None or protocol == challengeProtocol:
                if credplugin.logout(request):
                    if protocol is None:
                        return
                    elif challengeProtocol is None:
                        challengeProtocol = protocol

        if challengeProtocol is None:
            next = queryNextUtility(self, IAuthentication)
            if next is not None:
                next.logout(request)
    def unauthorized(self, id, request):
        # see #415 - workaround for MS Office products opening links
        # without a session in a browser, preventing logged users to see them
        ua = request.get('HTTP_USER_AGENT')
        uri = request.get('REQUEST_URI')

        if re.search('[^\w](Word|Excel|PowerPoint|ms-office)([^\w]|\z)', ua, re.I):
            return request.response.redirect(uri)

        login = queryMultiAdapter((self, request), ILoginService)
        if login is not None:
            if login.challenge():
                return

        next = queryNextUtility(self, IAuthentication)
        if next is not None:
            next.unauthorized(id, request)
Beispiel #9
0
    def logout(self, request):
        challengeProtocol = None

        for name in self.credentialsPlugins:
            credplugin = component.queryUtility(interfaces.ICredentialsPlugin,
                                                name)
            if credplugin is None:
                continue
            protocol = getattr(credplugin, 'challengeProtocol', None)
            if challengeProtocol is None or protocol == challengeProtocol:
                if credplugin.logout(request):
                    if protocol is None:
                        return
                    elif challengeProtocol is None:
                        challengeProtocol = protocol

        if challengeProtocol is None:
            next = queryNextUtility(self, IAuthentication)
            if next is not None:
                next.logout(request)