<s> '''<STR_LIT>''' <EOL> from abc import ABCMeta , abstractmethod <EOL> from conans . errors import ForbiddenException , InternalErrorException <EOL> from conans . model . ref import ConanFileReference <EOL> class Authorizer ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = ABCMeta <EOL> @ abstractmethod <EOL> def check_read_conan ( self , username , conan_reference ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplemented ( ) <EOL> @ abstractmethod <EOL> def check_write_conan ( self , username , conan_reference ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplemented ( ) <EOL> @ abstractmethod <EOL> def check_read_package ( self , username , package_reference ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplemented ( ) <EOL> @ abstractmethod <EOL> def check_write_package ( self , username , package_reference ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplemented ( ) <EOL> class Authenticator ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = ABCMeta <EOL> @ abstractmethod <EOL> def valid_user ( self , username , plain_password ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplemented ( ) <EOL> class BasicAuthenticator ( Authenticator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , users ) : <EOL> self . users = users <EOL> def valid_user ( self , username , plain_password ) : <EOL> """<STR_LIT>""" <EOL> return username in self . users and self . users [ username ] == plain_password <EOL> class BasicAuthorizer ( Authorizer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , read_permissions , write_permissions ) : <EOL> """<STR_LIT>""" <EOL> self . read_permissions = read_permissions <EOL> self . write_permissions = write_permissions <EOL> def check_read_conan ( self , username , conan_reference ) : <EOL> """<STR_LIT>""" <EOL> if conan_reference . user == username : <EOL> return <EOL> self . _check_any_rule_ok ( username , self . read_permissions , conan_reference ) <EOL> def check_write_conan ( self , username , conan_reference ) : <EOL> """<STR_LIT>""" <EOL> if conan_reference . user == username : <EOL> return True <EOL> self . _check_any_rule_ok ( username , self . write_permissions , conan_reference ) <EOL> def check_delete_conan ( self , username , conan_reference ) : <EOL> """<STR_LIT>""" <EOL> self . check_write_conan ( username , conan_reference ) <EOL> def check_read_package ( self , username , package_reference ) : <EOL> """<STR_LIT>""" <EOL> self . check_read_conan ( username , package_reference . conan ) <EOL> def check_write_package ( self , username , package_reference ) : <EOL> """<STR_LIT>""" <EOL> self . check_write_conan ( username , package_reference . conan ) <EOL> def check_delete_package ( self , username , package_reference ) : <EOL> """<STR_LIT>""" <EOL> self . check_write_package ( username , package_reference ) <EOL> def _check_any_rule_ok ( self , username , rules , * args , ** kwargs ) : <EOL> for rule in rules : <EOL> ret = self . _check_rule_ok ( username , rule , * args , ** kwargs ) <EOL> if ret : <EOL> return True <EOL> raise ForbiddenException ( "<STR_LIT>" ) <EOL> def _check_rule_ok ( self , username , rule , conan_reference ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> rule_ref = ConanFileReference . loads ( rule [ <NUM_LIT:0> ] ) <EOL> except Exception : <EOL> raise InternalErrorException ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> authorized_users = rule [ <NUM_LIT:1> ] . split ( "<STR_LIT:U+002C>" ) <EOL> if len ( authorized_users ) < <NUM_LIT:1> : <EOL> raise InternalErrorException ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if self . _check_ref_apply_for_rule ( rule_ref , conan_reference ) : <EOL> if authorized_users [ <NUM_LIT:0> ] == "<STR_LIT:*>" or username in authorized_users : <EOL> return True <EOL> else : <EOL> raise ForbiddenException ( "<STR_LIT>" ) <EOL> return False <EOL> def _check_ref_apply_for_rule ( self , rule_ref , conan_reference ) : <EOL> """<STR_LIT>""" <EOL> name , version , user , channel = rule_ref <EOL> return not ( ( name != "<STR_LIT:*>" and name != conan_reference . name ) or <EOL> ( version != "<STR_LIT:*>" and version != conan_reference . version ) or <EOL> ( user != "<STR_LIT:*>" and user != conan_reference . user ) or <EOL> ( channel != "<STR_LIT:*>" and channel != conan_reference . channel ) ) </s>
