"Fossies" - the Fresh Open Source Software Archive

Member "viewvc-1.2.1/lib/vcauth/forbiddenre/__init__.py" (26 Mar 2020, 2059 Bytes) of package /linux/misc/viewvc-1.2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "__init__.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.1.28_vs_1.2.1.

    1 # -*-python-*-
    2 #
    3 # Copyright (C) 2008-2020 The ViewCVS Group. All Rights Reserved.
    4 #
    5 # By using this file, you agree to the terms and conditions set forth in
    6 # the LICENSE.html file which can be found at the top level of the ViewVC
    7 # distribution or at http://viewvc.org/license-1.html.
    8 #
    9 # For more information, visit http://viewvc.org/
   10 #
   11 # -----------------------------------------------------------------------
   12 import vcauth
   13 import vclib
   14 import fnmatch
   15 import re
   16 
   17 
   18 def _split_regexp(restr):
   19   """Return a 2-tuple consisting of a compiled regular expression
   20   object and a boolean flag indicating if that object should be
   21   interpreted inversely."""
   22   if restr[0] == '!':
   23     return re.compile(restr[1:]), 1
   24   return re.compile(restr), 0
   25 
   26 
   27 class ViewVCAuthorizer(vcauth.GenericViewVCAuthorizer):
   28   """A simple regular-expression-based authorizer."""
   29   def __init__(self, root_lookup_func, username, params={}):
   30     forbidden = params.get('forbiddenre', '')
   31     self.forbidden = map(lambda x: _split_regexp(x.strip()),
   32                          filter(None, forbidden.split(',')))
   33                          
   34   def _check_root_path_access(self, root_path):
   35     default = 1
   36     for forbidden, negated in self.forbidden:
   37       if negated:
   38         default = 0
   39         if forbidden.search(root_path):
   40           return 1
   41       elif forbidden.search(root_path):
   42         return 0
   43     return default
   44       
   45   def check_root_access(self, rootname):
   46     return self._check_root_path_access(rootname)
   47   
   48   def check_universal_access(self, rootname):
   49     # If there aren't any forbidden regexps, we can grant universal
   50     # read access.  Otherwise, we make no claim.
   51     if not self.forbidden:
   52       return 1
   53     return None
   54     
   55   def check_path_access(self, rootname, path_parts, pathtype, rev=None):
   56     root_path = rootname
   57     if path_parts:
   58       root_path = root_path + '/' + '/'.join(path_parts)
   59       if pathtype == vclib.DIR:
   60         root_path = root_path + '/'
   61     else:
   62       root_path = root_path + '/'
   63     return self._check_root_path_access(root_path)
   64