-
Notifications
You must be signed in to change notification settings - Fork 2
/
createPopup.go
128 lines (112 loc) · 3.02 KB
/
createPopup.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package admins
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"github.com/xlzd/gotp"
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreatePopupAction) RunGet(params struct{}) {
this.Data["modules"] = configloaders.AllModuleMaps(this.LangCode())
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
Fullname string
Username string
Pass1 string
Pass2 string
ModuleCodes []string
IsSuper bool
CanLogin bool
// OTP
OtpOn bool
Must *actions.Must
CSRF *actionutils.CSRF
}) {
params.Must.
Field("fullname", params.Fullname).
Require("请输入系统用户全名")
params.Must.
Field("username", params.Username).
Require("请输入登录用户名").
Match(`^[a-zA-Z0-9_]+$`, "用户名中只能包含英文、数字或下划线")
existsResp, err := this.RPC().AdminRPC().CheckAdminUsername(this.AdminContext(), &pb.CheckAdminUsernameRequest{
AdminId: 0,
Username: params.Username,
})
if err != nil {
this.ErrorPage(err)
return
}
if existsResp.Exists {
this.FailField("username", "此用户名已经被别的系统用户使用,请换一个")
}
params.Must.
Field("pass1", params.Pass1).
Require("请输入登录密码").
Field("pass2", params.Pass2).
Require("请输入确认登录密码")
if params.Pass1 != params.Pass2 {
this.FailField("pass2", "两次输入的密码不一致")
}
modules := []*systemconfigs.AdminModule{}
for _, code := range params.ModuleCodes {
modules = append(modules, &systemconfigs.AdminModule{
Code: code,
AllowAll: true,
Actions: nil, // TODO 后期再开放细粒度控制
})
}
modulesJSON, err := json.Marshal(modules)
if err != nil {
this.ErrorPage(err)
return
}
createResp, err := this.RPC().AdminRPC().CreateAdmin(this.AdminContext(), &pb.CreateAdminRequest{
Username: params.Username,
Password: params.Pass1,
Fullname: params.Fullname,
ModulesJSON: modulesJSON,
IsSuper: params.IsSuper,
CanLogin: params.CanLogin,
})
if err != nil {
this.ErrorPage(err)
return
}
// OTP
if params.OtpOn {
_, err = this.RPC().LoginRPC().UpdateLogin(this.AdminContext(), &pb.UpdateLoginRequest{Login: &pb.Login{
Id: 0,
Type: "otp",
ParamsJSON: maps.Map{
"secret": gotp.RandomSecret(16), // TODO 改成可以设置secret长度
}.AsJSON(),
IsOn: true,
AdminId: createResp.AdminId,
UserId: 0,
}})
if err != nil {
this.ErrorPage(err)
return
}
}
defer this.CreateLogInfo(codes.Admin_LogCreateAdmin, createResp.AdminId)
// 通知更改
err = configloaders.NotifyAdminModuleMappingChange()
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}