修复bug:修复存在匿名未登录的请求情况下current_user对象无法正常获取id的问题

This commit is contained in:
azhengzz 2021-03-11 18:36:47 +08:00
parent aaea993956
commit 45cc96d11b
3 changed files with 15 additions and 5 deletions

View File

@ -20,6 +20,7 @@ def emit_dispatcher_result(id, type, result):
:param result: 结果
:return:
"""
user_id = current_user.id if current_user.is_authenticated else 0
socketio.emit(
SocketIOEvent.DISPATCHER_RESULT,
dict(
@ -27,7 +28,7 @@ def emit_dispatcher_result(id, type, result):
type=str(type),
result=str(result),
),
namespace='/user/' + str(current_user.id),
namespace='/user/' + str(user_id),
)
@ -38,6 +39,7 @@ def emit_dispatcher_start(id, type, report_id):
:param type:调度类型 project/module
:param report_id: 报告id
"""
user_id = current_user.id if current_user.is_authenticated else 0
socketio.emit(
SocketIOEvent.DISPATCHER_BEGIN,
dict(
@ -45,7 +47,7 @@ def emit_dispatcher_start(id, type, report_id):
type=str(type).lower(),
report_id=str(report_id),
),
namespace='/user/' + str(current_user.id),
namespace='/user/' + str(user_id),
)
@ -56,6 +58,7 @@ def emit_dispatcher_end(id, type, end_type):
:param type:调度类型 project/module
:param end_type: 结束类型 DISPATCHER_END_TYPE
"""
user_id = current_user.id if current_user.is_authenticated else 0
socketio.emit(
SocketIOEvent.DISPATCHER_END,
dict(
@ -63,7 +66,7 @@ def emit_dispatcher_end(id, type, end_type):
type=str(type).lower(),
end_type=end_type,
),
namespace='/user/' + str(current_user.id),
namespace='/user/' + str(user_id),
)
# def test_message(message):

View File

@ -2668,7 +2668,13 @@ class Dispatcher(db.Model):
@classmethod
def add(cls, element_type, element_id):
dispatcher = cls(element_type=element_type, element_id=element_id, user_id=current_user.id)
# 定时构建会在服务器启动后第一个请求前app.before_first_request注册
# 而此时如果用户未登录则请求上下文中无法获取正确的用户id即current_user.id
# 因此这里需要做下判断
if current_user.is_authenticated:
dispatcher = cls(element_type=element_type, element_id=element_id, user_id=current_user.id)
else:
dispatcher = cls(element_type=element_type, element_id=element_id)
db.session.add(dispatcher)
db.session.commit()
return dispatcher

View File

@ -1,7 +1,7 @@
# coding=utf-8
from flask import send_file
from flask_login import current_user
from flask_login import current_user, login_required
import os
from app.utils import bp
@ -9,6 +9,7 @@ from app.config import attachment_dir
@bp.route("/download/<string:filename>", methods=['GET'])
@login_required
def download_file(filename):
user_id = current_user.id
file_path = os.path.join(attachment_dir, str(user_id), filename)