<s> import getpass <EOL> import json <EOL> import pkg_resources <EOL> from trac . admin import IAdminPanelProvider <EOL> from trac . core import * <EOL> from trac . config import Option , BoolOption <EOL> from trac . util . translation import _ <EOL> from trac . web . chrome import ITemplateProvider <EOL> from trac . web . chrome import add_notice <EOL> from trac . web . chrome import add_warning <EOL> from trac_gitolite import utils <EOL> class GitolitePermissionManager ( Component ) : <EOL> implements ( IAdminPanelProvider , ITemplateProvider ) <EOL> gitolite_admin_reponame = Option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> default = "<STR_LIT>" ) <EOL> gitolite_admin_ssh_path = Option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> default = "<STR_LIT>" ) <EOL> gitolite_admin_real_reponame = Option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> default = "<STR_LIT>" ) <EOL> gitolite_admin_system_user = Option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> default = "<STR_LIT>" ) <EOL> def get_users ( self ) : <EOL> node = utils . get_repo_node ( self . env , self . gitolite_admin_reponame , <EOL> "<STR_LIT>" ) <EOL> assert node . isdir , "<STR_LIT>" % node <EOL> for child in node . get_entries ( ) : <EOL> name = child . get_name ( ) <EOL> assert name . endswith ( "<STR_LIT>" ) , "<STR_LIT>" % name <EOL> name = name [ : - <NUM_LIT:4> ] <EOL> yield name <EOL> def read_config ( self ) : <EOL> node = utils . get_repo_node ( self . env , self . gitolite_admin_reponame , <EOL> "<STR_LIT>" ) <EOL> fp = node . get_content ( ) <EOL> return utils . read_config ( fp ) <EOL> def get_admin_panels ( self , req ) : <EOL> if '<STR_LIT>' in req . perm : <EOL> yield ( '<STR_LIT>' , _ ( '<STR_LIT>' ) , '<STR_LIT>' , <EOL> _ ( '<STR_LIT>' ) ) <EOL> def render_admin_panel ( self , req , category , page , path_info ) : <EOL> req . perm . require ( '<STR_LIT>' ) <EOL> if req . method == '<STR_LIT:POST>' : <EOL> perms = { } <EOL> for setting in req . args : <EOL> try : <EOL> setting = json . loads ( setting ) <EOL> except ValueError : <EOL> continue <EOL> if not isinstance ( setting , dict ) or '<STR_LIT>' not in setting or '<STR_LIT:user>' not in setting or '<STR_LIT>' not in setting : <EOL> continue <EOL> repo = setting [ '<STR_LIT>' ] ; perm = setting [ '<STR_LIT>' ] ; user = setting [ '<STR_LIT:user>' ] <EOL> if repo not in perms : <EOL> perms [ repo ] = { } <EOL> if perm not in perms [ repo ] : <EOL> perms [ repo ] [ perm ] = [ ] <EOL> if user not in perms [ repo ] [ perm ] : <EOL> perms [ repo ] [ perm ] . append ( user ) <EOL> gitolite_admin_perms = perms . get ( self . gitolite_admin_real_reponame , { } ) <EOL> if ( self . gitolite_admin_system_user not in gitolite_admin_perms . get ( '<STR_LIT:R>' , [ ] ) or <EOL> self . gitolite_admin_system_user not in gitolite_admin_perms . get ( '<STR_LIT>' , [ ] ) ) : <EOL> add_warning ( req , _ ( '<STR_LIT>' % self . gitolite_admin_system_user ) ) <EOL> req . redirect ( req . href . admin ( category , page ) ) <EOL> utils . save_file ( self . gitolite_admin_ssh_path , '<STR_LIT>' , <EOL> utils . to_string ( perms ) , <EOL> _ ( '<STR_LIT>' ) ) <EOL> add_notice ( req , _ ( '<STR_LIT>' ) ) <EOL> req . redirect ( req . href . admin ( category , page ) ) <EOL> perms = self . read_config ( ) <EOL> users_listed_in_perms = set ( ) <EOL> flattened_perms = set ( ) <EOL> for p in perms . values ( ) : <EOL> for perm in p : <EOL> flattened_perms . add ( perm ) <EOL> users_listed_in_perms . update ( p [ perm ] ) <EOL> flattened_perms = list ( flattened_perms ) <EOL> def sort_perms ( perms ) : <EOL> tail = [ ] <EOL> if '<STR_LIT:+>' in perms : <EOL> perms . remove ( "<STR_LIT:+>" ) <EOL> tail . append ( "<STR_LIT:+>" ) <EOL> perms = sorted ( perms ) <EOL> perms . extend ( tail ) <EOL> return perms <EOL> flattened_perms = sort_perms ( flattened_perms ) <EOL> users = sorted ( list ( set ( list ( self . get_users ( ) ) + list ( users_listed_in_perms ) ) ) ) <EOL> data = { '<STR_LIT>' : perms , '<STR_LIT>' : flattened_perms , <EOL> '<STR_LIT>' : users , <EOL> '<STR_LIT>' : sort_perms } <EOL> return '<STR_LIT>' , data <EOL> def get_htdocs_dirs ( self ) : <EOL> return [ ] <EOL> def get_templates_dirs ( self ) : <EOL> return [ pkg_resources . resource_filename ( '<STR_LIT>' , '<STR_LIT>' ) ] </s>
