Closed schrecksim closed 1 year ago
Hey,
I can't test the code right now, but I think that the problem is with the way the base filter is written. What you want is this:
from flask_appbuilder.models.sqla.filters import FilterEqualFunction
base_filters = [['ben_id', FilterEqualFunction, get_user_ben]]
The two changes are using FilterEqualFunction instead of Filter Equal (obviously) and only giving the function without calling it right away (might be easier to overlook :)). See also here in the documentation: https://flask-appbuilder.readthedocs.io/en/latest/advanced.html#base-filtering And i recommend scanning this page to get an idea of what filters are available to you: https://github.com/dpgaspar/Flask-AppBuilder/blob/master/flask_appbuilder/models/sqla/filters.py#L17
To add some context: FAB tries to evaluate your filter when starting the app, at which point there is no g.user available. That's why the error points at Flask not being able to start the app instead of the error occuring when trying to access the view. The new code let's FAB evaluate the filter when accessing the view, so now you have g.user and everything should work out nicely.
Let me know if it's still not working!
Works now, thanks for your support. I wasn't aware about the difference between FilterEqual & FilterEqualFunction. Thanks for explaining.
Environment
Flask-Appbuilder version: 4.3.6
pip freeze output:
Describe the expected results
The g.user should return the username to filter on it. If I place a string like UserGlobalProvider.username == 'someuser' it works totally fine. The g.user should return the username, if possible, as a string to use it like shown below.
I know that probably I could do the solution shorter like: def get_user_ben(): return g.user.ben_id
but it needs to work like that as well for getting user information in other context of the application as well.
Describe the actual results
It seems that the g.user isn't set, although besides what is written above there is nothing else done to the security manager than shown above with a sec_view to implement the new attribute ben_id. The sec.py file is also done like written in the documentation.