From 8a4a1d91c1944ba6ec4a09d8fc93d4f71bd0f7d1 Mon Sep 17 00:00:00 2001 From: marvzhang Date: Tue, 21 Apr 2020 13:31:11 +0800 Subject: [PATCH] fixed https://github.com/crawlab-team/crawlab/issues/670 --- backend/routes/user.go | 5 +++++ backend/services/user.go | 3 +++ 2 files changed, 8 insertions(+) diff --git a/backend/routes/user.go b/backend/routes/user.go index 0be8a5b9..56a8cb2c 100644 --- a/backend/routes/user.go +++ b/backend/routes/user.go @@ -98,6 +98,11 @@ func PutUser(c *gin.Context) { // UserId uid := services.GetCurrentUserId(c) + // 空 UserId 处理 + if uid == "" { + uid = bson.ObjectIdHex(constants.ObjectIdNull) + } + // 添加用户 if err := services.CreateNewUser(reqData.Username, reqData.Password, reqData.Role, reqData.Email, uid); err != nil { HandleError(http.StatusInternalServerError, c, err) diff --git a/backend/services/user.go b/backend/services/user.go index adc56136..fbad2c71 100644 --- a/backend/services/user.go +++ b/backend/services/user.go @@ -114,6 +114,9 @@ func CreateNewUser(username string, password string, role string, email string, func GetCurrentUser(c *gin.Context) *model.User { data, _ := c.Get(constants.ContextUser) + if data == nil { + return &model.User{} + } return data.(*model.User) }