Edgewall Software

Ticket #157: mod_authz_perm_p1.patch

File mod_authz_perm_p1.patch, 6.7 KB (added by Utopiste, 8 years ago)

First patch for mod_authz support

  • trac/core.py

    ndex: trac/core.py
     
    2929 
    3030import Href 
    3131import perm 
     32import authzperm 
    3233import auth 
    3334import Environment 
    3435import Session 
     
    153154    module.req = req 
    154155    module._name = mode 
    155156    module.db = db 
    156     module.perm = perm.PermissionCache(module.db, req.authname) 
     157    module.perm = perm.PermissionCache(module.db, req.authname)     
    157158    module.perm.add_to_hdf(req.hdf) 
    158  
     159     
    159160    # Only open the subversion repository for the modules that really 
    160161    # need it. This saves us some precious time. 
    161162    if need_svn: 
     
    166167        module.fs_ptr = fs_ptr 
    167168        sync.sync(module.db, rep, fs_ptr, pool) 
    168169        module.pool = pool 
     170        module.authzperm = authzperm.AuthzPermission(env,req.authname) 
    169171    return module 
    170172 
    171173def open_environment(): 
  • trac/db_default.py

     
    414414  ('trac', 'repository_dir', '/var/svn/myrep'), 
    415415  ('trac', 'templates_dir', '/usr/lib/trac/templates'), 
    416416  ('trac', 'database', 'sqlite:db/trac.db'), 
     417  ('trac', 'authz_file', ''), 
    417418  ('logging', 'log_type', 'none'), 
    418419  ('logging', 'log_file', 'trac.log'), 
    419420  ('logging', 'log_level', 'DEBUG'), 
  • trac/perm.py

     
    5151MILESTONE_MODIFY = 'MILESTONE_MODIFY' 
    5252MILESTONE_DELETE = 'MILESTONE_DELETE' 
    5353 
     54AUTHZSVN_VIEW = 'AUTHZSVN_VIEW' 
     55AUTHZSVN_MODIFY = 'AUTHZSVN_MODIFY' 
     56 
    5457TRAC_ADMIN = 'TRAC_ADMIN' 
    5558TICKET_ADMIN = 'TICKET_ADMIN' 
    5659REPORT_ADMIN = 'REPORT_ADMIN' 
    5760WIKI_ADMIN = 'WIKI_ADMIN' 
    5861ROADMAP_ADMIN = 'MILESTONE_ADMIN' 
     62AUTHZSVN_ADMIN = 'AUTHZSVN_ADMIN' 
    5963 
    6064meta_permission = { 
    6165    TRAC_ADMIN: [TICKET_ADMIN, REPORT_ADMIN, WIKI_ADMIN, ROADMAP_ADMIN, 
     
    6670                   REPORT_DELETE], 
    6771    WIKI_ADMIN: [WIKI_VIEW, WIKI_CREATE, WIKI_MODIFY, WIKI_DELETE], 
    6872    ROADMAP_ADMIN: [ROADMAP_VIEW, MILESTONE_VIEW, MILESTONE_CREATE, 
    69                     MILESTONE_MODIFY, MILESTONE_DELETE] 
     73                    MILESTONE_MODIFY, MILESTONE_DELETE], 
     74    AUTHZSVN_ADMIN: [AUTHZSVN_VIEW, AUTHZSVN_MODIFY] 
    7075} 
    7176 
    7277 
  • trac/File.py

     
    3131import svn 
    3232 
    3333import perm 
     34import authzperm 
    3435import util 
     36 
    3537from Module import Module 
    3638from Wiki import wiki_to_html 
    3739 
     
    100102        self.filename = self.args.get('filename', None) 
    101103        if self.filename: 
    102104            self.filename = os.path.basename(self.filename) 
    103  
     105             
    104106        if not self.attachment_type or not self.attachment_id: 
    105107            raise util.TracError('Unknown request') 
    106108 
     
    136138            self.mime_type = self.env.mimeview.get_mimetype(self.filename) \ 
    137139                             or 'application/octet-stream' 
    138140            return 
    139  
     141         
    140142        if self.args.has_key('description') and \ 
    141143               self.args.has_key('author') and \ 
    142144               self.args.has_key('attachment') and \ 
     
    217219                                      self.env.href.browser(path)) 
    218220 
    219221    def display(self): 
     222        self.authzperm.assert_permission(self.path) 
    220223        FileCommon.display(self) 
     224         
    221225 
    222226    def render(self): 
    223227        FileCommon.render(self) 
    224          
     228         
    225229        rev = self.args.get('rev', None) 
    226230        self.path = self.args.get('path', '/') 
    227231        if not rev: 
     
    236240                rev = svn.fs.youngest_rev(self.fs_ptr, self.pool) 
    237241 
    238242        self.generate_path_links(rev, rev_specified) 
    239          
     243         
    240244        try: 
    241245            root = svn.fs.revision_root(self.fs_ptr, rev, self.pool) 
    242246        except svn.core.SubversionException: 
  • trac/authzperm.py

     
     1# -*- coding: iso8859-1 -*- 
     2# 
     3# Copyright (C) 2004 Edgewall Software 
     4# Copyright (C) 2004 Francois Harvey <fharvey@securiweb.net> 
     5# 
     6# Trac is free software; you can redistribute it and/or 
     7# modify it under the terms of the GNU General Public License as 
     8# published by the Free Software Foundation; either version 2 of the 
     9# License, or (at your option) any later version. 
     10# 
     11# Trac is distributed in the hope that it will be useful, 
     12# but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
     14# General Public License for more details. 
     15# 
     16# You should have received a copy of the GNU General Public License 
     17# along with this program; if not, write to the Free Software 
     18# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
     19# 
     20# Author: Francois Harvey <fharvey@securiweb.net> 
     21 
     22from exceptions import StandardError 
     23import ConfigParser 
     24import string 
     25import os 
     26 
     27class AuthzPermissionError (StandardError): 
     28    """Insufficient permissions to view this file""" 
     29    def __str__ (self): 
     30        return 'authz read privileges required to view this file' 
     31     
     32 
     33class AuthzPermission: 
     34    auth_name = '' 
     35    module_name = '' 
     36    conf_authz = None 
     37    authz_file = '' 
     38     
     39    def __init__(self,env,authname): 
     40        if authname == 'anonymous': 
     41            self.auth_name = '*' 
     42        else: 
     43            self.auth_name = authname 
     44        self.module_name = env.get_config('project', 'name') 
     45        self.autz_file = env.get_config('trac','authz_file')     
     46        if env.get_config('trac','authz_file'): 
     47            self.conf_authz = ConfigParser.ConfigParser() 
     48            self.conf_authz.read( self.autz_file ) 
     49             
     50 
     51    def has_permission(self, path): 
     52        acc = 'r' 
     53        path_comb = '' 
     54          
     55        if (path != None) and (self.conf_authz != None) : 
     56            if self.conf_authz.has_section(self.module_name + ':/') and self.conf_authz.has_option(self.module_name + ':/', self.auth_name): 
     57                acc = self.conf_authz.get(self.module_name + ':/',self.auth_name) 
     58            for path_ele in path.split('/'): 
     59                if path_ele != '': 
     60                    path_comb = path_comb + '/' + path_ele 
     61                    section_name = self.module_name + ':' + path_comb 
     62                    if self.conf_authz.has_section(section_name) and self.conf_authz.has_option(section_name,self.auth_name): 
     63                        acc =  self.conf_authz.get(section_name ,self.auth_name)                         
     64        return acc 
     65 
     66    def assert_permission (self, path): 
     67        if self.has_permission(path) == '': 
     68            raise AuthzPermissionError() 
     69 
     70