<s> from django . contrib . auth import SESSION_KEY , BACKEND_SESSION_KEY <EOL> try : <EOL> from django . contrib . auth import HASH_SESSION_KEY <EOL> except ImportError : <EOL> HASH_SESSION_KEY = '<STR_LIT>' <EOL> from django_su import get_user_model <EOL> def su_login_callback ( user ) : <EOL> if user . is_active and user . is_staff : <EOL> return True <EOL> return user . has_perm ( '<STR_LIT>' ) <EOL> def _get_user_session_key ( request ) : <EOL> return get_user_model ( ) . _meta . pk . to_python ( request . session [ SESSION_KEY ] ) <EOL> def custom_login ( request , user ) : <EOL> session_auth_hash = '<STR_LIT>' <EOL> if user is None : <EOL> user = request . user <EOL> if hasattr ( user , '<STR_LIT>' ) : <EOL> session_auth_hash = user . get_session_auth_hash ( ) <EOL> if SESSION_KEY in request . session : <EOL> if _get_user_session_key ( request ) != user . pk or ( <EOL> session_auth_hash and <EOL> request . session . get ( HASH_SESSION_KEY ) != session_auth_hash ) : <EOL> request . session . flush ( ) <EOL> else : <EOL> request . session . cycle_key ( ) <EOL> request . session [ SESSION_KEY ] = user . _meta . pk . value_to_string ( user ) <EOL> request . session [ BACKEND_SESSION_KEY ] = user . backend <EOL> request . session [ HASH_SESSION_KEY ] = session_auth_hash <EOL> if hasattr ( request , '<STR_LIT:user>' ) : <EOL> request . user = user <EOL> try : <EOL> from django . middleware . csrf import rotate_token <EOL> rotate_token ( request ) <EOL> except ImportError : <EOL> pass </s>
