Changeset 3625

Show
Ignore:
Timestamp:
05/06/08 12:06:02 (4 months ago)
Author:
robert_martin
Message:

Reads groups from the groups file, and puts them in the authz file
automatically.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • finegrainedpageauthzeditorplugin/0.11/page_authz_policy_editor/pape_admin.py

    r3608 r3625  
    1919import os 
    2020import pkg_resources 
     21from configobj import ConfigObj 
     22#import ConfigParser 
     23from StringIO import StringIO 
    2124 
    2225from genshi import HTML 
     
    8487            try: 
    8588                for user_line in password_file: 
    86                     group = user_line.strip() 
    8789                    # Ignore blank lines and lines starting with # 
    8890                    if user_line and not user_line.startswith('#'): 
    8991                        user_name = user_line.split(':', 1)[0] 
    90                         users_list.append(user_name
     92                        users_list.append(user_name.strip()
    9193            finally: 
    9294                password_file.close() 
     
    9496        return users 
    9597 
     98    # Get the groups and their members so they can easily be included in the 
     99    # groups section of the authz file.  Need it as a dictionary of arrays so it be easily 
     100    # iterated. 
     101    def _get_groups_and_members(self): 
     102        group_file_name = self.config.get('account-manager', 'group_file') 
     103        if os.path.exists(group_file_name): 
     104            group_file = file(group_file_name) 
     105            try: 
     106                groups_dict = dict() 
     107                for group_line in group_file: 
     108                    group = group_line.strip() 
     109                    # Ignore blank lines and lines starting with # 
     110                    if group_line and not group_line.startswith('#'): 
     111                        group_name = group_line.split(':', 1)[0] 
     112                        group_members = group_line.split(':', 2)[1].split(' ') 
     113                        groups_dict[group_name] = [ x for x in [member.strip() for member in group_members] if x ] 
     114            finally: 
     115                group_file.close() 
     116        if len(groups_dict): 
     117            return groups_dict 
     118        else: 
     119            return None 
     120 
    96121    def render_admin_panel(self, req, cat, page, path_info): 
    97122        req.perm.require('TRAC_ADMIN') 
    98123        authz_policy_file_name = self.config.get('authz_policy', 'authz_file') 
     124        group_details = self._get_groups_and_members() 
    99125        # Handle the return data 
    100126        if req.method == 'POST': 
     
    105131                authz_policy_file.close() 
    106132 
    107         contents = open(authz_policy_file_name).readlines() 
     133        authz_policy_file = ConfigObj(authz_policy_file_name) 
     134 
     135        # If there isn't a group file, don't destroy the existing entries 
     136        if (group_details): 
     137            authz_policy_file['groups'] = group_details 
     138 
     139        # This is purely to fill in the text area with the contents. 
     140        contents = StringIO() 
     141        authz_policy_file.write(contents) 
     142 
     143 
     144        #contents = open(authz_policy_file_name).readlines() 
    108145        data = { 
    109146            'file_name' : authz_policy_file_name, 
    110             'contents': contents
     147            'contents': contents.getvalue()
    111148            'users' : self._get_users(), 
    112149            'groups' : self._get_groups()