[英]is this way of using current_user from Flask-Login safe?
我無法理解 Flask-Login 庫中的 current_user 如何工作。 例如,當兩個用戶同時訪問同一路由並使用其 function 時,我調用多個模塊,這些模塊也使用 current_user 作為導入。 我將用一些代碼詳細說明:
我有一條名為 update_account 的路線(我刪除了一些部分,因為它們與我的問題無關):
@users.route('/account/user/<username>/update', methods=['GET', 'POST'])
def update_account(username):
update_account_form = UpdateForm()
if update_account_form.validate_on_submit():
#here we handle updating from another module
if AccountManager.update_account(update_account_form): #retuns True if no errors has occured
flash('Your account has been successfully updated', "success")
return redirect(url_for('users.update_account', username=current_user.username))
flash('Your client matched max requests', "warning")
return redirect(url_for('users.update_account', username=current_user.username))
return render_template('account/update.html', update_form=update_account_form)
我的問題是關於我調用AccountManager.update_account(update_account_form)
的部分,因為我沒有傳遞任何 current_users 數據,而是在該模塊中導入 current_user,這就是我獲取數據的方式。 以下是我如何實現的:
from flask_login import login_user, current_user
class AccountManager:
@staticmethod
def update_account(account_form):
if current_user.request_counter >= 5:
return False
current_user.username = account_form.username.data.strip()
current_user.email = account_form.email.data.strip()
if account_form.change_password.data:
current_user.password = bcrypt.generate_password_hash(account_form.password.data).decode('utf-8')
db.session.commit()
return True
我的問題就在這里。 這安全嗎? 我應該將 current_user 作為參數傳遞而不是在此處導入嗎? 因為也許如果另一個請求來了 current_user 改變,這個方法會改變其他人的數據。
謝謝你的時間。
你在做什么很好。
current_user
取決於請求上下文,因此您不會僅僅因為另一個用戶的請求在您完成第一個處理之前進入而獲得不同的用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.