File: PermissionRolePatch.py

package info (click to toggle)
zope-verbosesecurity 0.6-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 104 kB
  • ctags: 102
  • sloc: python: 613; makefile: 34; sh: 23
file content (118 lines) | stat: -rw-r--r-- 3,467 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
__doc__="""Patch of PermissionRole.

This patch exposes the permission name for a role computation.

$Id: PermissionRolePatch.py,v 1.4 2004/06/17 02:38:44 shane Exp $
"""

import sys
from ExtensionClass import Base
from AccessControl.PermissionRole \
     import PermissionRole, _what_not_even_god_should_do
from zLOG import LOG, ERROR


try:
    # Patch PermissionRole.

    def __of__(self, parent):
        r = imPermissionRole()
        r._p = self._p
        r._pa = parent
        r._d = self._d
        p = getattr(parent, 'aq_inner', None)
        if p is not None:
            return r.__of__(p)
        else:
            return r

    PermissionRole.__of__ = __of__

except:
    LOG('VerboseSecurity', ERROR, "Unable to patch PermissionRole.",
        error=sys.exc_info())


class imPermissionRole(Base):
    """Implement permission-based roles"""

    def __of__(self, parent):
        obj = parent
        n = self._p
        # The next line includes the permission name
        # in the allowable roles.
        r = [n]
        while 1:
            if hasattr(obj, n):
                roles = getattr(obj, n)

                if roles is None:
                    return 'Anonymous',

                t = roles.__class__

                if t is tuple:
                    # If we get a tuple, then we don't acquire
                    if r is None:
                        return roles
                    return r + list(roles)

                if issubclass(t, basestring):
                    # We found roles set to a name.  Start over
                    # with the new permission name.  If the permission
                    # name is '', then treat as private!
                    if roles:
                        if roles != n:
                            n = roles
                        # If we find a name that is the same as the
                        # current name, we just ignore it.
                        roles = None
                    else:
                        return _what_not_even_god_should_do

                elif roles:
                    if r is None: r = list(roles)
                    else: r = r + list(roles)

            obj = getattr(obj, 'aq_inner', None)
            if obj is None:
                break
            obj = obj.aq_parent

        if r is None or len(r) < 2:
            r = self._d

        return r

    # The following methods are needed in the unlikely case that an unwrapped
    # object is accessed:
    def __getitem__(self, i):
        try:
            v = self._v
        except:
            v = self._v = self.__of__(self._pa)
            del self._pa

        return v[i]

    def __len__(self):
        try:
            v = self._v
        except:
            v = self._v = self.__of__(self._pa)
            del self._pa

        return len(v)