Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 425
David Gonzalez Avatar asked Sep 16 '16 03:09

David Gonzalez


People also ask

What signals does flask-security send?

In addition to those signals, Flask-Security sends the following signals. Sent when a user successfully authenticates. In addition to the app (which is the sender), it is passed user, and authn_via arguments.

What does the security class do in flask?

The Security class initializes the Flask-Security extension. app – The application. datastore – An instance of a user datastore. Initializes the Flask-Security extension for the specified application and datastore implentation.

How to check for user roles in Java?

The first way to check for user roles in Java is to use the @PreAuthorize annotation provided by Spring Security. This annotation can be applied to a class or method, and it accepts a single string value that represents a SpEL expression. Before we can use this annotation, we must first enable global method security.

What is @authorization in flask?

Authorization is the process of specifying and enforcing access rights of users to resources. Flask-User offers role-based authorization through the use of the @roles_required decorator. If a view function is decorated with the @roles_required decorator, the user: must be associated with the specified role names.


Video Answer


1 Answers

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)
like image 124
lari Avatar answered Oct 18 '22 02:10

lari