【问题标题】:How to use Flask-admin with flask-principal(security) to handle users by roles?如何使用 Flask-admin 和 flask-principal(security) 按角色处理用户?
【发布时间】:2014-08-08 00:39:59
【问题描述】:

我已经有了带有烧瓶登录的自定义管理索引,并且我有一些 sqlalchemy 模型视图。我只是使用is_accessible() 来处理要显示的内容:登录表单或模型视图。

class BaseAdminController(ModelView):
    column_exclude_list = ('created_on','modified_on')
    def is_accessible(self):
        return login.current_user.is_authenticated()

例如,我有两种用户类型 - 版主和用户。如何只为用户显示某些视图而为版主显示所有其他视图?

【问题讨论】:

    标签: python flask flask-extensions


    【解决方案1】:

    要使某些视图仅供版主访问,您可以执行以下操作

    def is_accessible(self):
        if not login.current_user.is_authenticated():
            return False
        if not is_moderator(login.current_user):
            return False
        return True
    

    然后在 is_moderator 中检查用户是否属于“主持人”类型。您没有提到如何在数据模型中存储用户类型,所以我无法为您提供 is_moderator 函数。


    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 2013-06-23
      • 1970-01-01
      • 2017-05-28
      • 2016-02-09
      • 1970-01-01
      • 2013-09-13
      • 2017-01-24
      • 1970-01-01
      相关资源
      最近更新 更多