How to customize registration provided by Flask-security?

风流意气都作罢 提交于 2019-12-10 11:19:38

问题


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.



来源:https://stackoverflow.com/questions/25376647/how-to-customize-registration-provided-by-flask-security

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!