Created
July 3, 2012 16:41
-
-
Save jgeewax/3040900 to your computer and use it in GitHub Desktop.
login_required by default
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def login_not_required(handler_method): | |
| """Allows a user to *not* be logged in. | |
| The login_required attribute is inspected by BaseHandlerMeta and is used | |
| as the flag for whether to wrap a method with login_required or not. | |
| """ | |
| handler_method.login_required = False | |
| return handler_method | |
| def login_required(handler_method): | |
| """Requires that a user be logged in.""" | |
| required = getattr(handler_method, 'login_required', True) | |
| already_wrapped = getattr(handler_method, 'wrapped', False) | |
| # If the method doesn't require a login, or has already been wrapped, | |
| # just return the original. | |
| if not required or already_wrapped: | |
| return handler_method | |
| def check_login(self, *args, **kwargs): | |
| if not self.user_is_logged_in(): | |
| uri = self.uri_for('login', redirect=self.request.path) | |
| self.redirect(uri, abort=True) | |
| else: | |
| return handler_method(self, *args, **kwargs) | |
| # Let others know that this method is already wrapped to avoid wrapping | |
| # it more than once... | |
| check_login.wrapped = True | |
| return check_login |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import types | |
| import webapp2 | |
| from auth import login_required | |
| class BaseHandlerMeta(type): | |
| """Meta class for all request handlers. | |
| This automatically wraps all handler methods with the login_required | |
| decorator. If something should be exposed publicly, it should be wrapped | |
| with the login_not_required decorator. | |
| """ | |
| def __new__(cls, name, bases, local): | |
| if name != 'BaseHandler': | |
| for func_name, func in local.iteritems(): | |
| if isinstance(func, types.FunctionType): | |
| local[func_name] = login_required(func) | |
| return type.__new__(cls, name, bases, local) | |
| class BaseHandler(webapp2.RequestHandler): | |
| """Base class for all RequestHandlers.""" | |
| __metaclass__ = BaseHandlerMeta | |
| def user_is_logged_in(self): | |
| # Do some magic here to check if someone is logged in | |
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import base | |
| from auth import login_not_required | |
| class MyHandler(base.BaseHandler): | |
| @login_not_required | |
| def my_public_method(self): | |
| self.response.write('Hello world!') | |
| @login_not_required | |
| def my_public_method_with_a_problem(self): | |
| # Calling this should force a redirect to the login page! | |
| self.my_other_handler_that_is_protected() | |
| def my_other_handler_that_is_protected(self): | |
| self.response.write('Hello privately!') | |
| def protected_by_default(self): | |
| self.response.write('Once you log in, you can view this!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment