-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
user_create.go
85 lines (75 loc) · 2.89 KB
/
user_create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package handler
import (
"github.com/ArtalkJS/Artalk/internal/core"
"github.com/ArtalkJS/Artalk/internal/entity"
"github.com/ArtalkJS/Artalk/internal/i18n"
"github.com/ArtalkJS/Artalk/internal/log"
"github.com/ArtalkJS/Artalk/internal/utils"
"github.com/ArtalkJS/Artalk/server/common"
"github.com/gofiber/fiber/v2"
)
type ParamsUserCreate struct {
Name string `json:"name" validate:"required"` // The user name
Email string `json:"email" validate:"required"` // The user email
Password string `json:"password" validate:"optional"` // The user password
Link string `json:"link" validate:"optional"` // The user link
IsAdmin bool `json:"is_admin" validate:"required"` // The user is an admin
ReceiveEmail bool `json:"receive_email" validate:"required"` // The user receive email
BadgeName string `json:"badge_name" validate:"optional"` // The user badge name
BadgeColor string `json:"badge_color" validate:"optional"` // The user badge color (hex format)
}
type ResponseUserCreate struct {
entity.CookedUserForAdmin
}
// @Id CreateUser
// @Summary Create User
// @Description Create a new user
// @Tags User
// @Param user body ParamsUserCreate true "The user data"
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Success 200 {object} ResponseUserCreate
// @Failure 400 {object} Map{msg=string}
// @Failure 403 {object} Map{msg=string}
// @Failure 500 {object} Map{msg=string}
// @Router /users [post]
func UserCreate(app *core.App, router fiber.Router) {
router.Post("/users", common.AdminGuard(app, func(c *fiber.Ctx) error {
var p ParamsUserCreate
if isOK, resp := common.ParamsDecode(c, &p); !isOK {
return resp
}
if !app.Dao().FindUser(p.Name, p.Email).IsEmpty() {
return common.RespError(c, 400, i18n.T("{{name}} already exists", Map{"name": i18n.T("User")}))
}
if !utils.ValidateEmail(p.Email) {
return common.RespError(c, 400, i18n.T("Invalid {{name}}", Map{"name": i18n.T("Email")}))
}
if p.Link != "" && !utils.ValidateURL(p.Link) {
return common.RespError(c, 400, i18n.T("Invalid {{name}}", Map{"name": i18n.T("Link")}))
}
user := entity.User{}
user.Name = p.Name
user.Email = p.Email
user.Link = p.Link
user.IsAdmin = p.IsAdmin
user.ReceiveEmail = p.ReceiveEmail
user.BadgeName = p.BadgeName
user.BadgeColor = p.BadgeColor
if p.Password != "" {
err := user.SetPasswordEncrypt(p.Password)
if err != nil {
log.Errorln(err)
return common.RespError(c, 500, i18n.T("Password update failed"))
}
}
err := app.Dao().CreateUser(&user)
if err != nil {
return common.RespError(c, 500, i18n.T("{{name}} creation failed", Map{"name": i18n.T("User")}))
}
return common.RespData(c, ResponseUserCreate{
CookedUserForAdmin: app.Dao().UserToCookedForAdmin(&user),
})
}))
}