mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-03 12:20:28 +08:00
单体应用初始化的时候自动进入修改用户名密码界面
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||||
"github.com/iwind/TeaGo/actions"
|
"github.com/iwind/TeaGo/actions"
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
@@ -121,6 +122,19 @@ func (this *IndexAction) RunGet(params struct {
|
|||||||
// 删除Cookie
|
// 删除Cookie
|
||||||
loginutils.UnsetCookie(this.Object())
|
loginutils.UnsetCookie(this.Object())
|
||||||
|
|
||||||
|
// 检查单体实例是否已经被初始化
|
||||||
|
{
|
||||||
|
settingResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeStandaloneInstanceInitialized})
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if string(settingResp.ValueJSON) == "0" {
|
||||||
|
this.RedirectURL("/initPassword")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.Show()
|
this.Show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ func init() {
|
|||||||
Prefix("").
|
Prefix("").
|
||||||
GetPost("/", new(IndexAction)).
|
GetPost("/", new(IndexAction)).
|
||||||
GetPost("/index/otp", new(OtpAction)).
|
GetPost("/index/otp", new(OtpAction)).
|
||||||
|
GetPost("/initPassword", new(InitPasswordAction)).
|
||||||
EndAll()
|
EndAll()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
104
internal/web/actions/default/index/initPassword.go
Normal file
104
internal/web/actions/default/index/initPassword.go
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||||
|
|
||||||
|
package index
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||||
|
"github.com/iwind/TeaGo/actions"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InitPasswordAction struct {
|
||||||
|
actionutils.ParentAction
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *InitPasswordAction) Init() {
|
||||||
|
this.Nav("", "", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *InitPasswordAction) RunGet(params struct{}) {
|
||||||
|
isNotInitialized, err := this.isNotInitialized()
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !isNotInitialized {
|
||||||
|
this.RedirectURL("/")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.Data["username"] = "admin"
|
||||||
|
this.Data["password"] = ""
|
||||||
|
|
||||||
|
this.Show()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *InitPasswordAction) RunPost(params struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
|
||||||
|
Must *actions.Must
|
||||||
|
CSRF *actionutils.CSRF
|
||||||
|
}) {
|
||||||
|
isNotInitialized, err := this.isNotInitialized()
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !isNotInitialized {
|
||||||
|
this.ResponseWriter.WriteHeader(http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
params.Must.
|
||||||
|
Field("username", params.Username).
|
||||||
|
Require("请输入登录用户名").
|
||||||
|
Match(`^[a-zA-Z0-9_]+$`, "用户名中只能包含英文、数字或下划线").
|
||||||
|
Field("password", params.Password).
|
||||||
|
Require("请输入密码")
|
||||||
|
|
||||||
|
// 查找ID
|
||||||
|
adminResp, err := this.RPC().AdminRPC().FindAdminWithUsername(this.AdminContext(), &pb.FindAdminWithUsernameRequest{Username: "admin" /** 固定的 **/})
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if adminResp.Admin == nil {
|
||||||
|
this.Fail("数据错误,请将数据库中的edgeAdmins表中的用户名修改为admin后再试")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var adminId = adminResp.Admin.Id
|
||||||
|
|
||||||
|
// 修改密码
|
||||||
|
_, err = this.RPC().AdminRPC().UpdateAdminLogin(this.AdminContext(), &pb.UpdateAdminLoginRequest{
|
||||||
|
AdminId: adminId,
|
||||||
|
Username: params.Username,
|
||||||
|
Password: params.Password, // raw
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改为初始化完成
|
||||||
|
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
|
||||||
|
Code: systemconfigs.SettingCodeStandaloneInstanceInitialized,
|
||||||
|
ValueJSON: []byte("1"),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.Success()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *InitPasswordAction) isNotInitialized() (bool, error) {
|
||||||
|
settingResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeStandaloneInstanceInitialized})
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return string(settingResp.ValueJSON) == "0", nil
|
||||||
|
}
|
||||||
38
web/views/@default/index/initPassword.css
Normal file
38
web/views/@default/index/initPassword.css
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
.form-box {
|
||||||
|
position: fixed;
|
||||||
|
top: 2em;
|
||||||
|
bottom: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
}
|
||||||
|
form {
|
||||||
|
position: fixed;
|
||||||
|
width: 21em;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
margin-left: -10em;
|
||||||
|
margin-top: -16em;
|
||||||
|
}
|
||||||
|
form .header {
|
||||||
|
text-align: center;
|
||||||
|
font-size: 1em !important;
|
||||||
|
}
|
||||||
|
form p {
|
||||||
|
font-size: 0.8em;
|
||||||
|
margin-top: 0.3em;
|
||||||
|
margin-bottom: 0;
|
||||||
|
font-weight: normal;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
form .comment {
|
||||||
|
margin-top: 0.5em;
|
||||||
|
padding: 0.5em;
|
||||||
|
color: gray;
|
||||||
|
}
|
||||||
|
@media screen and (max-width: 512px) {
|
||||||
|
form {
|
||||||
|
width: 80%;
|
||||||
|
margin-left: -40%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*# sourceMappingURL=initPassword.css.map */
|
||||||
1
web/views/@default/index/initPassword.css.map
Normal file
1
web/views/@default/index/initPassword.css.map
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"sources":["initPassword.less"],"names":[],"mappings":"AAAA;EACI,eAAA;EACA,QAAA;EACA,SAAA;EACA,OAAA;EACA,QAAA;;AAGJ;EACI,eAAA;EACA,WAAA;EACA,QAAA;EACA,SAAA;EACA,kBAAA;EACA,iBAAA;;AANJ,IAQC;EACC,kBAAA;EACA,yBAAA;;AAVF,IAaC;EACC,gBAAA;EACA,iBAAA;EACA,gBAAA;EACA,mBAAA;EACA,UAAA;;AAlBF,IAqBC;EACC,iBAAA;EACA,cAAA;EACA,WAAA;;AAIF,mBAAqC;EACjC;IACI,UAAA;IACA,iBAAA","file":"initPassword.css"}
|
||||||
44
web/views/@default/index/initPassword.html
Normal file
44
web/views/@default/index/initPassword.html
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||||
|
<title>初始化系统</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
|
||||||
|
{$TEA.VUE}
|
||||||
|
{$TEA.SEMANTIC}
|
||||||
|
<script type="text/javascript" src="/js/md5.min.js"></script>
|
||||||
|
<script type="text/javascript" src="/js/utils.min.js"></script>
|
||||||
|
<script type="text/javascript" src="/js/sweetalert2/dist/sweetalert2.all.min.js"></script>
|
||||||
|
<script type="text/javascript" src="/js/components.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<div class="form-box">
|
||||||
|
<form method="post" class="ui form" data-tea-action="$" data-tea-success="success" autocomplete="off">
|
||||||
|
<csrf-token></csrf-token>
|
||||||
|
<div class="ui segment stacked">
|
||||||
|
<div class="ui header">
|
||||||
|
设置管理员初始密码
|
||||||
|
</div>
|
||||||
|
<div class="ui field">
|
||||||
|
<div class="ui left icon input">
|
||||||
|
<i class="ui user icon small"></i>
|
||||||
|
<input type="text" name="username" v-model="username" placeholder="请输入用户名" maxlength="200" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="ui field">
|
||||||
|
<div class="ui left icon input">
|
||||||
|
<i class="ui lock icon small"></i>
|
||||||
|
<input type="text" name="password" placeholder="请输入密码" maxlength="200" v-model="password"/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button class="ui button primary fluid" type="submit">保存用户名密码</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
7
web/views/@default/index/initPassword.js
Normal file
7
web/views/@default/index/initPassword.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
Tea.context(function () {
|
||||||
|
this.success = function () {
|
||||||
|
teaweb.success("用户名和密码保存成功,现在去登录", function () {
|
||||||
|
window.location = "/"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
42
web/views/@default/index/initPassword.less
Normal file
42
web/views/@default/index/initPassword.less
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
.form-box {
|
||||||
|
position: fixed;
|
||||||
|
top: 2em;
|
||||||
|
bottom: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
form {
|
||||||
|
position: fixed;
|
||||||
|
width: 21em;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
margin-left: -10em;
|
||||||
|
margin-top: -16em;
|
||||||
|
|
||||||
|
.header {
|
||||||
|
text-align: center;
|
||||||
|
font-size: 1em !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
p {
|
||||||
|
font-size: 0.8em;
|
||||||
|
margin-top: 0.3em;
|
||||||
|
margin-bottom: 0;
|
||||||
|
font-weight: normal;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.comment {
|
||||||
|
margin-top: 0.5em;
|
||||||
|
padding: 0.5em;
|
||||||
|
color: gray;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media screen and (max-width: 512px) {
|
||||||
|
form {
|
||||||
|
width: 80%;
|
||||||
|
margin-left: -40%;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user