Flask-security: How to check if a user has a role in a template

Created on 15 May 2016  路  1Comment  路  Source: mattupstate/flask-security

I have been going over the source code for a couple of days now. Is there a way to check if a user has a particular role in a template. My use case is simple. I only want to make a menu item available to certain users. it would be useful to do this:

{% if current_user.has_role('admin') %}
    <li><a href="{% url_for('dashboard') %}">Dashboard</a></li>
{% endif %}

Most helpful comment

It was as simple as I thought it should be: I just had to add a has_role method to the User model. This allowed what I wanted above to work. This could be added to the user mixin:

    def has_role(self, role):
        return role in self.roles

>All comments

It was as simple as I thought it should be: I just had to add a has_role method to the User model. This allowed what I wanted above to work. This could be added to the user mixin:

    def has_role(self, role):
        return role in self.roles
Was this page helpful?
0 / 5 - 0 ratings

Related issues

DerekDomino picture DerekDomino  路  5Comments

williamcheng-web picture williamcheng-web  路  4Comments

ochronus picture ochronus  路  4Comments

Ben095 picture Ben095  路  6Comments

Galstat picture Galstat  路  3Comments