22"""
33__docformat__ = 'restructuredtext'
44
5+ import logging
56import weakref
67
78from roundup import hyperdb , support
89
9- import logging
1010logger = logging .getLogger ('roundup.security' )
1111
1212
@@ -119,6 +119,8 @@ def check(db, userid, itemid):
119119 for a in args :
120120 if cls .filter ([itemid ], ** a ):
121121 return True
122+ return False
123+
122124 return check
123125
124126 def test (self , db , permission , classname , property , userid , itemid ):
@@ -241,16 +243,16 @@ def __repr__(self):
241243 pl = self .permission_list ()
242244 return '<Role 0x%x %r,%r>' % (id (self ), self .name , pl )
243245
244- def addPermission (self , * permissions ):
246+ def addPermission (self , * permissions ):
245247 for p in permissions :
246248 pn = p .name
247249 self ._permissions .setdefault (pn , {})
248250 cn = p .klass
249251 if p .klass not in self ._permissions [pn ]:
250- self ._permissions [pn ][cn ] = dict ((( False , []), ( True , [])))
252+ self ._permissions [pn ][cn ] = { False : [], True : []}
251253 self ._permissions [pn ][cn ][bool (p .check )].append (p )
252254
253- def filter_iter (self , permission , classname ):
255+ def filter_iter (self , permission , classname ):
254256 """ Loop over all permissions for the current role on the class
255257 with a check method (and props_only False).
256258 """
@@ -265,7 +267,7 @@ def filter_iter (self, permission, classname):
265267 continue
266268 yield p
267269
268- def hasPermission (self , db , perm , uid , classname , property , itemid , chk ):
270+ def hasPermission (self , db , perm , uid , classname , property , itemid , chk ):
269271 # if itemid is given a classname must, too, checked in caller
270272 if itemid and classname is None :
271273 raise ValueError ('classname must accompany itemid' )
@@ -287,32 +289,35 @@ def hasPermission (self, db, perm, uid, classname, property, itemid, chk):
287289 if p .test (db , perm , classname , property , uid , itemid ):
288290 return True
289291
290- def permission_list (self ):
292+ return False
293+
294+ def permission_list (self ):
291295 """ Used for reporting in admin tool """
292- l = []
296+ perm_list = []
293297 for p in self ._permissions :
294298 for c in self ._permissions [p ]:
295299 for cond in (False , True ):
296- l .extend (self ._permissions [p ][c ][cond ])
297- l .sort (key = lambda x : (x .klass or '' , x .name ))
298- return l
300+ perm_list .extend (self ._permissions [p ][c ][cond ])
301+ perm_list .sort (key = lambda x : (x .klass or '' , x .name ))
302+ return perm_list
299303
300- def searchable (self , classname , propname ):
301- for perm in 'View' , 'Search' :
304+ def searchable (self , classname , propname ):
305+ for perm_name in 'View' , 'Search' :
302306 # Only permissions without a check method
303- if perm not in self ._permissions :
307+ if perm_name not in self ._permissions :
304308 continue
305- p = self ._permissions [perm ]
306- if classname not in p and None not in p :
309+ perms = self ._permissions [perm_name ]
310+ if classname not in perms and None not in perms :
307311 continue
308- if None in p :
309- for p in p [None ][False ]:
312+ if None in perms :
313+ for p in perms [None ][False ]:
310314 if p .searchable (classname , propname ):
311315 return True
312- if classname in p :
313- for p in p [classname ][False ]:
316+ if classname in perms :
317+ for p in perms [classname ][False ]:
314318 if p .searchable (classname , propname ):
315319 return True
320+ return False
316321
317322
318323class Security :
@@ -334,9 +339,10 @@ def __init__(self, db):
334339 self .addRole (name = "Anonymous" , description = "An anonymous user" )
335340
336341 # default permissions - Admin may do anything
337- for p in 'create edit restore retire view' .split ():
338- p = self .addPermission (name = p .title (),
339- description = "User may %s everything" % p )
342+ for perm_name in 'create edit restore retire view' .split ():
343+ p = self .addPermission (name = perm_name .title (),
344+ description = "User may %s everything" %
345+ perm_name )
340346 self .addPermissionToRole ('Admin' , p )
341347
342348 # initialise the permissions and roles needed for the UIs
@@ -441,7 +447,7 @@ def is_filterable(self, permission, userid, classname):
441447 no permissions with a check method found, the performed
442448 checks later will find no matching records.
443449 """
444- for perm in self .filter_iter (permission , userid , classname ):
450+ for perm in self .filter_iter (permission , userid , classname ):
445451 if not perm .filter :
446452 return False
447453 return True
@@ -450,7 +456,6 @@ def roleHasSearchPermission(self, classname, property, *rolenames):
450456 """ For each of the given roles, check the permissions.
451457 Property can be a transitive property.
452458 """
453- perms = []
454459 # Note: break from inner loop means "found"
455460 # break from outer loop means "not found"
456461 cn = classname
@@ -478,13 +483,13 @@ def roleHasSearchPermission(self, classname, property, *rolenames):
478483 else :
479484 # for Link and Multilink require search permission on label-
480485 # and order-properties and on ID
481- if isinstance (prop , Multilink ) or isinstance ( prop , Link ):
486+ if isinstance (prop , ( Link , Multilink ) ):
482487 try :
483488 cls = self .db .getclass (prop .classname )
484489 except KeyError :
485490 return 0
486491 props = dict .fromkeys (('id' , cls .labelprop (), cls .orderprop ()))
487- for p in props . keys () :
492+ for p in props :
488493 for rn in rolenames :
489494 if self .role [rn ].searchable (prop .classname , p ):
490495 break
@@ -568,8 +573,8 @@ def addPermissionToRole(self, rolename, permission, classname=None,
568573 def filterFilterspec (self , userid , classname , filterspec ):
569574 """ Return a filterspec that has all non-allowed properties removed.
570575 """
571- return dict ([( k , v ) for k , v in filterspec .items ()
572- if self .hasSearchPermission (userid , classname , k )])
576+ return { k : v for k , v in filterspec .items ()
577+ if self .hasSearchPermission (userid , classname , k )}
573578
574579 def filterSortspec (self , userid , classname , sort ):
575580 """ Return a sort- or group-list that has all non-allowed properties
0 commit comments