【问题标题】:Restricting access of some roles to some specific pages限制某些角色对某些特定页面的访问
【发布时间】:2022-11-26 14:03:28
【问题描述】:
@bp.route("/products/wishlist", methods=["GET"])
@login_required
@roles_required(
    "ADMIN",
    "CUSTOMER_STORE_MANAGER"
)
def product_wishlist():
    return product_wishlist_page()

我有这样的角色限制,其中每个页面都有一些角色要求,我需要做的是限制某些角色,以便他们只能访问某些特定页面,例如当 CUSTOMER_STORE_MANAGER登录网页,他们应该只能查看product_wishlist

我考虑过为每个角色定义页面,并检查他们是否试图访问他们有权访问的页面。 但是我想知道在 Flask 中是否有更方便的方法来做到这一点?

【问题讨论】:

    标签: python flask


    【解决方案1】:

    你为什么不根据角色在模板中过滤

    {% if current_user.role == CUSTOMER_STORE_MANAGER %}
        {# show what this role can view or do #}
    {% elif current_user.role == "Admin"%}
       ...
    {% endif %}
    

    这很简单并且在后端呈现,因此它与用更少的代码做多个模板是一样的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-25
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      相关资源
      最近更新 更多