有没有关于flask-principal的好教程?我正在尝试进行身份验证和授权(需求角色和需求身份),但是一无所获。
我几乎可以肯定没有真正全面的教程--也许你们中的一些人有时间并想发布一个教程作为答案?我非常决心使用flask而不是django,但需要解决这个问题。
有没有关于flask-principal的好教程?我正在尝试进行身份验证和授权(需求角色和需求身份),但是一无所获。
我几乎可以肯定没有真正全面的教程--也许你们中的一些人有时间并想发布一个教程作为答案?我非常决心使用flask而不是django,但需要解决这个问题。
identity_loaded
:当调用此信号时,我们知道要为用户创建一个身份对象。(通过Principal._set_thread_identity()
调用)
identity_changed
:当调用此信号时,我们知道要更新用户的身份。(调用它会执行Principal._on_identity_changed()
)
Principal._on_identity_changed()
被设置为identity_changed
信号的订阅者。每当发送信号identity_changed
时,就会执行_on_identity_changed()。代码看起来像这样:from blinker import signal
test = signal('test')
test.connect(func_we_want_to_execute_when_signal_is_called)
send()
时,信号处理程序将被执行。因此,对于我们的test
信号,语法只是这样的:test.send()
test.send()
时,将执行func_we_want_to_execute_when_signal_is_called
。希望这个FP文档中的例子现在更容易理解了:def login_view(req):
username = req.form.get('username')
# Your authentication here.
# Notice our signal (identity_changed) is being called (identity_changed.send())
# What function is being called? Principal._on_identity_changed()
identity_changed.send(app, identity=Identity(username))
@test.connect
def func_we_want_to_execute_when_signal_is_called():
return stuff
# We're setting up our signal (identity_loaded)
# to execute the function below (on_identity_loaded)
# when we call our signal (identity_loaded.send())
# which is called in Principal._set_thread_identity()
@identity_loaded.connect
def on_identity_loaded(sender, identity):
# Get the user information from the db
user = db.get(identity.name)
# Update the roles that a user can provide
for role in user.roles:
identity.provides.add(RoleNeed(role.name))
# Save the user somewhere so we only look it up once
identity.user = user