Open scratchmex opened 2 years ago
You can use OOP for that.
from flask_admin.contrib.sqla import ModelView
from flask import redirect, url_for, request
from flask_login import current_user
class SecureModelView(ModelView):
def is_accessible(self):
# Example: Only allow access if the user is authenticated and has a specific role
return current_user.is_authenticated and current_user.has_role('admin')
def inaccessible_callback(self, name, **kwargs):
# Redirect to login page if the user doesn't have access
return redirect(url_for('login', next=request.url))
Then you only need your views to inherit from SecureModelView.
class View1(SecureModelView):
pass
class View2(SecureModelView):
pass
currently, I have to do this to add authentication to my admin panel
I would be nice if you expose the blueprint before you create it inside
init_app
by callingview.create_blueprint
on each view.What I am saying is to change the signature of
init_app
toIn the first case we can register middleware with
blueprint.before_request
and in the second case,init_app
do it manually. I prefer the first to have all the flexibility. The middleware could beI am happy to do a PR