src/pyams_form/security.py
changeset 16 2f2596eee744
child 38 cdc30169fac1
equal deleted inserted replaced
15:6c594e84ec05 16:2f2596eee744
       
     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_form.interfaces.form import IFormSecurityContext, IFormContextPermissionChecker
       
    20 
       
    21 # import packages
       
    22 
       
    23 
       
    24 class ProtectedFormObjectMixin(object):
       
    25     """Form object protected by a given permission"""
       
    26 
       
    27     @property
       
    28     def permission(self):
       
    29         registry = self.request.registry
       
    30         checker = None
       
    31         context = IFormSecurityContext(self, None)
       
    32         if context is None:
       
    33             context = self.context
       
    34         view = getattr(self, '__parent__', None) or getattr(self, 'view', None) or getattr(self, 'table', None)
       
    35         if view is not None:
       
    36             checker = registry.queryMultiAdapter((context, self.request, view), IFormContextPermissionChecker)
       
    37         if checker is None:
       
    38             checker = registry.queryAdapter(context, IFormContextPermissionChecker)
       
    39         if checker is not None:
       
    40             return checker.edit_permission