DEV Community

abbazs
abbazs

Posted on

FLASK-USER current_user.has_roles('Admin')

I'm working in a flask project and I wanted the URLs to views available if only the user has access to those views. Apparently, flask-user has a method called has_roles that can be used for this purpose but not available in the documentation https://flask-user.readthedocs.io/

Here is how I've enabled the selective availability of URLs in jinja2 template:

{% if current_user.has_roles('Admin') %}
<li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
        data-toggle="dropdown" aria-haspopup="true">Users</a>
    <div class="dropdown-menu bg-dark" aria-labelledby="navbarDropdown">
        <a class="dropdown-item" href="#">Dashboard</a>
        <a class="dropdown-item" href="{{ url_for('auth.adduser') }}">Add</a>
    </div>
</li>
{% endif %}
Enter fullscreen mode Exit fullscreen mode

Billboard image

Imagine monitoring that's actually built for developers

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay