2020-12-04 16:00:55 +08:00
|
|
|
package users
|
|
|
|
|
|
|
|
|
|
import (
|
2022-08-28 20:21:57 +08:00
|
|
|
"encoding/json"
|
2020-12-04 16:00:55 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2022-08-28 20:21:57 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
2020-12-04 16:00:55 +08:00
|
|
|
"github.com/iwind/TeaGo/actions"
|
2022-07-24 16:14:38 +08:00
|
|
|
"github.com/iwind/TeaGo/maps"
|
|
|
|
|
"github.com/xlzd/gotp"
|
2020-12-04 16:00:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CreatePopupAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreatePopupAction) Init() {
|
|
|
|
|
this.Nav("", "", "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreatePopupAction) RunGet(params struct{}) {
|
|
|
|
|
this.Show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreatePopupAction) RunPost(params struct {
|
2022-08-28 20:21:57 +08:00
|
|
|
Username string
|
|
|
|
|
Pass1 string
|
|
|
|
|
Pass2 string
|
|
|
|
|
Fullname string
|
|
|
|
|
Mobile string
|
|
|
|
|
Tel string
|
|
|
|
|
Email string
|
|
|
|
|
Remark string
|
|
|
|
|
ClusterId int64
|
|
|
|
|
FeaturesType string
|
2020-12-04 16:00:55 +08:00
|
|
|
|
2022-07-24 16:14:38 +08:00
|
|
|
// OTP
|
|
|
|
|
OtpOn bool
|
|
|
|
|
|
2020-12-04 16:00:55 +08:00
|
|
|
Must *actions.Must
|
|
|
|
|
CSRF *actionutils.CSRF
|
|
|
|
|
}) {
|
2022-08-28 20:21:57 +08:00
|
|
|
var userId int64
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
this.CreateLogInfo("创建用户 %d", userId)
|
|
|
|
|
}()
|
|
|
|
|
|
2020-12-04 16:00:55 +08:00
|
|
|
params.Must.
|
|
|
|
|
Field("username", params.Username).
|
|
|
|
|
Require("请输入用户名").
|
|
|
|
|
Match(`^[a-zA-Z0-9_]+$`, "用户名中只能含有英文、数字和下划线")
|
|
|
|
|
|
2020-12-15 11:53:05 +08:00
|
|
|
checkUsernameResp, err := this.RPC().UserRPC().CheckUserUsername(this.AdminContext(), &pb.CheckUserUsernameRequest{
|
2020-12-04 16:00:55 +08:00
|
|
|
UserId: 0,
|
|
|
|
|
Username: params.Username,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if checkUsernameResp.Exists {
|
|
|
|
|
this.FailField("username", "此用户名已经被占用,请换一个")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params.Must.
|
|
|
|
|
Field("pass1", params.Pass1).
|
|
|
|
|
Require("请输入密码").
|
|
|
|
|
Field("pass2", params.Pass2).
|
|
|
|
|
Require("请再次输入确认密码").
|
|
|
|
|
Equal(params.Pass1, "两次输入的密码不一致")
|
|
|
|
|
|
|
|
|
|
params.Must.
|
|
|
|
|
Field("fullname", params.Fullname).
|
|
|
|
|
Require("请输入全名")
|
|
|
|
|
|
2020-12-16 15:49:15 +08:00
|
|
|
if params.ClusterId <= 0 {
|
|
|
|
|
this.Fail("请选择关联集群")
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 16:00:55 +08:00
|
|
|
if len(params.Mobile) > 0 {
|
|
|
|
|
params.Must.
|
|
|
|
|
Field("mobile", params.Mobile).
|
|
|
|
|
Mobile("请输入正确的手机号")
|
|
|
|
|
}
|
|
|
|
|
if len(params.Email) > 0 {
|
|
|
|
|
params.Must.
|
|
|
|
|
Field("email", params.Email).
|
|
|
|
|
Email("请输入正确的电子邮箱")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createResp, err := this.RPC().UserRPC().CreateUser(this.AdminContext(), &pb.CreateUserRequest{
|
2020-12-17 15:50:44 +08:00
|
|
|
Username: params.Username,
|
|
|
|
|
Password: params.Pass1,
|
|
|
|
|
Fullname: params.Fullname,
|
|
|
|
|
Mobile: params.Mobile,
|
|
|
|
|
Tel: params.Tel,
|
|
|
|
|
Email: params.Email,
|
|
|
|
|
Remark: params.Remark,
|
|
|
|
|
Source: "admin:" + numberutils.FormatInt64(this.AdminId()),
|
|
|
|
|
NodeClusterId: params.ClusterId,
|
2020-12-04 16:00:55 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-07-24 16:14:38 +08:00
|
|
|
|
2022-08-28 20:21:57 +08:00
|
|
|
userId = createResp.UserId
|
2022-07-24 16:14:38 +08:00
|
|
|
|
2022-08-28 20:21:57 +08:00
|
|
|
// 功能
|
|
|
|
|
if params.FeaturesType == "default" {
|
|
|
|
|
resp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeUserRegisterConfig})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var config = userconfigs.DefaultUserRegisterConfig()
|
|
|
|
|
if len(resp.ValueJSON) > 0 {
|
|
|
|
|
err = json.Unmarshal(resp.ValueJSON, config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_, err = this.RPC().UserRPC().UpdateUserFeatures(this.AdminContext(), &pb.UpdateUserFeaturesRequest{
|
|
|
|
|
UserId: userId,
|
|
|
|
|
FeatureCodes: config.Features,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if params.FeaturesType == "all" {
|
|
|
|
|
featuresResp, err := this.RPC().UserRPC().FindAllUserFeatureDefinitions(this.AdminContext(), &pb.FindAllUserFeatureDefinitionsRequest{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var featureCodes = []string{}
|
|
|
|
|
for _, def := range featuresResp.Features {
|
|
|
|
|
featureCodes = append(featureCodes, def.Code)
|
|
|
|
|
}
|
|
|
|
|
_, err = this.RPC().UserRPC().UpdateUserFeatures(this.AdminContext(), &pb.UpdateUserFeaturesRequest{
|
|
|
|
|
UserId: userId,
|
|
|
|
|
FeatureCodes: featureCodes,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-24 16:14:38 +08:00
|
|
|
|
|
|
|
|
// 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: 0,
|
|
|
|
|
UserId: userId,
|
|
|
|
|
}})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-04 16:00:55 +08:00
|
|
|
|
|
|
|
|
this.Success()
|
|
|
|
|
}
|