<s> """<STR_LIT>""" <EOL> import logging <EOL> from collections import defaultdict <EOL> from pyramid . security import Everyone , Authenticated <EOL> from pyramid . settings import aslist , asbool <EOL> from . base import IAccessBackend <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class ConfigAccessBackend ( IAccessBackend ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request = None , settings = None , admins = None , <EOL> group_map = None , user_groups = None , ** kwargs ) : <EOL> super ( ConfigAccessBackend , self ) . __init__ ( request , ** kwargs ) <EOL> self . _settings = settings <EOL> self . admins = admins <EOL> self . group_map = group_map <EOL> self . user_groups = user_groups <EOL> @ classmethod <EOL> def configure ( cls , settings ) : <EOL> kwargs = super ( ConfigAccessBackend , cls ) . configure ( settings ) <EOL> if asbool ( settings . get ( '<STR_LIT>' , False ) ) : <EOL> LOG . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> kwargs [ '<STR_LIT>' ] = [ Everyone ] <EOL> kwargs [ '<STR_LIT>' ] = [ Authenticated ] <EOL> kwargs [ '<STR_LIT>' ] = settings <EOL> kwargs [ '<STR_LIT>' ] = aslist ( settings . get ( '<STR_LIT>' , [ ] ) ) <EOL> user_groups = defaultdict ( list ) <EOL> group_map = { } <EOL> for key , value in settings . iteritems ( ) : <EOL> if not key . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> group_name = key [ len ( '<STR_LIT>' ) : ] <EOL> members = aslist ( value ) <EOL> group_map [ group_name ] = members <EOL> for member in members : <EOL> user_groups [ member ] . append ( group_name ) <EOL> kwargs [ '<STR_LIT>' ] = group_map <EOL> kwargs [ '<STR_LIT>' ] = user_groups <EOL> return kwargs <EOL> def _get_password_hash ( self , username ) : <EOL> key = "<STR_LIT>" % username <EOL> return self . _settings . get ( key ) <EOL> def groups ( self , username = None ) : <EOL> if username is None : <EOL> return self . group_map . keys ( ) <EOL> else : <EOL> return self . user_groups [ username ] <EOL> def group_members ( self , group ) : <EOL> return self . group_map . get ( group , [ ] ) <EOL> def is_admin ( self , username ) : <EOL> return username in self . admins <EOL> @ staticmethod <EOL> def _perms_from_short ( value ) : <EOL> """<STR_LIT>""" <EOL> value = value or '<STR_LIT>' <EOL> perms = [ ] <EOL> if '<STR_LIT:r>' in value : <EOL> perms . append ( '<STR_LIT>' ) <EOL> if '<STR_LIT:w>' in value : <EOL> perms . append ( '<STR_LIT>' ) <EOL> return perms <EOL> def group_permissions ( self , package , group = None ) : <EOL> if group is not None : <EOL> key = '<STR_LIT>' % ( package , group ) <EOL> return self . _perms_from_short ( self . _settings . get ( key ) ) <EOL> perms = { } <EOL> group_prefix = '<STR_LIT>' % package <EOL> for key , value in self . _settings . iteritems ( ) : <EOL> if not key . startswith ( group_prefix ) : <EOL> continue <EOL> group = key [ len ( group_prefix ) : ] <EOL> perms [ group ] = self . _perms_from_short ( value ) <EOL> return perms <EOL> def user_permissions ( self , package , username = None ) : <EOL> if username is not None : <EOL> key = '<STR_LIT>' % ( package , username ) <EOL> return self . _perms_from_short ( self . _settings . get ( key ) ) <EOL> perms = { } <EOL> user_prefix = '<STR_LIT>' % package <EOL> for key , value in self . _settings . iteritems ( ) : <EOL> if not key . startswith ( user_prefix ) : <EOL> continue <EOL> user = key [ len ( user_prefix ) : ] <EOL> perms [ user ] = self . _perms_from_short ( value ) <EOL> return perms <EOL> def user_data ( self , username = None ) : <EOL> if username is not None : <EOL> return { <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT>' : self . is_admin ( username ) , <EOL> '<STR_LIT>' : self . groups ( username ) , <EOL> } <EOL> users = [ ] <EOL> user_prefix = '<STR_LIT>' <EOL> for key in self . _settings : <EOL> if not key . startswith ( user_prefix ) : <EOL> continue <EOL> username = key [ len ( user_prefix ) : ] <EOL> users . append ( { <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT>' : self . is_admin ( username ) <EOL> } ) <EOL> return users <EOL> def user_package_permissions ( self , username ) : <EOL> perms = [ ] <EOL> for key , value in self . _settings . iteritems ( ) : <EOL> pieces = key . split ( '<STR_LIT:.>' ) <EOL> if ( len ( pieces ) != <NUM_LIT:4> or pieces [ <NUM_LIT:0> ] != '<STR_LIT>' or <EOL> pieces [ <NUM_LIT:2> ] != '<STR_LIT:user>' ) : <EOL> continue <EOL> package , user = pieces [ <NUM_LIT:1> ] , pieces [ <NUM_LIT:3> ] <EOL> if user == username : <EOL> perms . append ( { <EOL> '<STR_LIT>' : package , <EOL> '<STR_LIT>' : self . _perms_from_short ( value ) , <EOL> } ) <EOL> return perms <EOL> def group_package_permissions ( self , group ) : <EOL> perms = [ ] <EOL> for key , value in self . _settings . iteritems ( ) : <EOL> pieces = key . split ( '<STR_LIT:.>' ) <EOL> if ( len ( pieces ) != <NUM_LIT:4> or pieces [ <NUM_LIT:0> ] != '<STR_LIT>' or <EOL> pieces [ <NUM_LIT:2> ] != '<STR_LIT>' ) : <EOL> continue <EOL> package , groupname = pieces [ <NUM_LIT:1> ] , pieces [ <NUM_LIT:3> ] <EOL> if group == groupname : <EOL> perms . append ( { <EOL> '<STR_LIT>' : package , <EOL> '<STR_LIT>' : self . _perms_from_short ( value ) , <EOL> } ) <EOL> return perms <EOL> def load ( self , data ) : <EOL> lines = [ ] <EOL> admins = [ ] <EOL> for user in data [ '<STR_LIT>' ] : <EOL> lines . append ( '<STR_LIT>' . format ( ** user ) ) <EOL> if user . get ( '<STR_LIT>' ) : <EOL> admins . append ( user [ '<STR_LIT:username>' ] ) <EOL> if admins : <EOL> lines . append ( '<STR_LIT>' ) <EOL> for admin in admins : <EOL> lines . append ( '<STR_LIT>' . format ( admin ) ) <EOL> for group , members in data [ '<STR_LIT>' ] . iteritems ( ) : <EOL> lines . append ( '<STR_LIT>' . format ( group ) ) <EOL> for member in members : <EOL> lines . append ( '<STR_LIT>' . format ( member ) ) <EOL> def encode_permissions ( perms ) : <EOL> """<STR_LIT>""" <EOL> ret = '<STR_LIT>' <EOL> if '<STR_LIT>' in perms : <EOL> ret += '<STR_LIT:r>' <EOL> if '<STR_LIT>' in perms : <EOL> ret += '<STR_LIT:w>' <EOL> return ret <EOL> for package , groups in data [ '<STR_LIT>' ] [ '<STR_LIT>' ] . iteritems ( ) : <EOL> for group , permissions in groups . iteritems ( ) : <EOL> lines . append ( '<STR_LIT>' <EOL> . format ( package , group , <EOL> encode_permissions ( permissions ) ) ) <EOL> for package , users in data [ '<STR_LIT>' ] [ '<STR_LIT>' ] . iteritems ( ) : <EOL> for user , permissions in users . iteritems ( ) : <EOL> lines . append ( '<STR_LIT>' <EOL> . format ( package , user , <EOL> encode_permissions ( permissions ) ) ) <EOL> return '<STR_LIT:\n>' . join ( lines ) </s>
