Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created July 6, 2011 09:47
Show Gist options
  • Save peterbe/1066929 to your computer and use it in GitHub Desktop.
Save peterbe/1066929 to your computer and use it in GitHub Desktop.
authenticated_plus decorator
import functools
import urllib
import urlparse
# taken from tornado.web.authenticated
def authenticated_plus(extra_check):
"""Decorate methods with this to require that the user be logged in."""
def wrap(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
if not (self.current_user and extra_check(self.current_user)):
if self.request.method in ("GET", "HEAD"):
url = self.get_login_url()
if "?" not in url:
if urlparse.urlsplit(url).scheme:
# if login url is absolute, make next absolute too
next_url = self.request.full_url()
else:
next_url = self.request.uri
url += "?" + urllib.urlencode(dict(next=next_url))
self.redirect(url)
return
raise HTTPError(403)
return method(self, *args, **kwargs)
return wrapper
return wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment