[Zope-Checkins] CVS: Zope/lib/python/AccessControl - SimpleObjectPolicies.py:1.12.70.1.2.1 ZopeGuards.py:1.16.2.1.2.1

Jim Fulton jim at zope.com
Fri Jan 16 14:43:49 EST 2004


Update of /cvs-repository/Zope/lib/python/AccessControl
In directory cvs.zope.org:/tmp/cvs-serv30706/lib/python/AccessControl

Modified Files:
      Tag: caz_dict_list_redo
	SimpleObjectPolicies.py ZopeGuards.py 
Log Message:
Changed the strategy for handling special list and dict methods.


=== Zope/lib/python/AccessControl/SimpleObjectPolicies.py 1.12.70.1 => 1.12.70.1.2.1 ===
--- Zope/lib/python/AccessControl/SimpleObjectPolicies.py:1.12.70.1	Thu Jan  8 18:33:43 2004
+++ Zope/lib/python/AccessControl/SimpleObjectPolicies.py	Fri Jan 16 14:43:18 2004
@@ -10,39 +10,34 @@
 # FOR A PARTICULAR PURPOSE
 #
 ##############################################################################
-"""Collect rules for access to objects that don\'t have roles.
+"""Collect some policies for s few built-in container objects.
 
-The rules are expressed as a mapping from type -> assertion
+   This module defines a registry, ContainerAssertions, that contains
+   some data that is used by the zope security policy and by zope
+   guards when considering whether to allow access to objects.
 
-An assertion can be:
+   The zope security policy and the zope guards use this data in
+   different ways.  The source code for those modules is the best
+   source of documentation for their use. :(
 
-  - A dict
+   The requirements for the values of ContainerAssertions is provided
+   here.
 
-  - A callable
+   The keys of ContainerAssertions are built-in types.
 
-  - Something with a truth value
+   The values must be one of:
 
-If the assertion is a callable, then it will be called with
-a name being accessed and the name used.  Its return value is ignored,
-but in may veto an access by raising an exception.
+   1 -- In this case access to attributes of instances of the type are
+        always allowed.  Access to items may be allowed, depending on
+        the item type and value. (Use the source.)
 
-If the assertion is a dictionary, then the keys are attribute names.
-The values may be callables or objects with boolean values. If a value
-is callable, it will be called with the object we are accessing an
-attribute of and the attribute name. It should return an attribute
-value. Callables are often used to returned guarded versions of
-methods.  Otherwise, accesses are allowed if values in this dictionary
-are true and disallowed if the values are false or if an item for an
-attribute name is not present.
+   a callable -- In this case, the callable will be called with the
+        attribute name and value and should return a true callable or
+        1. If 1 is returned, then access may be granted, otherwise the
+        second callable will be called with the original object, and
+        the attribute name, and should return an object that is
+        returned as the value of the attribute.
 
-If the assertion is not a dict and is not callable, then access to
-unprotected attributes is allowed if the assertion is true, and
-disallowed otherwise.
-
-XXX This descrition doesn't actually match what's done in ZopeGuards
-or in ZopeSecurityPolicy. :(
-
-$Id$
 """
 
 _noroles = [] # this is imported from various places
@@ -52,25 +47,6 @@
 # Allow access to unprotected attributes
 Record.Record.__allow_access_to_unprotected_subobjects__=1
 
-# ContainerAssertions are used by cAccessControl to check access to
-# attributes of container types, like dict, list, or string.
-# ContainerAssertions maps types to a either a dict, a function, or a
-# simple boolean value.  When guarded_getattr checks the type of its
-# first argument against ContainerAssertions, and invokes checking
-# logic depending on what value it finds.
-
-# If the value for a type is:
-#   - a boolean value:
-#      - the value determines whether access is allowed
-#   - a function (or callable):
-#      - The function is called with the name of the attribute and
-#        the actual attribute value, then the value is returned.
-#        The function can raise an exception.
-#   - a dict:
-#      - The dict maps attribute names to boolean values or functions.
-#        The boolean values behave as above, but the functions do not.
-#        The value returned for attribute access is the result of
-#        calling the function with the object and the attribute name.
 
 ContainerAssertions={
     type(()): 1,


=== Zope/lib/python/AccessControl/ZopeGuards.py 1.16.2.1 => 1.16.2.1.2.1 ===
--- Zope/lib/python/AccessControl/ZopeGuards.py:1.16.2.1	Thu Jan  8 18:33:43 2004
+++ Zope/lib/python/AccessControl/ZopeGuards.py	Fri Jan 16 14:43:18 2004
@@ -154,16 +154,20 @@
 # See comment in SimpleObjectPolicies for an explanation of what the
 # dicts below actually mean.
 
-ContainerAssertions[type({})] = {
-    'clear':1, 'copy':1, 'fromkeys':1, 'get':get_dict_get, 'has_key':1,
-    'items':1, 'iteritems':1, 'keys':1,
+dict_handlers = {
+    'get':get_dict_get, 'has_key':1,
     'iterkeys': get_iter,  'itervalues':get_iter,
-    'pop':get_dict_pop, 'popitem':1, 'setdefault':1, 'update':1, 'values':1}
+    'pop':get_dict_pop, }
+def dict_attr_factory(attr, v, handlers = dict_handlers):
+    return handlers.get(attr, 1)
 
-ContainerAssertions[type([])] = {
-    'append':1, 'count':1, 'extend':1, 'index':1, 'insert':1,
-    'pop':get_list_pop, 'remove':1, 'reverse':1, 'sort':1}
+ContainerAssertions[type({})] = dict_attr_factory
 
+list_handlers = {'pop':get_list_pop, }
+def list_attr_factory(attr, v, handlers = list_handlers):
+    return handlers.get(attr, 1)
+
+ContainerAssertions[type([])] = list_attr_factory
 
 # This implementation of a "safe" iterator uses a global guard()
 # function to implement the actual guard.  This check is defined as a




More information about the Zope-Checkins mailing list