<s> from django . conf import settings <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import Permission <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from protector . helpers import get_all_user_permissions <EOL> ALL_PERMS_CACHE_FIELD = '<STR_LIT>' <EOL> def generate_perm_list ( perm_qset ) : <EOL> perm_qset = perm_qset . values_list ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return set ( "<STR_LIT>" . format ( ct , name ) for ct , name in perm_qset ) <EOL> class GenericPermissionBackend ( object ) : <EOL> def get_all_permissions ( self , user_obj , obj = None ) : <EOL> if not user_obj . is_active or user_obj . is_anonymous ( ) : <EOL> return set ( ) <EOL> disable_cache = getattr ( settings , '<STR_LIT>' , False ) <EOL> if user_obj . is_superuser : <EOL> perms = getattr ( self , '<STR_LIT>' , None ) <EOL> if perms is None : <EOL> perms = generate_perm_list ( Permission . objects . all ( ) ) <EOL> if not disable_cache : <EOL> setattr ( self , '<STR_LIT>' , perms ) <EOL> return perms <EOL> if obj is None : <EOL> cache_field_name = ALL_PERMS_CACHE_FIELD <EOL> else : <EOL> cache_field_name = '<STR_LIT>' . format ( <EOL> field = ALL_PERMS_CACHE_FIELD , pk = obj . pk , <EOL> ctype_id = ContentType . objects . get_for_model ( obj ) . id <EOL> ) <EOL> if not hasattr ( user_obj , cache_field_name ) or disable_cache : <EOL> perms = get_all_user_permissions ( user_obj , obj ) <EOL> setattr ( user_obj , cache_field_name , perms ) <EOL> return getattr ( user_obj , cache_field_name ) <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> 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> UserModel = get_user_model ( ) <EOL> try : <EOL> return UserModel . _default_manager . get ( pk = user_id ) <EOL> except UserModel . DoesNotExist : <EOL> return None </s>
