From 387da11f1b98a80b9bfb4559eb89bd92822fc83f Mon Sep 17 00:00:00 2001 From: naiba Date: Tue, 22 Oct 2024 21:19:30 +0800 Subject: [PATCH] feat: create user, read users, batch delete users --- cmd/dashboard/controller/controller.go | 14 ++-- cmd/dashboard/controller/ddns.go | 4 +- cmd/dashboard/controller/server.go | 2 +- cmd/dashboard/controller/server_group.go | 4 +- cmd/dashboard/controller/user.go | 86 ++++++++++++++++++++++++ model/user.go | 2 +- model/user_api.go | 6 ++ 7 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 cmd/dashboard/controller/user.go create mode 100644 model/user_api.go diff --git a/cmd/dashboard/controller/controller.go b/cmd/dashboard/controller/controller.go index 87284b7..de71bda 100644 --- a/cmd/dashboard/controller/controller.go +++ b/cmd/dashboard/controller/controller.go @@ -63,17 +63,21 @@ func routers(r *gin.Engine) { auth := api.Group("", authMiddleware.MiddlewareFunc()) auth.GET("/refresh_token", authMiddleware.RefreshHandler) - auth.POST("/server-group", commonHandler(newServerGroup)) - auth.PATCH("/server-group/:id", commonHandler(editServerGroup)) + auth.GET("/user", commonHandler(listUser)) + auth.POST("/user", commonHandler(createUser)) + auth.POST("/batch-delete/user", commonHandler(batchDeleteUser)) + + auth.POST("/server-group", commonHandler(createServerGroup)) + auth.PATCH("/server-group/:id", commonHandler(updateServerGroup)) auth.POST("/batch-delete/server-group", commonHandler(batchDeleteServerGroup)) - auth.PATCH("/server/:id", commonHandler(editServer)) + auth.PATCH("/server/:id", commonHandler(updateServer)) auth.POST("/batch-delete/server", commonHandler(batchDeleteServer)) auth.GET("/ddns", commonHandler(listDDNS)) auth.GET("/ddns/providers", commonHandler(listProviders)) - auth.POST("/ddns", commonHandler(newDDNS)) - auth.PATCH("/ddns/:id", commonHandler(editDDNS)) + auth.POST("/ddns", commonHandler(createDDNS)) + auth.PATCH("/ddns/:id", commonHandler(updateDDNS)) auth.POST("/batch-delete/ddns", commonHandler(batchDeleteDDNS)) } diff --git a/cmd/dashboard/controller/ddns.go b/cmd/dashboard/controller/ddns.go index 805a346..059f7d4 100644 --- a/cmd/dashboard/controller/ddns.go +++ b/cmd/dashboard/controller/ddns.go @@ -25,7 +25,7 @@ import ( // @Produce json // @Success 200 {object} model.CommonResponse[any] // @Router /ddns [post] -func newDDNS(c *gin.Context) error { +func createDDNS(c *gin.Context) error { var df model.DDNSForm var p model.DDNSProfile @@ -86,7 +86,7 @@ func newDDNS(c *gin.Context) error { // @Produce json // @Success 200 {object} model.CommonResponse[any] // @Router /ddns/{id} [patch] -func editDDNS(c *gin.Context) error { +func updateDDNS(c *gin.Context) error { idStr := c.Param("id") id, err := strconv.ParseUint(idStr, 10, 64) diff --git a/cmd/dashboard/controller/server.go b/cmd/dashboard/controller/server.go index a7571fd..4672902 100644 --- a/cmd/dashboard/controller/server.go +++ b/cmd/dashboard/controller/server.go @@ -24,7 +24,7 @@ import ( // @Produce json // @Success 200 {object} model.CommonResponse[any] // @Router /server/{id} [patch] -func editServer(c *gin.Context) error { +func updateServer(c *gin.Context) error { idStr := c.Param("id") id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { diff --git a/cmd/dashboard/controller/server_group.go b/cmd/dashboard/controller/server_group.go index 8811261..a8d6224 100644 --- a/cmd/dashboard/controller/server_group.go +++ b/cmd/dashboard/controller/server_group.go @@ -64,7 +64,7 @@ func listServerGroup(c *gin.Context) error { // @Produce json // @Success 200 {object} model.CommonResponse[any] // @Router /server-group [post] -func newServerGroup(c *gin.Context) error { +func createServerGroup(c *gin.Context) error { var sgf model.ServerGroupForm if err := c.ShouldBindJSON(&sgf); err != nil { return err @@ -114,7 +114,7 @@ func newServerGroup(c *gin.Context) error { // @Produce json // @Success 200 {object} model.CommonResponse[any] // @Router /server-group/{id} [patch] -func editServerGroup(c *gin.Context) error { +func updateServerGroup(c *gin.Context) error { id := c.Param("id") var sg model.ServerGroupForm if err := c.ShouldBindJSON(&sg); err != nil { diff --git a/cmd/dashboard/controller/user.go b/cmd/dashboard/controller/user.go new file mode 100644 index 0000000..84e9f44 --- /dev/null +++ b/cmd/dashboard/controller/user.go @@ -0,0 +1,86 @@ +package controller + +import ( + "errors" + + "github.com/gin-gonic/gin" + "github.com/naiba/nezha/model" + "github.com/naiba/nezha/service/singleton" + "golang.org/x/crypto/bcrypt" +) + +// List user +// @Summary List user +// @Security BearerAuth +// @Schemes +// @Description List user +// @Tags auth required +// @Produce json +// @Success 200 {object} model.CommonResponse[[]model.User] +// @Router /user [get] +func listUser(c *gin.Context) error { + var users []model.User + if err := singleton.DB.Find(&users).Error; err != nil { + return err + } + c.JSON(200, model.CommonResponse[[]model.User]{ + Success: true, + Data: users, + }) + return nil +} + +// Create user +// @Summary Create user +// @Security BearerAuth +// @Schemes +// @Description Create user +// @Tags auth required +// @Accept json +// @param request body model.UserForm true "User Request" +// @Produce json +// @Success 200 {object} model.CommonResponse[any] +// @Router /user [post] +func createUser(c *gin.Context) error { + var uf model.UserForm + if err := c.ShouldBindJSON(&uf); err != nil { + return err + } + + if len(uf.Password) < 6 { + return errors.New("password length must be greater than 6") + } + if uf.Username == "" { + return errors.New("username can't be empty") + } + + var u model.User + u.Username = uf.Username + + hash, err := bcrypt.GenerateFromPassword([]byte(uf.Password), bcrypt.DefaultCost) + if err != nil { + return err + } + u.Password = string(hash) + + return singleton.DB.Create(&u).Error +} + +// Batch delete users +// @Summary Batch delete users +// @Security BearerAuth +// @Schemes +// @Description Batch delete users +// @Tags auth required +// @Accept json +// @param request body []uint true "id list" +// @Produce json +// @Success 200 {object} model.CommonResponse[any] +// @Router /batch-delete/user [post] +func batchDeleteUser(c *gin.Context) error { + var ids []uint + if err := c.ShouldBindJSON(&ids); err != nil { + return err + } + return singleton.DB.Where("id IN (?)", ids).Delete(&model.User{}).Error +} diff --git a/model/user.go b/model/user.go index 17288cc..a09c8a6 100644 --- a/model/user.go +++ b/model/user.go @@ -2,6 +2,6 @@ package model type User struct { Common - Username string `json:"username,omitempty"` + Username string `json:"username,omitempty" gorm:"uniqueIndex"` Password string `json:"password,omitempty" gorm:"type:char(72)"` } diff --git a/model/user_api.go b/model/user_api.go new file mode 100644 index 0000000..135dce0 --- /dev/null +++ b/model/user_api.go @@ -0,0 +1,6 @@ +package model + +type UserForm struct { + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty" gorm:"type:char(72)"` +}