app/soc/views/helper/access.py
changeset 1226 a671f0d63562
parent 1223 aca77e2cc8f7
child 1227 38afecddfbed
equal deleted inserted replaced
1225:1d5c58e24fd3 1226:a671f0d63562
   205     base = params.get('rights') if params else None
   205     base = params.get('rights') if params else None
   206     self.rights = base.rights if base else {}
   206     self.rights = base.rights if base else {}
   207     self.id = None
   207     self.id = None
   208     self.user = None
   208     self.user = None
   209 
   209 
   210   def __setitem__(self, key, value):
   210   def normalizeChecker(self, checker):
   211     """Sets a value only if no old value exists.
   211     """Normalizes the checker to a pre-defined format.
   212     """
       
   213 
       
   214     oldvalue = self.rights.get(key)
       
   215     self.rights[key] = oldvalue if oldvalue else value
       
   216 
       
   217   def __getitem__(self, key):
       
   218     """Retrieves the right checkers and massages then into a default format.
       
   219 
   212 
   220     The result is guaranteed to be a list of 2-tuples, the first element is a
   213     The result is guaranteed to be a list of 2-tuples, the first element is a
   221     checker (iff there is an checker with the specified name), the second
   214     checker (iff there is an checker with the specified name), the second
   222     element is a list of arguments that should be passed to the checker when
   215     element is a list of arguments that should be passed to the checker when
   223     calling it in addition to the standard django_args.
   216     calling it in addition to the standard django_args.
   224     """
   217     """
   225 
   218 
   226     result = []
   219     # Be nice an repack so that it is always a list with tuples
   227 
   220     if isinstance(checker, tuple):
   228     for i in self.rights.get(key, []):
   221       name, arg = checker
   229       # Be nice an repack so that it is always a list with tuples
   222       return (name, (arg if isinstance(arg, list) else [arg]))
   230       if isinstance(i, tuple):
   223     else:
   231         name, arg = i
   224       return (checker, [])
   232         tmp = (name, (arg if isinstance(arg, list) else [arg]))
   225 
   233         result.append(tmp)
   226   def __setitem__(self, key, value):
   234       else:
   227     """Sets a value only if no old value exists.
   235         tmp = (i, [])
   228     """
   236         result.append(tmp)
   229 
   237 
   230     oldvalue = self.rights.get(key)
   238     return result
   231     self.rights[key] = oldvalue if oldvalue else value
       
   232 
       
   233   def __getitem__(self, key):
       
   234     """Retrieves and normalizes the right checkers.
       
   235     """
       
   236 
       
   237     return [self.normalizeChecker(i) for i in self.rights.get(key, [])]
   239 
   238 
   240   def key(self, checker_name):
   239   def key(self, checker_name):
   241     """Returns the key for the specified checker for the current user.
   240     """Returns the key for the specified checker for the current user.
   242     """
   241     """
   243 
   242 
   358 
   357 
   359     # try to see if they belong to any of the roles, if not, raise an
   358     # try to see if they belong to any of the roles, if not, raise an
   360     # access violation for the specified action, prefix and status.
   359     # access violation for the specified action, prefix and status.
   361     for role in roles:
   360     for role in roles:
   362       try:
   361       try:
   363         checker_name = self.MEMBERSHIP[role]
   362         checker_name, args = self.normalizeChecker(self.MEMBERSHIP[role])
   364         self.doCheck(checker_name, django_args, [])
   363         self.doCheck(checker_name, django_args, args)
   365 
       
   366         # the check passed, we can stop now
   364         # the check passed, we can stop now
   367         break
   365         break
   368       except out_of_band.Error:
   366       except out_of_band.Error:
   369         continue
   367         continue
   370     else:
   368     else: