<s> from __future__ import unicode_literals <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import Permission <EOL> class ModelBackend ( object ) : <EOL> """<STR_LIT>""" <EOL> def authenticate ( self , username = None , password = None , ** kwargs ) : <EOL> UserModel = get_user_model ( ) <EOL> if username is None : <EOL> username = kwargs . get ( UserModel . USERNAME_FIELD ) <EOL> try : <EOL> user = UserModel . _default_manager . get_by_natural_key ( username ) <EOL> if user . check_password ( password ) : <EOL> return user <EOL> except UserModel . DoesNotExist : <EOL> return None <EOL> def get_group_permissions ( self , user_obj , obj = None ) : <EOL> """<STR_LIT>""" <EOL> if user_obj . is_anonymous ( ) or obj is not None : <EOL> return set ( ) <EOL> if not hasattr ( user_obj , '<STR_LIT>' ) : <EOL> if user_obj . is_superuser : <EOL> perms = Permission . objects . all ( ) <EOL> else : <EOL> user_groups_field = get_user_model ( ) . _meta . get_field ( '<STR_LIT>' ) <EOL> user_groups_query = '<STR_LIT>' % user_groups_field . related_query_name ( ) <EOL> perms = Permission . objects . filter ( ** { user_groups_query : user_obj } ) <EOL> perms = perms . values_list ( '<STR_LIT>' , '<STR_LIT>' ) . order_by ( ) <EOL> user_obj . _group_perm_cache = set ( [ "<STR_LIT>" % ( ct , name ) for ct , name in perms ] ) <EOL> return user_obj . _group_perm_cache <EOL> def get_all_permissions ( self , user_obj , obj = None ) : <EOL> if user_obj . is_anonymous ( ) or obj is not None : <EOL> return set ( ) <EOL> if not hasattr ( user_obj , '<STR_LIT>' ) : <EOL> user_obj . _perm_cache = set ( [ "<STR_LIT>" % ( p . content_type . app_label , p . codename ) for p in user_obj . user_permissions . select_related ( ) ] ) <EOL> user_obj . _perm_cache . update ( self . get_group_permissions ( user_obj ) ) <EOL> return user_obj . _perm_cache <EOL> def has_perm ( self , user_obj , perm , obj = None ) : <EOL> if not user_obj . is_active : <EOL> return False <EOL> return perm in self . get_all_permissions ( user_obj , obj ) <EOL> def has_module_perms ( self , user_obj , app_label ) : <EOL> """<STR_LIT>""" <EOL> if not user_obj . is_active : <EOL> return False <EOL> for perm in self . get_all_permissions ( user_obj ) : <EOL> if perm [ : perm . index ( '<STR_LIT:.>' ) ] == app_label : <EOL> return True <EOL> return False <EOL> def get_user ( self , user_id ) : <EOL> try : <EOL> UserModel = get_user_model ( ) <EOL> return UserModel . _default_manager . get ( pk = user_id ) <EOL> except UserModel . DoesNotExist : <EOL> return None <EOL> class RemoteUserBackend ( ModelBackend ) : <EOL> """<STR_LIT>""" <EOL> create_unknown_user = True <EOL> def authenticate ( self , remote_user ) : <EOL> """<STR_LIT>""" <EOL> if not remote_user : <EOL> return <EOL> user = None <EOL> username = self . clean_username ( remote_user ) <EOL> UserModel = get_user_model ( ) <EOL> if self . create_unknown_user : <EOL> user , created = UserModel . objects . get_or_create ( ** { <EOL> UserModel . USERNAME_FIELD : username <EOL> } ) <EOL> if created : <EOL> user = self . configure_user ( user ) <EOL> else : <EOL> try : <EOL> user = UserModel . objects . get_by_natural_key ( username ) <EOL> except UserModel . DoesNotExist : <EOL> pass <EOL> return user <EOL> def clean_username ( self , username ) : <EOL> """<STR_LIT>""" <EOL> return username <EOL> def configure_user ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return user </s>
