How to customize registration provided by Flask-se

2019-05-28 14:17发布

问题:

What am i supposed to do, if need to set up Role of user to, for example FooUser? Where i need to do this? In my own /create_account view or in context processor?

@app.route('/create_account')
def create_account():
    user_datastore.find_or_create_role('FooUser')
    user_datastore.add_role_to_user(user,role)
    return render_template('register_user.html')

回答1:

You may customize views following these instructions: http://flask-security.readthedocs.org/en/latest/customizing.html

And you may hook into the user registration process using signals: http://flask-security.readthedocs.org/en/latest/api.html#signals



回答2:

IMPORTANT! Second way is pretty simple. @user_registered.connect_via(app) def user_registered_sighandler(sender, **extra): sender.logger.debug("logger-user_registered_sighandler:", extra) user = extra.get('user') role = user_datastore.find_or_create_role('farmer') user_datastore.add_role_to_user(user,role) db.session.commit() print "print-user_registered_sighandler:", extra



回答3:

To add a role to a user you have to use flask-security's API:

flask_security.datastore.UserDatastore.add_role_to_user(user, role)

Defninitely you will do this inside your own create_account or similar code. ContextProcessors are used to prepare data before rendering templates. It means that you create or get User and Role objects then call the above API to assign the role to the user. Be more specific if you need further help.