59 lines
2.2 KiB
Go
59 lines
2.2 KiB
Go
// Copyright 2026 tamakyi. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package user
|
|
|
|
import (
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/tamakyi/TamaBox/internal/context"
|
|
"github.com/tamakyi/TamaBox/internal/db"
|
|
"github.com/tamakyi/TamaBox/internal/form"
|
|
"github.com/tamakyi/TamaBox/internal/i18n"
|
|
)
|
|
|
|
// UpdatePagePassword 更新页面访问密码
|
|
func UpdatePagePassword(ctx context.Context, f form.UpdatePagePassword) {
|
|
// 反序列化失败(如请求体超限)时零值 Password="" 会把密码静默解除——先中止,
|
|
// 不触碰现有密码。govalid 校验失败(如密码超长)同样在此拦截,避免绕过前端限制。
|
|
// 该表单已随「内容防护」聚合迁入 security 面板,回跳地址跟随表单携带的 ?tab=。
|
|
if abortOnFormError(ctx, returnTab(ctx)) {
|
|
return
|
|
}
|
|
|
|
if err := db.Users.UpdatePagePassword(ctx.Request().Context(), ctx.User.ID, f.Password); err != nil {
|
|
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to update page password")
|
|
ctx.SetInternalErrorFlash()
|
|
} else {
|
|
if f.Password == "" {
|
|
ctx.SetSuccessFlash(i18n.T(ctx.Lang(), "admin.flash.pagePwdOff"))
|
|
} else {
|
|
ctx.SetSuccessFlash(i18n.T(ctx.Lang(), "admin.flash.pagePwdUpdated"))
|
|
}
|
|
}
|
|
ctx.Redirect(returnTab(ctx))
|
|
}
|
|
|
|
// UpdateAskPassword 更新提问密码
|
|
func UpdateAskPassword(ctx context.Context, f form.UpdateAskPassword) {
|
|
// 同上:反序列化失败或校验失败时零值 Password="" 会把提问密码静默解除。
|
|
// 回跳地址跟随表单携带的 ?tab=(该表单已迁入「内容防护」security 面板)。
|
|
if abortOnFormError(ctx, returnTab(ctx)) {
|
|
return
|
|
}
|
|
|
|
if err := db.Users.UpdateAskPassword(ctx.Request().Context(), ctx.User.ID, f.Password); err != nil {
|
|
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to update ask password")
|
|
ctx.SetInternalErrorFlash()
|
|
} else {
|
|
if f.Password == "" {
|
|
ctx.SetSuccessFlash(i18n.T(ctx.Lang(), "admin.flash.askPwdOff"))
|
|
} else {
|
|
ctx.SetSuccessFlash(i18n.T(ctx.Lang(), "admin.flash.askPwdUpdated"))
|
|
}
|
|
}
|
|
// 保存后回到提交时所在的面板(内容防护面板)
|
|
ctx.Redirect(returnTab(ctx))
|
|
}
|