实现修改管理员信息功能

This commit is contained in:
GoEdgeLab
2020-10-14 19:42:32 +08:00
parent c31ef4f558
commit f558eec79e
8 changed files with 155 additions and 6 deletions

View File

@@ -1,6 +1,11 @@
package login
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
actionutils.ParentAction
@@ -11,5 +16,63 @@ func (this *IndexAction) Init() {
}
func (this *IndexAction) RunGet(params struct{}) {
adminResp, err := this.RPC().AdminRPC().FindEnabledAdmin(this.AdminContext(), &pb.FindEnabledAdminRequest{AdminId: this.AdminId()})
if err != nil {
this.ErrorPage(err)
return
}
admin := adminResp.Admin
if admin == nil {
this.NotFound("admin", this.AdminId())
return
}
this.Data["admin"] = maps.Map{
"username": admin.Username,
"fullname": admin.Fullname,
}
this.Show()
}
func (this *IndexAction) RunPost(params struct {
Username string
Password string
Password2 string
Must *actions.Must
}) {
params.Must.
Field("username", params.Username).
Require("请输入登录用户名").
Match(`^[a-zA-Z0-9_]+$`, "用户名中只能包含英文、数字或下划线")
existsResp, err := this.RPC().AdminRPC().CheckAdminUsername(this.AdminContext(), &pb.CheckAdminUsernameRequest{
AdminId: this.AdminId(),
Username: params.Username,
})
if err != nil {
this.ErrorPage(err)
return
}
if existsResp.Exists {
this.FailField("username", "此用户名已经被别的管理员使用,请换一个")
}
if len(params.Password) > 0 {
if params.Password != params.Password2 {
this.FailField("password2", "两次输入的密码不一致")
}
}
_, err = this.RPC().AdminRPC().UpdateAdminLogin(this.AdminContext(), &pb.UpdateAdminLoginRequest{
AdminId: this.AdminId(),
Username: params.Username,
Password: params.Password,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -12,7 +12,7 @@ func init() {
Helper(helpers.NewUserMustAuth()).
Helper(settingutils.NewHelper("login")).
Prefix("/settings/login").
Get("", new(IndexAction)).
GetPost("", new(IndexAction)).
EndAll()
})
}

View File

@@ -1,6 +1,11 @@
package profile
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
actionutils.ParentAction
@@ -11,5 +16,41 @@ func (this *IndexAction) Init() {
}
func (this *IndexAction) RunGet(params struct{}) {
adminResp, err := this.RPC().AdminRPC().FindEnabledAdmin(this.AdminContext(), &pb.FindEnabledAdminRequest{AdminId: this.AdminId()})
if err != nil {
this.ErrorPage(err)
return
}
admin := adminResp.Admin
if admin == nil {
this.NotFound("admin", this.AdminId())
return
}
this.Data["admin"] = maps.Map{
"fullname": admin.Fullname,
}
this.Show()
}
func (this *IndexAction) RunPost(params struct {
Fullname string
Must *actions.Must
}) {
params.Must.
Field("fullname", params.Fullname).
Require("请输入你的姓名")
_, err := this.RPC().AdminRPC().UpdateAdmin(this.AdminContext(), &pb.UpdateAdminRequest{
AdminId: this.AdminId(),
Fullname: params.Fullname,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -12,7 +12,7 @@ func init() {
Helper(helpers.NewUserMustAuth()).
Helper(settingutils.NewHelper("profile")).
Prefix("/settings/profile").
Get("", new(IndexAction)).
GetPost("", new(IndexAction)).
EndAll()
})
}