This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
/
admin_user_add.go
69 lines (59 loc) · 1.68 KB
/
admin_user_add.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
package http
import (
"github.com/ArtalkJS/ArtalkGo/lib"
"github.com/ArtalkJS/ArtalkGo/model"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
)
type ParamsAdminUserAdd struct {
Name string `mapstructure:"name" param:"required"`
Email string `mapstructure:"email" param:"required"`
Password string `mapstructure:"password"`
Link string `mapstructure:"link"`
IsAdmin bool `mapstructure:"is_admin" param:"required"`
SiteNames string `mapstructure:"site_names"`
ReceiveEmail bool `mapstructure:"receive_email" param:"required"`
BadgeName string `mapstructure:"badge_name"`
BadgeColor string `mapstructure:"badge_color"`
}
func (a *action) AdminUserAdd(c echo.Context) error {
if !GetIsSuperAdmin(c) {
return RespError(c, "无权操作")
}
var p ParamsAdminUserAdd
if isOK, resp := ParamsDecode(c, &p); !isOK {
return resp
}
if !model.FindUser(p.Name, p.Email).IsEmpty() {
return RespError(c, "用户已存在")
}
if !lib.ValidateEmail(p.Email) {
return RespError(c, "Invalid email")
}
if p.Link != "" && !lib.ValidateURL(p.Link) {
return RespError(c, "Invalid link")
}
user := model.User{}
user.Name = p.Name
user.Email = p.Email
user.Link = p.Link
user.IsAdmin = p.IsAdmin
user.SiteNames = p.SiteNames
user.ReceiveEmail = p.ReceiveEmail
user.BadgeName = p.BadgeName
user.BadgeColor = p.BadgeColor
if p.Password != "" {
err := user.SetPasswordEncrypt(p.Password)
if err != nil {
logrus.Errorln(err)
return RespError(c, "密码设置失败")
}
}
err := model.CreateUser(&user)
if err != nil {
return RespError(c, "user 创建失败")
}
return RespData(c, Map{
"user": user.ToCookedForAdmin(),
})
}