Files
tamabox/route/user/password.go
T
2026-09-08 10:10:44 +08:00

57 lines
2.1 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 校验失败(如密码超长)同样在此拦截,避免绕过前端限制。
if abortOnFormError(ctx, "/user/profile") {
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("/user/profile")
}
// UpdateAskPassword 更新提问密码
func UpdateAskPassword(ctx context.Context, f form.UpdateAskPassword) {
// 同上:反序列化失败或校验失败时零值 Password="" 会把提问密码静默解除。
if abortOnFormError(ctx, profileURLWithTab(ProfileTabQuestion)) {
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(profileURLWithTab(ProfileTabQuestion))
}