Flask Security- check what Roles a User has
<p>I was looking at the flask-security api and i dont see any function that returns the list of roles a specific User has. Is there anyway to return a list of Roles a user has?</p>

<p>If you look at how the <a href="https://github.com/mattupstate/flask-security/blob/develop/flask_security/core.py#L318" rel="nofollow"><code>has_role(...)</code> method</a> has been defined, it simply iterates through <code>self.roles</code>. So the <code>roles</code> attribute in the user is a list of <code>Role</code> objects.</p>

<p>You need to define your <code>User</code> and <code>Role</code> models as in the example <a href="https://pythonhosted.org/Flask-Security/quickstart.html" rel="nofollow">here</a>, so that the <code>User</code> model has a many-to-many relationship to <code>Role</code> model set in the <code>User.roles</code> attribute.</p>

<pre class="lang-py prettyprint-override"><code># This one is a list of Role objects
roles = user.roles
# This one is a list of Role names
role_names = (role.name for role in user.roles)
</code></pre>

python flask flask-sqlalchemy flask-security