Flask Security- check what Roles a User has
Asked Answered
R

1

5

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?

Rhodesia answered 16/9, 2016 at 3:35 Comment(0)
W
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)
Washbowl answered 16/9, 2016 at 6:25 Comment(1)
has_role() updated link to the methodCrowl

© 2022 - 2024 — McMap. All rights reserved.