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?
Asked
Active
Viewed 4,527 times
1 Answers
8
If you look at how the has_role(...)
method has been defined, it simply iterates through self.roles
. So the roles
attribute in the user is a list of Role
objects.
You need to define your User
and Role
models as in the example here, so that the User
model has a many-to-many relationship to Role
model set in the User.roles
attribute.
# 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)

lari
- 752
- 4
- 19
-
[has_role()](https://github.com/mattupstate/flask-security/blob/674b18103fa8734aca71bbd084ea01e3709817ef/flask_security/core.py#L399) updated link to the method – Akshay Shegaonkar Jan 26 '21 at 19:22