src/pyams_security/index.py
changeset 42 07229ac2497b
equal deleted inserted replaced
41:905e30f5467b 42:07229ac2497b
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from pyams_security.interfaces import IProtectedObject
       
    20 
       
    21 # import packages
       
    22 from hypatia.keyword import KeywordIndex
       
    23 
       
    24 
       
    25 class PrincipalsRoleIndex(KeywordIndex):
       
    26     """Principals role index"""
       
    27 
       
    28     def __init__(self, role_id, family=None):
       
    29         KeywordIndex.__init__(self, role_id, family)
       
    30         self.role_id = role_id
       
    31 
       
    32     def discriminate(self, obj, default):
       
    33         protected_object = IProtectedObject(obj, None)
       
    34         if protected_object is None:
       
    35             return default
       
    36         return protected_object.get_principals(self.role_id)