Files
waf-platform/EdgeUser/internal/web/actions/default/settings/email-verify/index.go

126 lines
3.2 KiB
Go

// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package emailverify
import (
"errors"
"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"
timeutil "github.com/iwind/TeaGo/utils/time"
"net/mail"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct{}) {
// 是否可以激活Email
registerConfig, _ := configloaders.LoadRegisterConfig()
this.Data["canVerify"] = false
if registerConfig == nil {
this.Show()
return
}
var canVerify = registerConfig.EmailVerification.IsOn
if !canVerify {
this.Show()
return
}
this.Data["canVerify"] = true
// 当前已激活邮箱
userResp, err := this.RPC().UserRPC().FindEnabledUser(this.UserContext(), &pb.FindEnabledUserRequest{
UserId: this.UserId(),
})
if err != nil {
this.ErrorPage(err)
return
}
if userResp.User == nil {
this.ErrorPage(errors.New("can not find user info"))
return
}
this.Data["verifiedEmail"] = userResp.User.VerifiedEmail
// 现在正等待激活的邮箱
latestVerificationResp, err := this.RPC().UserEmailVerificationRPC().FindLatestUserEmailVerification(this.UserContext(), &pb.FindLatestUserEmailVerificationRequest{})
if err != nil {
this.ErrorPage(err)
return
}
var latestVerification = latestVerificationResp.UserEmailVerification
if latestVerification == nil {
this.Data["latestVerification"] = nil
} else {
this.Data["latestVerification"] = maps.Map{
"email": latestVerification.Email,
"expiresTime": timeutil.FormatTime("Y-m-d H:i:s", latestVerification.ExpiresAt),
"isSent": latestVerification.IsSent,
}
}
this.Show()
}
func (this *IndexAction) RunPost(params struct {
Email string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
if len(params.Email) == 0 {
this.FailField("email", "请输入要激活的邮箱")
return
}
_, err := mail.ParseAddress(params.Email)
if err != nil {
this.FailField("email", "邮箱格式不正确")
return
}
// 检查邮件是否已被使用
checkResp, err := this.RPC().UserRPC().CheckUserEmail(this.UserContext(), &pb.CheckUserEmailRequest{Email: params.Email})
if err != nil {
this.ErrorPage(err)
return
}
if checkResp.Exists {
this.FailField("email", "当前邮箱已被别的用户使用,请换一个")
return
}
// 如果同自己已认证邮箱一致,则不重复发送
userResp, err := this.RPC().UserRPC().FindEnabledUser(this.UserContext(), &pb.FindEnabledUserRequest{UserId: this.UserId()})
if err != nil {
this.ErrorPage(err)
return
}
if userResp.User == nil {
this.Fail("can not find current user")
return
}
if userResp.User.VerifiedEmail == params.Email {
this.FailField("email", "此邮箱已激活,无需再次激活")
return
}
// 发送激活邮件
_, err = this.RPC().UserEmailVerificationRPC().SendUserEmailVerification(this.UserContext(), &pb.SendUserEmailVerificationRequest{Email: params.Email})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}