From 27dea093da5f3948e82fac14e3a0968c82f9a679 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Fri, 14 Jun 2024 12:41:45 +0800 Subject: [PATCH] fix: user password issue --- controllers/base_v2.go | 10 +++++++++- controllers/router_v2.go | 5 +++++ controllers/user_v2.go | 43 +++++++++++++++++++++++++++++++++++++++- interfaces/model.go | 7 +++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/controllers/base_v2.go b/controllers/base_v2.go index 4d3cd1a..3497d35 100644 --- a/controllers/base_v2.go +++ b/controllers/base_v2.go @@ -50,8 +50,11 @@ func (ctr *BaseControllerV2[T]) Post(c *gin.Context) { HandleErrorBadRequest(c, err) return } - m := any(&model).(interfaces.Model) + u := GetUserFromContextV2(c) + m := any(&model).(interfaces.ModelV2) m.SetId(primitive.NewObjectID()) + m.SetCreated(u.Id) + m.SetUpdated(u.Id) col := ctr.modelSvc.GetCol() res, err := col.GetCollection().InsertOne(col.GetContext(), m) if err != nil { @@ -81,6 +84,11 @@ func (ctr *BaseControllerV2[T]) PutById(c *gin.Context) { return } + u := GetUserFromContextV2(c) + m := any(&model).(interfaces.ModelV2) + m.SetId(primitive.NewObjectID()) + m.SetUpdated(u.Id) + if err := ctr.modelSvc.ReplaceById(id, model); err != nil { HandleErrorInternalServerError(c, err) return diff --git a/controllers/router_v2.go b/controllers/router_v2.go index e8d684e..47a445b 100644 --- a/controllers/router_v2.go +++ b/controllers/router_v2.go @@ -263,6 +263,11 @@ func InitRoutes(app *gin.Engine) (err error) { }, )) RegisterController(groups.AuthGroup, "/users", NewControllerV2[models.UserV2]( + Action{ + Method: http.MethodPost, + Path: "", + HandlerFunc: PostUser, + }, Action{ Method: http.MethodPost, Path: "/:id/change-password", diff --git a/controllers/user_v2.go b/controllers/user_v2.go index 412cf71..9c59365 100644 --- a/controllers/user_v2.go +++ b/controllers/user_v2.go @@ -8,6 +8,42 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) +func PostUser(c *gin.Context) { + var payload struct { + Username string `json:"username"` + Password string `json:"password"` + Role string `json:"role"` + Email string `json:"email"` + } + if err := c.ShouldBindJSON(&payload); err != nil { + HandleErrorBadRequest(c, err) + return + } + u := GetUserFromContextV2(c) + model := models.UserV2{ + Username: payload.Username, + Password: utils.EncryptMd5(payload.Password), + Role: payload.Role, + Email: payload.Email, + } + model.SetCreated(u.Id) + model.SetUpdated(u.Id) + id, err := service.NewModelServiceV2[models.UserV2]().InsertOne(model) + if err != nil { + HandleErrorInternalServerError(c, err) + return + } + + result, err := service.NewModelServiceV2[models.UserV2]().GetById(id) + if err != nil { + HandleErrorInternalServerError(c, err) + return + } + + HandleSuccessWithData(c, result) + +} + func PostUserChangePassword(c *gin.Context) { // get id id, err := primitive.ObjectIDFromHex(c.Param("id")) @@ -48,7 +84,12 @@ func PostUserChangePassword(c *gin.Context) { func GetUserMe(c *gin.Context) { u := GetUserFromContextV2(c) - HandleSuccessWithData(c, u) + _u, err := service.NewModelServiceV2[models.UserV2]().GetById(u.Id) + if err != nil { + HandleErrorInternalServerError(c, err) + return + } + HandleSuccessWithData(c, _u) } func PutUserById(c *gin.Context) { diff --git a/interfaces/model.go b/interfaces/model.go index 13a8feb..bcbc6f7 100644 --- a/interfaces/model.go +++ b/interfaces/model.go @@ -9,6 +9,13 @@ type Model interface { SetId(id primitive.ObjectID) } +type ModelV2 interface { + GetId() (id primitive.ObjectID) + SetId(id primitive.ObjectID) + SetCreated(by primitive.ObjectID) + SetUpdated(by primitive.ObjectID) +} + type ModelId int const (