Edgewall Software

Ticket #157: mod_authz_perm_p1.2.patch

File mod_authz_perm_p1.2.patch, 9.3 KB (added by utopiste, 8 years ago)

work on trunk, initial changeset support

  • core.py

     
    3030 
    3131import Href 
    3232import perm 
     33import authzperm 
    3334import auth 
    3435import Environment 
    3536import Session 
     
    154155    module.req = req 
    155156    module._name = mode 
    156157    module.db = db 
    157     module.perm = perm.PermissionCache(module.db, req.authname) 
     158    module.perm = perm.PermissionCache(module.db, req.authname)     
    158159    module.perm.add_to_hdf(req.hdf) 
    159  
     160     
    160161    # Only open the subversion repository for the modules that really 
    161162    # need it. This saves us some precious time. 
    162163    if need_svn: 
     
    167168        module.fs_ptr = fs_ptr 
    168169        sync.sync(module.db, rep, fs_ptr, pool) 
    169170        module.pool = pool 
     171        module.authzperm = authzperm.AuthzPermission(env,req.authname) 
    170172    return module 
    171173 
    172174def open_environment(): 
  • db_default.py

     
    415415  ('trac', 'repository_dir', '/var/svn/myrep'), 
    416416  ('trac', 'templates_dir', '/usr/lib/trac/templates'), 
    417417  ('trac', 'database', 'sqlite:db/trac.db'), 
     418  ('trac', 'authz_file', ''), 
    418419  ('trac', 'default_charset', 'iso-8859-15'), 
    419420  ('logging', 'log_type', 'none'), 
    420421  ('logging', 'log_file', 'trac.log'), 
  • 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 
  • File.py

     
    3131import svn 
    3232 
    3333import perm 
     34import authzperm 
    3435import util 
    3536import Module 
    3637from WikiFormatter import wiki_to_html 
     
    109110        self.filename = self.args.get('filename', None) 
    110111        if self.filename: 
    111112            self.filename = os.path.basename(self.filename) 
    112  
     113             
    113114        if not self.attachment_type or not self.attachment_id: 
    114115            raise util.TracError('Unknown request') 
    115116 
     
    145146            self.mime_type = self.env.mimeview.get_mimetype(self.filename) \ 
    146147                             or 'application/octet-stream' 
    147148            return 
    148  
     149         
    149150        if self.args.has_key('description') and \ 
    150151               self.args.has_key('author') and \ 
    151152               self.args.has_key('attachment') and \ 
     
    226227                                      self.env.href.browser(path)) 
    227228 
    228229    def display(self): 
     230        self.authzperm.assert_permission(self.path) 
    229231        FileCommon.display(self) 
     232         
    230233 
    231234    def render(self): 
    232235        FileCommon.render(self) 
    233          
     236         
    234237        rev = self.args.get('rev', None) 
    235238        self.path = self.args.get('path', '/') 
    236239        if not rev: 
     
    245248                rev = svn.fs.youngest_rev(self.fs_ptr, self.pool) 
    246249 
    247250        self.generate_path_links(rev, rev_specified) 
    248          
     251         
    249252        try: 
    250253            root = svn.fs.revision_root(self.fs_ptr, rev, self.pool) 
    251254        except svn.core.SubversionException: 
  • 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#    def expand_meta_permission(self, action): 
     51#        self.perm_cache[action] = 1 
     52#        if meta_permission.has_key(action): 
     53#            for perm in meta_permission[action]: 
     54#                self.expand_meta_permission(perm) 
     55 
     56    def has_permission(self, path): 
     57        acc = 'r' 
     58        path_comb = '' 
     59          
     60        if (path != None) and (self.conf_authz != None) : 
     61            if self.conf_authz.has_section(self.module_name + ':/') and self.conf_authz.has_option(self.module_name + ':/', self.auth_name): 
     62                acc = self.conf_authz.get(self.module_name + ':/',self.auth_name) 
     63            for path_ele in path.split('/'): 
     64                if path_ele != '': 
     65                    path_comb = path_comb + '/' + path_ele 
     66                    section_name = self.module_name + ':' + path_comb 
     67                    if self.conf_authz.has_section(section_name) and self.conf_authz.has_option(section_name,self.auth_name): 
     68                        acc =  self.conf_authz.get(section_name ,self.auth_name)                         
     69        return acc 
     70 
     71    def assert_permission (self, path): 
     72        if self.has_permission(path) == '': 
     73            raise AuthzPermissionError() 
     74 
     75     
  • Changeset.py

     
    2424import util 
    2525import Diff 
    2626import perm 
     27import authzperm 
    2728import Module 
    2829from WikiFormatter import wiki_to_html 
    2930 
     
    4344        self.args = args 
    4445        self.env = env 
    4546        self.fileno = 0 
    46  
     47        self.authz = authzperm.AuthzPermission(env,req.authname) 
     48         
    4749    def print_diff (self, old_path, new_path, pool): 
    4850        if not old_path or not new_path: 
    4951            return 
     
    5254        new_rev = svn.fs.node_created_rev(self.new_root, new_path, pool) 
    5355 
    5456        options = Diff.get_options(self.env, self.req, self.args, 1) 
    55         differ = svn.fs.FileDiff(self.old_root, old_path, self.new_root, new_path, 
     57        if (self.authz.has_permission(new_path)): 
     58            differ = svn.fs.FileDiff(self.old_root, old_path, self.new_root, new_path, 
    5659                             pool, options) 
    57         differ.get_files() 
    58         pobj = differ.get_pipe() 
    59         prefix = 'changeset.diff.files.%d' % (self.fileno) 
    60         tabwidth = int(self.env.get_config('diff', 'tab_width', '8')) 
    61         builder = Diff.HDFBuilder(self.req.hdf, prefix, tabwidth) 
    62         self.fileno += 1 
    63         builder.writeline('header %s %s | %s %s redaeh' % (old_path, old_rev, 
     60            differ.get_files() 
     61            pobj = differ.get_pipe() 
     62            prefix = 'changeset.diff.files.%d' % (self.fileno) 
     63            tabwidth = int(self.env.get_config('diff', 'tab_width', '8')) 
     64            builder = Diff.HDFBuilder(self.req.hdf, prefix, tabwidth) 
     65            self.fileno += 1 
     66            builder.writeline('header %s %s | %s %s redaeh' % (old_path, old_rev, 
    6467                                                           new_path, new_rev)) 
    65         while 1: 
    66             line = pobj.readline() 
    67             if not line: 
    68                 break 
    69             builder.writeline(util.escape(util.to_utf8(line))) 
    70         builder.close() 
     68            while 1: 
     69                line = pobj.readline() 
     70                if not line: 
     71                    break 
     72                builder.writeline(util.escape(util.to_utf8(line))) 
     73                builder.close() 
    7174 
    7275    def add_file(self, path, parent_baton, copyfrom_path, 
    7376                 copyfrom_revision, file_pool): 
     
    112115    perm = None 
    113116    fs_ptr = None 
    114117    pool = None 
    115  
     118     
    116119    def get_changeset_info (self, rev): 
    117120        cursor = self.db.cursor () 
    118121        cursor.execute ('SELECT time, author, message FROM revision ' +