Forums

understanding user roles

Hi,

I am trying to setup two type of logins in my app. Customer and Vendor. I see that I can use user_roles in flask_security. But I am unable to understand how it works or implement it. I tried introducing flask-security and login and register works. But role table does not get populated....I am a bit confused on how do I make it work for customer and vendor...

Any help is much appreciated...

If role table is a database table, then it sounds like you need to populate it yourself.

Thanks conrad. I am sort of lost as Flask_security does populating in the background...

This is what I followed: https://pythonhosted.org/Flask-Security/quickstart.html

There are two tables here :

# Define models
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

I am not sure how to use these to populate and check for the appropriate user I want to show the page to...

We don't use Flask-Security ourselves (our internal code is mostly Django for user-facing stuff, and we only use Flask for internal microservices which don't do user management). So be warned that anything we suggest won't necessarily be from a position of any deep knowledge :-S

Having said that, I've found this example code which looks like it covers how the whole thing works in a much more in-depth manner than the main documentation. In particular, it appears that you need to create a SQLAlchemyUserDatastore object and then use its find_or_create_role and get_user, create_user, and add_role_to_user methods to manage which roles you have and which users are assigned to which roles.

Sorry I can't give more details, but given my lack of knowledge I suspect anything more I tried to infer from the documentation would be as likely to mislead you as help you...