feat: multi user template

This commit is contained in:
naiba
2024-12-06 23:19:28 +08:00
parent 8755b65ac5
commit 9b0697491d
7 changed files with 62 additions and 14 deletions

View File

@@ -249,11 +249,11 @@ func fallbackToFrontend(adminFrontend, userFrontend fs.FS) func(*gin.Context) {
}
return
}
localFilePath := path.Join("user-dist", c.Request.URL.Path)
localFilePath := path.Join(singleton.Conf.UserTemplate, c.Request.URL.Path)
if checkLocalFileOrFs(c, userFrontend, localFilePath) {
return
}
if !checkLocalFileOrFs(c, userFrontend, "user-dist/index.html") {
if !checkLocalFileOrFs(c, userFrontend, singleton.Conf.UserTemplate+"/index.html") {
c.JSON(http.StatusOK, newErrorResponse(errors.New("404 Not Found")))
}
}

View File

@@ -1,6 +1,8 @@
package controller
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/nezhahq/nezha/model"
@@ -21,8 +23,9 @@ func listConfig(c *gin.Context) (model.SettingResponse, error) {
authorized := isMember // TODO || isViewPasswordVerfied
conf := model.SettingResponse{
Config: *singleton.Conf,
Version: singleton.Version,
Config: *singleton.Conf,
Version: singleton.Version,
UserTemplates: singleton.UserTemplates,
}
if !authorized {
@@ -55,7 +58,16 @@ func updateConfig(c *gin.Context) (any, error) {
if err := c.ShouldBindJSON(&sf); err != nil {
return nil, err
}
var userTemplateValid bool
for _, v := range singleton.UserTemplates {
if v.Path == sf.UserTemplate {
userTemplateValid = true
break
}
}
if !userTemplateValid {
return nil, errors.New("invalid user template")
}
singleton.Conf.Language = sf.Language
singleton.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification
singleton.Conf.EnablePlainIPInNotification = sf.EnablePlainIPInNotification
@@ -69,6 +81,7 @@ func updateConfig(c *gin.Context) (any, error) {
singleton.Conf.CustomCodeDashboard = sf.CustomCodeDashboard
singleton.Conf.RealIPHeader = sf.RealIPHeader
singleton.Conf.TLS = sf.TLS
singleton.Conf.UserTemplate = sf.UserTemplate
if err := singleton.Conf.Save(); err != nil {
return nil, newGormError("%v", err)

View File

@@ -82,7 +82,7 @@ func updateProfile(c *gin.Context) (any, error) {
// @Router /user [get]
func listUser(c *gin.Context) ([]model.User, error) {
var users []model.User
if err := singleton.DB.Find(&users).Error; err != nil {
if err := singleton.DB.Omit("password").Find(&users).Error; err != nil {
return nil, err
}
return users, nil