1
0
Fork 0

fix: allow custom user getter for new_request_set_user() hook

This commit is contained in:
Lance Edgar 2024-08-05 15:06:55 -05:00
parent fc339ba81b
commit 0e0460b831
2 changed files with 39 additions and 13 deletions

View file

@ -35,6 +35,7 @@ However some custom apps may need to supplement or replace the event
hooks contained here, depending on the circumstance.
"""
import functools
import json
import logging
@ -108,10 +109,29 @@ def new_request(event):
request.set_property(use_oruga, reify=True)
def new_request_set_user(event, db_session=None):
def default_user_getter(request, db_session=None):
"""
This is the default function used to retrieve user object from
database. Result of this is then assigned to :attr:`request.user`
as part of the :func:`new_request_set_user()` hook.
"""
uuid = request.authenticated_userid
if uuid:
config = request.wutta_config
app = config.get_app()
model = app.model
session = db_session or Session()
return session.get(model.User, uuid)
def new_request_set_user(
event,
user_getter=default_user_getter,
db_session=None,
):
"""
Event hook called when processing a new :term:`request`, for sake
of setting the ``request.user`` property.
of setting the :attr:`request.user` and similar properties.
The hook is auto-registered if this module is "included" by
Pyramid config object. Or you can explicitly register it::
@ -137,32 +157,38 @@ def new_request_set_user(event, db_session=None):
Flag indicating whether user is currently elevated to root
privileges. This is only possible if :attr:`request.is_admin`
is also true.
You may wish to "supplement" this hook by registering your own
custom hook and then invoking this one as needed. You can then
pass certain params to override only parts of the logic:
:param user_getter: Optional getter function to retrieve the user
from database, instead of :func:`default_user_getter()`.
:param db_session: Optional :term:`db session` to use,
instead of :class:`wuttaweb.db.Session`.
"""
request = event.request
config = request.registry.settings['wutta_config']
app = config.get_app()
def user(request):
uuid = request.authenticated_userid
if uuid:
session = db_session or Session()
model = app.model
return session.get(model.User, uuid)
request.set_property(user, reify=True)
# request.user
if db_session:
user_getter = functools.partial(user_getter, db_session=db_session)
request.set_property(user_getter, name='user', reify=True)
# request.is_admin
def is_admin(request):
auth = app.get_auth_handler()
return auth.user_is_admin(request.user)
request.set_property(is_admin, reify=True)
# request.is_root
def is_root(request):
if request.is_admin:
if request.session.get('is_root', False):
return True
return False
request.set_property(is_root, reify=True)

View file

@ -103,7 +103,7 @@ class TestNewRequestSetUser(TestCase):
'wutta.db.default.url': 'sqlite://',
})
self.request = testing.DummyRequest()
self.request = testing.DummyRequest(wutta_config=self.config)
self.pyramid_config = testing.setUp(request=self.request, settings={
'wutta_config': self.config,
})