Flask-Admin 的 inaccessible_callback() 无法正常工作
Flask-Admin's inaccessible_callback() not working properly
我想控制谁可以访问我的 Flask 应用程序的管理页面。
我一直在尝试覆盖 flask_admin.ModelView 的方法,'is_accessible' 和 'inaccessible_callback' 来处理这种情况。
这是 AdminView class 我正在创建:
class AdminView(ModelView):
def is_accessible(self):
return current_user.admin
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for('auth.login', next=request.url))
和模型:
class User(db.Model, UserMixin):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True)
email = db.Column(db.String(120), unique=True)
password_hash = db.Column(db.String(128))
admin = db.Column(db.Boolean, default=False)
def __init__(self, username=None, email=None, password=None):
self.username = username
self.email = email
self.password_hash = password_hash
self.admin = admin
和 AdminView 初始化:
def init_admin(admin):
from app.models import User
admin.add_view(AdminView(User, db.session))
在应用工厂调用:
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
if test_config is None:
# load the isntance config, if it exists, when not testing
app.config.from_object(Config)
else:
# load the test config passed in
app.config.from_object(test_config)
db.init_app(app)
migrate = Migrate(app, db)
login_manager.init_app(app)
mail.init_app(app)
bootstrap.init_app(app)
admin = Admin(app, name='app', template_mode='bootstrap3')
from app.auth import auth_bp
app.register_blueprint(auth_bp)
from app.tables import tables_bp
app.register_blueprint(tables_bp)
init_admin(admin)
try:
os.makedirs(app.instance_path)
except OSError:
pass
return app
当我使用 admin
属性设置为 True
的用户登录时,它会 returns 正确的管理页面,用户模型已准备好使用。当我使用具有 false admin
属性的用户登录时,它仍然显示管理页面,但没有附加用户模型。我宁愿将他们重定向到登录页面,并警告他们被禁止进入该页面。
多亏了 youtube 视频,我知道了如何让它工作!
查看更多 in-depth 解释!
问题是“/admin”页面由 flask_admin.AdminIndexView
加载
因此我必须创建自己的 AdminIndexView
子 class 并在初始化 Admin()
时将其设置为 index_view
参数
这是更新后的代码:
我在管理文件中添加了 MyIndexView class:
# ...
from flask_admin import AdminIndexView
# ...
class MyIndexView(AdminIndexView):
def is_accessible(self):
return current_user.admin
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for('auth.login', next=request.url))
然后我在我的应用程序工厂中设置index_view
参数
# ...
from app.admin import AdminView, MyIndexView
# ...
admin = Admin(app, name='app', template_mode='bootstrap3',
index_view=MyIndexView())
# ...
现在可以完美运行了!
我想控制谁可以访问我的 Flask 应用程序的管理页面。
我一直在尝试覆盖 flask_admin.ModelView 的方法,'is_accessible' 和 'inaccessible_callback' 来处理这种情况。
这是 AdminView class 我正在创建:
class AdminView(ModelView):
def is_accessible(self):
return current_user.admin
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for('auth.login', next=request.url))
和模型:
class User(db.Model, UserMixin):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True)
email = db.Column(db.String(120), unique=True)
password_hash = db.Column(db.String(128))
admin = db.Column(db.Boolean, default=False)
def __init__(self, username=None, email=None, password=None):
self.username = username
self.email = email
self.password_hash = password_hash
self.admin = admin
和 AdminView 初始化:
def init_admin(admin):
from app.models import User
admin.add_view(AdminView(User, db.session))
在应用工厂调用:
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
if test_config is None:
# load the isntance config, if it exists, when not testing
app.config.from_object(Config)
else:
# load the test config passed in
app.config.from_object(test_config)
db.init_app(app)
migrate = Migrate(app, db)
login_manager.init_app(app)
mail.init_app(app)
bootstrap.init_app(app)
admin = Admin(app, name='app', template_mode='bootstrap3')
from app.auth import auth_bp
app.register_blueprint(auth_bp)
from app.tables import tables_bp
app.register_blueprint(tables_bp)
init_admin(admin)
try:
os.makedirs(app.instance_path)
except OSError:
pass
return app
当我使用 admin
属性设置为 True
的用户登录时,它会 returns 正确的管理页面,用户模型已准备好使用。当我使用具有 false admin
属性的用户登录时,它仍然显示管理页面,但没有附加用户模型。我宁愿将他们重定向到登录页面,并警告他们被禁止进入该页面。
多亏了 youtube 视频,我知道了如何让它工作!
查看更多 in-depth 解释!
问题是“/admin”页面由 flask_admin.AdminIndexView
因此我必须创建自己的 AdminIndexView
子 class 并在初始化 Admin()
index_view
参数
这是更新后的代码: 我在管理文件中添加了 MyIndexView class:
# ...
from flask_admin import AdminIndexView
# ...
class MyIndexView(AdminIndexView):
def is_accessible(self):
return current_user.admin
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for('auth.login', next=request.url))
然后我在我的应用程序工厂中设置index_view
参数
# ...
from app.admin import AdminView, MyIndexView
# ...
admin = Admin(app, name='app', template_mode='bootstrap3',
index_view=MyIndexView())
# ...
现在可以完美运行了!