Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken LoginManager #432

Open
lukasjuhrich opened this issue Jan 21, 2022 · 0 comments
Open

Fix broken LoginManager #432

lukasjuhrich opened this issue Jan 21, 2022 · 0 comments

Comments

@lukasjuhrich
Copy link
Collaborator

lukasjuhrich commented Jan 21, 2022

The custom LoginManager implementation has two problems:

  • It somehow causes the user information to be loaded on the /static endpoint. This is a performance problem, because it means an API call for every time a logged in user visits one of these endpoints.
  • It does not cause any user loading to be disabled in the /documents endpoint because the disable_user_loading endpoint has never been set.

class SipaLoginManager(LoginManager):
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self.localize_callback = gettext
self.login_message = "Bitte melde Dich an, um die Seite zu sehen."
self.ignored_endpoints = set()
self._wrapped_user_callback = None
def ignore_endpoint(self, endpoint_name):
self.ignored_endpoints.add(endpoint_name)
def disable_user_loading(self, bp=None):
"""The decorator version of :py:meth:`ignore_endpoint`
Use as `@login_manager.disable_user_loading()` or
`@login_manager.disable_user_loading(bp)`.
"""
if bp is None:
def endpoint_name(f):
return f.__name__
else:
if not isinstance(bp, Blueprint):
raise TypeError("Must call `disable_user_loading`"
" with instance of `Blueprint`")
def endpoint_name(f):
return "{}.{}".format(bp.name, f.__name__)
def decorate(f):
self.ignore_endpoint(endpoint_name(f))
return f
return decorate
@property
def user_callback(self):
return self._wrapped_user_callback
@user_callback.setter
def user_callback(self, f):
@wraps(f)
def wrapped_user_callback(user_id):
if request.endpoint in self.ignored_endpoints:
return self.anonymous_user()
return f(user_id)
self._wrapped_user_callback = wrapped_user_callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant