[r3951]: tags / 1.0.3.4 / turbogears / identity / conditions.py Maximize Restore History

Download this file

conditions.py    344 lines (277 with data), 10.5 kB

  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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import cherrypy
import socket
import struct
import threading
import types
import turbogears
from turbogears.identity.exceptions import *
from turbogears.identity import *
from turbogears.decorator import weak_signature_decorator
class Predicate(object):
'''
Generic base class for testing true or false for a condition.
'''
def eval_with_object( self, obj, errors=None ):
'''
Determine whether the predicate is True or False for the given object.
'''
raise NotImplementedError
def append_error_message( self, errors=None ):
if errors is None:
return
errors.append( self.error_message % self.__dict__ )
class CompoundPredicate(Predicate):
'''
A predicate composed of other predicates.
'''
def __init__(self, *predicates):
self.predicates= predicates
class All(CompoundPredicate):
'''
A compound predicate that evaluates to true only if all sub-predicates
evaluate to true for the given input.
'''
def eval_with_object( self, obj, errors=None ):
'''
Return true if all sub-predicates evaluate to true.
'''
for p in self.predicates:
if not p.eval_with_object( obj, errors ):
return False
return True
class Any(CompoundPredicate):
'''
A compound predicate that evaluates to true if any one of its sub-predicates
evaluates to true.
'''
error_message= "No predicates were able to grant access"
def eval_with_object( self, obj, errors=None ):
'''
Return true if any sub-predicate evaluates to true.
'''
for p in self.predicates:
if p.eval_with_object( obj, None ):
return True
self.append_error_message( errors )
return False
class IdentityPredicateHelper(object):
'''
A mix-in helper class for Identity Predicates.
'''
def __nonzero__(self):
return self.eval_with_object( current )
class in_group(Predicate, IdentityPredicateHelper):
'''
Predicate for requiring a group.
'''
error_message= "Not member of group: %(group_name)s"
def __init__(self, group_name):
self.group_name= group_name
def eval_with_object( self, identity, errors=None ):
if self.group_name in identity.groups:
return True
self.append_error_message( errors )
return False
class in_all_groups(All, IdentityPredicateHelper):
'''
Predicate for requiring membership in a number of groups.
'''
def __init__(self, *groups):
group_predicates= [in_group(g) for g in groups]
super(in_all_groups,self).__init__( *group_predicates )
class in_any_group(Any, IdentityPredicateHelper):
'''
Predicate for requiring membership in at least one group
'''
error_message= "Not member of any group: %(group_list)s"
def __init__(self, *groups):
self.group_list= ", ".join(groups)
group_predicates= [in_group(g) for g in groups]
super(in_any_group,self).__init__( *group_predicates )
class not_anonymous(Predicate, IdentityPredicateHelper):
'''
Predicate for checking whether current visitor is anonymous.
'''
error_message= "Anonymous access denied"
def eval_with_object( self, identity, errors=None ):
if current.anonymous:
self.append_error_message( errors )
return False
return True
class has_permission(Predicate, IdentityPredicateHelper):
'''
Predicate for checking whether the visitor has a particular permission.
'''
error_message= "Permission denied: %(permission_name)s"
def __init__(self, permission_name):
self.permission_name= permission_name
def eval_with_object(self, identity, errors=None):
'''
Determine whether the visitor has the specified permission.
'''
if self.permission_name in identity.permissions:
return True
self.append_error_message( errors )
return False
class has_all_permissions(All, IdentityPredicateHelper):
'''
Predicate for checking whether the visitor has all permissions.
'''
def __init__(self, *permissions):
permission_predicates= [has_permission(p) for p in permissions]
super(has_all_permissions,self).__init__( *permission_predicates )
class has_any_permission(Any, IdentityPredicateHelper):
'''
Predicate for checking whether the visitor has at least one permission.
'''
error_message= "No matching permissions: %(permission_list)s"
def __init__(self, *permissions):
self.permission_list= ", ".join( permissions )
permission_predicates= [has_permission(p) for p in permissions]
super(has_any_permission,self).__init__( *permission_predicates )
def _remoteHost():
try:
ips= cherrypy.request.headers.get(
"X-Forwarded-For", cherrypy.request.headers.get('Remote-Addr'))
return ips.split(",")[-1].strip()
except:
return ""
def _match_ip(cidr, ip):
if not '/' in cidr:
return cidr == ip
else:
try:
b,m = cidr.split('/')
shift = 32 - int(m)
a1 = struct.unpack('!L', socket.inet_aton(b))[0] >> shift
a2 = struct.unpack('!L', socket.inet_aton(ip))[0] >> shift
return a1 == a2
except:
return False
class from_host(Predicate, IdentityPredicateHelper):
'''
Predicate for checking whether the visitor's host is an allowed host.
Note: We never want to announce what the list of allowed hosts is, because
it is way too easy to spoof an IP address in a TCP/IP packet.
'''
error_message= "Access from this host is not permitted."
def __init__(self, host):
self.host= host
def eval_with_object( self, obj, errors=None ):
'''
Match the visitor's host against the criteria.
'''
ip = _remoteHost()
if _match_ip( self.host, ip ):
return True
self.append_error_message( errors )
return False
class from_any_host(Any, IdentityPredicateHelper):
'''
Predicate for checking whether the visitor's host is one of a number of
permitted hosts.
'''
error_message= "Access from this host is not permitted."
def __init__(self, hosts):
host_predicates= [from_host(h) for h in hosts]
super(from_any_host,self).__init__( *host_predicates )
def require(predicate, obj=None):
'''
Function decorator that checks whether the current user is a member of the
groups specified and has the permissions required.
'''
def entangle(fn):
def require(func, self, *args, **kwargs):
try:
errors= []
if predicate is None or \
predicate.eval_with_object(current, errors):
return fn(self, *args, **kwargs)
except IdentityException, e:
errors= [str(e)]
raise IdentityFailure(errors)
fn._require= predicate
return require
return weak_signature_decorator(entangle)
def _secureResourceDecorator( fn ):
'''
A decorator function used by the SecureResourceMeta metaclass.
'''
def _wrapper( self, *args, **kwargs ):
predicate= getattr( self.__class__, "require", None )
try:
errors= []
if predicate is None or \
predicate.eval_with_object( current, errors ):
return fn( self, *args, **kwargs )
except IdentityException, e:
errors= [str(e)]
raise IdentityFailure( errors )
try:
_wrapper.func_name= fn.func_name
except TypeError:
pass
if hasattr( fn, "exposed" ):
_wrapper.exposed= fn.exposed
return _wrapper
def _check_method( obj, fn, predicate ):
def _wrapper( *args, **kw ):
errors= []
if predicate.eval_with_object( current, errors ):
return fn( *args, **kw )
else:
raise IdentityFailure( errors )
_wrapper.exposed= True
return _wrapper
class SecureResource(object):
def __getattribute__( self, name ):
from turbogears import controllers
if name[:3]=="_cp" or name in ["require"]:
return object.__getattribute__(self,name)
try:
value= object.__getattribute__(self,name)
try:
predicate= object.__getattribute__(self,"require")
except AttributeError:
predicate= turbogears.config.get( "identity.require", None )
if predicate is None:
raise AttributeError("SecureResource requires a require "
"attribute either on the controller class "
"itself or in the config file")
errors= []
if (isinstance( value, types.MethodType ) and
hasattr( value, "exposed" )):
return _check_method( self, value, predicate )
if isinstance( value, controllers.Controller ):
return SecureObject( value, predicate )
# Some other property
return value
except IdentityException, e:
errors= [str(e)]
raise IdentityFailure( errors )
class SecureObject(object):
def __init__( self, obj, require, exclude=[]):
self._exclude = exclude
self._object= obj
self._require= require
def __getattribute__( self, name ):
from turbogears import controllers
if name[:3]=="_cp" or name in ["_object","_require","_exclude"]:
return object.__getattribute__(self,name)
try:
obj= object.__getattribute__( self, "_object" )
value= getattr( obj, name )
errors= []
predicate= object.__getattribute__(self,"_require")
if name in object.__getattribute__(self, "_exclude"):
return value
if (isinstance( value, types.MethodType ) and
hasattr( value, "exposed" )):
return _check_method( obj, value, predicate )
if isinstance( value, controllers.Controller ):
return SecureObject( value, predicate )
# Some other property
return value
except IdentityException, e:
errors= [str(e)]
raise IdentityFailure( errors )