Skip to content Skip to sidebar Skip to footer

Flask Security- Check What Roles A User Has

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?

Solution 1:

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)

Post a Comment for "Flask Security- Check What Roles A User Has"