X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-web.git/blobdiff_plain/a6bc598397e68ca5b7c0571341da3aa81617a327..70b6a0214b7554af3465adedcde599362f5bdd4c:/code/view.py diff --git a/code/view.py b/code/view.py index 4135e24..1765ddf 100644 --- a/code/view.py +++ b/code/view.py @@ -9,6 +9,7 @@ from StringIO import StringIO from invirt.config import structs as config from webcommon import State + class MakoHandler(cherrypy.dispatch.LateParamPageHandler): """Callable which processes a dictionary, returning the rendered body.""" @@ -57,6 +58,7 @@ class MakoLoader(object): cherrypy.tools.mako = cherrypy.Tool('on_start_resource', MakoLoader()) + def revertStandardError(): """Move stderr to stdout, and return the contents of the old stderr.""" errio = sys.stderr @@ -66,6 +68,7 @@ def revertStandardError(): errio.seek(0) return errio.read() + def catchStderr(): old_handler = cherrypy.request.handler def wrapper(*args, **kwargs): @@ -81,6 +84,7 @@ def catchStderr(): cherrypy.tools.catch_stderr = cherrypy.Tool('before_handler', catchStderr) + class JSONEncoder(simplejson.JSONEncoder): def default(self, obj): if isinstance(obj, datetime.datetime): @@ -90,6 +94,7 @@ class JSONEncoder(simplejson.JSONEncoder): else: return simplejson.JSONEncoder.default(self, obj) + def jsonify_tool_callback(*args, **kwargs): if not cherrypy.request.cached: response = cherrypy.response @@ -98,6 +103,7 @@ def jsonify_tool_callback(*args, **kwargs): cherrypy.tools.jsonify = cherrypy.Tool('before_finalize', jsonify_tool_callback, priority=30) + def require_login(): """If the user isn't logged in, raise 403 with an error.""" if cherrypy.request.login is False: @@ -106,6 +112,7 @@ def require_login(): cherrypy.tools.require_login = cherrypy.Tool('on_start_resource', require_login, priority=150) + def require_POST(): """If the request isn't a POST request, raise 405 Method Not Allowed""" if cherrypy.request.method != "POST": @@ -114,27 +121,25 @@ def require_POST(): cherrypy.tools.require_POST = cherrypy.Tool('on_start_resource', require_POST, priority=150) + def remote_user_login(): - """Get the current user based on the SSL or GSSAPI environment -variables and store it in the request object's login variable. This -conforms to the CherryPy API: -http://www.cherrypy.org/wiki/RequestObject#login - -If the user is logged in successfully, cherrypy.request.login is set -to the username. If the user failed to log in, cherrypy.request.login -is set to False. If the user did not attempt authentication, -cherrypy.request.login is set to None.""" + """Get remote user from SSL or GSSAPI, and store in request object. + +Get the current user based on environment variables set by SSL or +GSSAPI, and store it in the attribute cherrpy.request.login. + +Per the CherryPy API (http://www.cherrypy.org/wiki/RequestObject#login), +the attribute is set to the username on successful login, to False on +failed login, and is left at None if the user attempted no authentication. +""" environ = cherrypy.request.wsgi_environ user = environ.get('REMOTE_USER') if user is None: return - else: - cherrypy.request.login = None # clear what cherrypy put there - if environ.get('AUTH_TYPE') == 'Negotiate': # Convert the krb5 principal into a krb4 username if not user.endswith('@%s' % config.kerberos.realm): - cherrypy.request.login = False # failed to login + cherrypy.request.login = False # failed to log in else: cherrypy.request.login = user.split('@')[0].replace('/', '.') else: @@ -142,6 +147,7 @@ cherrypy.request.login is set to None.""" cherrypy.tools.remote_user_login = cherrypy.Tool('on_start_resource', remote_user_login, priority=50) + def invirtwebstate_init(): """Initialize the cherrypy.request.state object from Invirt""" if not hasattr(cherrypy.request, "state"): @@ -149,5 +155,6 @@ def invirtwebstate_init(): cherrypy.tools.invirtwebstate = cherrypy.Tool('on_start_resource', invirtwebstate_init, priority=100) + class View(object): _cp_config = {'tools.mako.directories': [os.path.join(os.path.dirname(__file__),'templates')]}