##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""Define Zope's default security policy
$Id$"""
from types import MethodType
# AccessControl.Implementation inserts:
# ZopeSecurityPolicy, getRoles, rolesForPermissionOn
_norolesの実装は[]である。Noneと[]の扱いはちゃんとチェックしたほうがよさそう。roleを要求していない(いかなるroleもOK、allow any)のか、roleがない(allow none, 該当なしの空集合)のかなど。
from AccessControl.SimpleObjectPolicies import _noroles
rolesForPermissionOn = None # XXX: avoid import loop
tuple_or_list = tuple, list
def getRoles(container, name, value, default):
global rolesForPermissionOn # XXX: avoid import loop
if rolesForPermissionOn is None:
from PermissionRole import rolesForPermissionOn
roles = getattr(value, '__roles__', _noroles)
__roles__ attributeをもっていないケース。
if roles is _noroles:
if not name or not isinstance(name, basestring):
return default
im_selfはメソッドを結合されたインスタンスである。ここを参照のこと。つまりvalueがmethodだと、そのmethodがbindされているインスタンスを取り出す。
if type(value) is MethodType:
container = value.im_self
__class__はインスタンスのclassを取り出す。そしてそのclassはnameごとにroleを提供できる。
cls = getattr(container, '__class__', None)
if cls is None:
return default
roles = getattr(cls, name+'__roles__', _noroles)
if roles is _noroles:
return default
valueを保持しているcontainerを基準にrolesを決める。
value = container
rolesはsequenceである可能性が高い。そうでないときはrolesForPermissionOn attributeを持っているとvalueを見て決めているのだろう。
if roles is None or isinstance(roles, tuple_or_list):
return roles
rolesForPermissionOn = getattr(roles, 'rolesForPermissionOn', None)
if rolesForPermissionOn is not None:
roles = rolesForPermissionOn(value)
お疲れ様。
return roles
0 件のコメント:
コメントを投稿