105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
package login
|
|
|
|
import (
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
|
|
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
|
"github.com/iwind/TeaGo/actions"
|
|
"github.com/iwind/TeaGo/maps"
|
|
"regexp"
|
|
)
|
|
|
|
type IndexAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *IndexAction) Init() {
|
|
this.Nav("", "", "")
|
|
}
|
|
|
|
func (this *IndexAction) RunGet(params struct{}) {
|
|
userResp, err := this.RPC().UserRPC().FindEnabledUser(this.UserContext(), &pb.FindEnabledUserRequest{UserId: this.UserId()})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var user = userResp.User
|
|
if user == nil {
|
|
this.NotFound("user", this.UserId())
|
|
return
|
|
}
|
|
this.Data["user"] = maps.Map{
|
|
"username": user.Username,
|
|
"fullname": user.Fullname,
|
|
}
|
|
|
|
config, _ := configloaders.LoadRegisterConfig()
|
|
if config != nil {
|
|
this.Data["complexPassword"] = config.ComplexPassword
|
|
} else {
|
|
this.Data["complexPassword"] = false
|
|
}
|
|
|
|
this.Show()
|
|
}
|
|
|
|
func (this *IndexAction) RunPost(params struct {
|
|
Username string
|
|
Password string
|
|
Password2 string
|
|
|
|
Must *actions.Must
|
|
}) {
|
|
defer this.CreateLogInfo(codes.UserLogin_LogUpdateLogin)
|
|
|
|
var must = params.Must
|
|
must.Field("username", params.Username).
|
|
Require("请输入用户名").
|
|
Match("^[a-zA-Z0-9_]+$", "用户名中只能包含英文字母、数字、下划线").
|
|
MinLength(6, "用户名长度不能少于6位")
|
|
|
|
existsResp, err := this.RPC().UserRPC().CheckUserUsername(this.UserContext(), &pb.CheckUserUsernameRequest{
|
|
UserId: this.UserId(),
|
|
Username: params.Username,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if existsResp.Exists {
|
|
this.FailField("username", "此用户名已经被别的用户使用,请换一个")
|
|
}
|
|
|
|
// 密码
|
|
if len(params.Password) > 0 {
|
|
must.
|
|
Field("password", params.Password).
|
|
Require("请输入密码").
|
|
MinLength(6, "密码长度不能小于6位")
|
|
|
|
config, err := configloaders.LoadRegisterConfig()
|
|
if err == nil {
|
|
if config.ComplexPassword && (!regexp.MustCompile(`[a-z]`).MatchString(params.Password) || !regexp.MustCompile(`[A-Z]`).MatchString(params.Password)) {
|
|
this.FailField("password", "为了您的账号安全,密码中必须包含大写和小写字母")
|
|
}
|
|
}
|
|
|
|
if params.Password != params.Password2 {
|
|
this.FailField("password2", "两次输入的密码不一致")
|
|
}
|
|
}
|
|
|
|
_, err = this.RPC().UserRPC().UpdateUserLogin(this.UserContext(), &pb.UpdateUserLoginRequest{
|
|
UserId: this.UserId(),
|
|
Username: params.Username,
|
|
Password: params.Password,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Success()
|
|
}
|