<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import unicode_literals <EOL> from django . utils . six . moves import urllib_parse <EOL> from django . http import HttpResponseRedirect , HttpResponseForbidden <EOL> from django . conf import settings <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME <EOL> from django . contrib . auth . views import login , logout <EOL> from django . core . urlresolvers import reverse <EOL> from . views import login as cas_login , logout as cas_logout <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class CASMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> error = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> assert hasattr ( request , '<STR_LIT:user>' ) , error <EOL> def process_view ( self , request , view_func , view_args , view_kwargs ) : <EOL> """<STR_LIT>""" <EOL> if view_func == login : <EOL> return cas_login ( request , * view_args , ** view_kwargs ) <EOL> elif view_func == logout : <EOL> return cas_logout ( request , * view_args , ** view_kwargs ) <EOL> if view_func in ( cas_login , cas_logout ) : <EOL> return None <EOL> if settings . CAS_ADMIN_PREFIX : <EOL> if not request . path . startswith ( settings . CAS_ADMIN_PREFIX ) : <EOL> return None <EOL> elif not view_func . __module__ . startswith ( '<STR_LIT>' ) : <EOL> return None <EOL> if request . user . is_authenticated ( ) : <EOL> if request . user . is_staff : <EOL> return None <EOL> else : <EOL> error = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return HttpResponseForbidden ( error ) <EOL> params = urllib_parse . urlencode ( { REDIRECT_FIELD_NAME : request . get_full_path ( ) } ) <EOL> return HttpResponseRedirect ( reverse ( cas_login ) + '<STR_LIT:?>' + params ) </s>
