*** empty log message ***
[pli.git] / pli / misc / acl.py
blob6cb4cbde0a7cdf18f9a1aa18f3fc7f91910c9e64
1 #=======================================================================
3 __version__ = '''0.0.12'''
4 __sub_version__ = '''20050110052820'''
5 __copyright__ = '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
10 __doc__ = '''
11 General ACL Protocol:
12 objects that want to use this protocol may define the following
13 attributes:
15 __public__ : if True will render the object accessible;
16 if False will prevent access to the object;
17 (default: True)
18 __public_attrs__ : will define a list of attributes that are to be visible.
19 __private_attrs__ : will define a list of attributes that are not to be visible.
20 __writable_attrs__ : will define a list of attributes that are writable.
21 __readonly_attrs__ : will define a list of attributes that are not writable.
23 [not done]
24 __global_methods_allowed__
25 : will define the allowed global methods for the current object.
26 if True allow all...
27 __global_methods_disallowed__
28 : will define the disallowed global methods for the current object.
29 if True disallow all... (takes priority over __global_methods_allowed__)
31 TODO do global method acl for attrs...
33 __acl__ : ... (not yet done...)
34 the format is as follows:
35 { <obj_id> : {<prop>:<val>[, <prop>:<val>[...]]}[, ...]}
36 <obj_id> : this can be an object or a class (name or ref).
37 <prop> : this can be any of the above (except __acl__)
38 '''
40 # ACL:
41 # qualifires:
42 # <qualifier-name>
43 # rules:
44 # <qualifier-name> : <action>[, <action>[...]]
45 # <action> : <qualifier-name>[, <qualifier-name>[...]]
46 # actions:
47 # default:
48 # read
49 # write
50 # execute
51 # custon:
52 # <action-name> : <method-name>
54 # methods: (aspects)
55 # setacl(method, <acl>)
61 # TODO rename all attrs to __acl_<name>__ (???)
64 #-----------------------------------------------------------------------
66 import __builtin__
67 _hasattr = hasattr
68 _getattr = getattr
69 _setattr = setattr
70 ##_delattr = __builtin__.delattr
71 import sys
74 #-----------------------------------------------------------------------
75 # TODO process __acl__
76 #-----------------------------------------------------------isvisible---
77 def isvisible(obj, uid=None):
78 '''
79 '''
80 if _hasattr(obj, '__visible__') and not obj.__visible__:
81 return False
82 return True
85 #--------------------------------------------------------isaccessible---
86 def isaccessible(obj, uid=None):
87 '''
88 '''
89 if _hasattr(obj, '__public__') and not obj.__public__:
90 return False
91 return True
94 #-----------------------------------------------isglobalmethodallowed---
95 def isglobalmethodallowed(obj, meth, uid=None):
96 '''
97 '''
98 if isaccessible(obj) and \
99 (not _hasattr(obj, '__global_methods_disallowed__') or meth not in obj.__global_methods_disallowed__) and \
100 (not _hasattr(obj, '__global_methods_allowed__') or meth in obj.__global_methods_allowed__):
101 return True
102 return False
105 #-------------------------------------------------------------hasattr---
106 def hasattr(obj, name, uid=None):
109 if isaccessible(obj) and \
110 (not _hasattr(obj, '__private_attrs__') or name not in obj.__private_attrs__) and\
111 (not _hasattr(obj, '__public_attrs__') or name in obj.__public_attrs__):
112 if not _hasattr(obj, name):
113 return False
114 o_obj = _getattr(obj, name)
115 if isaccessible(o_obj):
116 return True
117 return False
120 #-------------------------------------------------------------getattr---
121 def getattr(obj, name, uid=None):
124 if isaccessible(obj) and \
125 (not _hasattr(obj, '__private_attrs__') or name not in obj.__private_attrs__) and\
126 (not _hasattr(obj, '__public_attrs__') or name in obj.__public_attrs__):
127 o_obj = _getattr(obj, name)
128 if isaccessible(o_obj):
129 return o_obj
130 raise AttributeError, '"%s" object has no attribute "%s".' % (obj, name)
133 #----------------------------------------------------------iswritable---
134 def iswritable(obj, name, uid=None):
137 if isaccessible(obj) and \
138 (not _hasattr(obj, '__private_attrs__') or name not in obj.__private_attrs__) and\
139 (not _hasattr(obj, '__public_attrs__') or name in obj.__public_attrs__) and\
140 (not _hasattr(obj, '__writable_attrs__') or name in obj.__writable_attrs__) and\
141 (not _hasattr(obj, '__readonly_attrs__') or name not in obj.__readonly_attrs__):
142 try:
143 o_obj = _getattr(obj, name)
144 if isaccessible(o_obj):
145 return True
146 except AttributeError:
147 return True
148 return False
151 #-------------------------------------------------------------setattr---
152 def setattr(obj, name, val, uid=None):
155 if isaccessible(obj) and \
156 (not _hasattr(obj, '__private_attrs__') or name not in obj.__private_attrs__) and\
157 (not _hasattr(obj, '__public_attrs__') or name in obj.__public_attrs__) and\
158 (not _hasattr(obj, '__writable_attrs__') or name in obj.__writable_attrs__) and\
159 (not _hasattr(obj, '__readonly_attrs__') or name not in obj.__readonly_attrs__):
160 return _setattr(obj, name, val)
161 raise AttributeError, 'can\'t write attribute "%s" of object "%s".' % (name, obj)
164 ###-------------------------------------------------------------delattr---
165 ##def delattr(obj, name, uid=None):
166 ## '''
167 ## '''
170 ###--------------------------------------------inheritclasssecuritylist---
171 ##def inheritclasssecuritylist(prop):
172 ## '''
173 ## this will return a tuple which is the union of the security option
174 ## given, set in the classes bases.
175 ## '''
176 ## cls = sys._getframe(1)
177 ## res = []
178 ## for c in cls.__bases__:
179 ## if _hasattr(c, prop):
180 ## res += _getattr(c, prop)
181 ## return tuple(dict([(i, None) for i in res]).keys())
184 #=======================================================================
185 # vim:set ts=4 sw=4 nowrap: