1
##############################################################################
3
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
13
##############################################################################
14
"""Utilities for testing form machinery
16
$Id: utils.py 29405 2005-03-07 18:22:16Z poster $
18
from zope.interface.interfaces import IMethod
19
from zope.security.interfaces import ForbiddenAttribute, Unauthorized
20
import zope.security.checker
21
from zope.schema import getFieldsInOrder
23
class DummyChecker(object):
24
"""a checker for testing that requires explicit declarations
26
requires explicit declaration of what is and is not authorized; does not
27
require testing machinery to set up an interaction or a request.
29
To instantiate, pass two dictionaries, the first for get access attribute
30
protection, and the second for set access attribute protection. keys
31
should be the attribute names, and values should be boolean True and
32
False, where True indicates authorized and False, unauthorized. Any
33
attributes that are not explicitly set and, in the case of get protection,
34
are not in the zope.security.checker._available_by_default list,
35
will cause ForbiddenAttribute to be raised when the name is checked, as
36
with the real zope.security checkers.
38
def __init__(self, getnames, setnames):
39
self.getnames = getnames
40
self.setnames = setnames
41
def check_getattr(self, obj, name):
42
if name not in zope.security.checker._available_by_default:
44
val = self.getnames[name]
46
raise ForbiddenAttribute
51
def check_setattr(self, obj, name):
53
val = self.setnames[name]
55
raise ForbiddenAttribute
59
def proxy(self, value):
62
def SchemaChecker(schema, readonly=False):
63
"""returns a checker that allows read and write access to fields in schema.
67
for name, field in getFieldsInOrder(schema):
69
if not field.readonly:
70
if IMethod.providedBy(field):
71
get[field.writer.__name__] = True
77
return DummyChecker(get, set)
79
def securityWrap(ob, schema, readonly=False):
80
return zope.security.checker.Proxy(ob, SchemaChecker(schema, readonly))