nezha/cmd/dashboard/controller/setting.go

114 lines
3.1 KiB
Go
Raw Normal View History

2024-10-27 01:10:07 -04:00
package controller
import (
2024-12-06 10:19:28 -05:00
"errors"
"strings"
2024-12-06 10:19:28 -05:00
2024-10-27 01:10:07 -04:00
"github.com/gin-gonic/gin"
2024-11-28 06:38:54 -05:00
"github.com/nezhahq/nezha/model"
"github.com/nezhahq/nezha/service/singleton"
2024-10-27 01:10:07 -04:00
)
// List settings
// @Summary List settings
// @Schemes
// @Description List settings
// @Security BearerAuth
// @Tags common
// @Produce json
// @Success 200 {object} model.CommonResponse[model.SettingResponse]
2024-10-27 01:10:07 -04:00
// @Router /setting [get]
func listConfig(c *gin.Context) (*model.SettingResponse, error) {
2024-12-22 03:17:13 -05:00
u, authorized := c.Get(model.CtxKeyAuthorizedUser)
var isAdmin bool
if authorized {
user := u.(*model.User)
2024-12-22 03:17:13 -05:00
isAdmin = user.Role == model.RoleAdmin
}
2024-10-27 01:10:07 -04:00
2024-12-28 10:50:59 -05:00
config := *singleton.Conf
config.Language = strings.Replace(config.Language, "_", "-", -1)
conf := model.SettingResponse{
Config: model.Setting{
ConfigForGuests: config.ConfigForGuests,
ConfigDashboard: config.ConfigDashboard,
IgnoredIPNotificationServerIDs: config.IgnoredIPNotificationServerIDs,
Oauth2Providers: config.Oauth2Providers,
},
Version: singleton.Version,
FrontendTemplates: singleton.FrontendTemplates,
2024-12-04 07:00:18 -05:00
}
2024-12-22 03:17:13 -05:00
if !authorized || !isAdmin {
configForGuests := config.ConfigForGuests
2024-12-28 10:50:59 -05:00
if authorized {
configForGuests.AgentTLS = singleton.Conf.AgentTLS
2025-01-06 08:07:32 -05:00
configForGuests.InstallHost = singleton.Conf.InstallHost
2024-12-28 10:50:59 -05:00
}
conf = model.SettingResponse{
Config: model.Setting{
ConfigForGuests: configForGuests,
Oauth2Providers: config.Oauth2Providers,
},
2024-10-27 01:10:07 -04:00
}
}
return &conf, nil
2024-10-27 01:10:07 -04:00
}
// Edit config
// @Summary Edit config
// @Security BearerAuth
// @Schemes
// @Description Edit config
2024-12-21 12:08:07 -05:00
// @Tags admin required
2024-10-27 01:10:07 -04:00
// @Accept json
// @Param body body model.SettingForm true "SettingForm"
// @Produce json
// @Success 200 {object} model.CommonResponse[any]
// @Router /setting [patch]
func updateConfig(c *gin.Context) (any, error) {
var sf model.SettingForm
if err := c.ShouldBindJSON(&sf); err != nil {
return nil, err
}
2024-12-06 10:19:28 -05:00
var userTemplateValid bool
for _, v := range singleton.FrontendTemplates {
if !userTemplateValid && v.Path == sf.UserTemplate && !v.IsAdmin {
2024-12-06 10:19:28 -05:00
userTemplateValid = true
}
if userTemplateValid {
2024-12-06 10:19:28 -05:00
break
}
}
if !userTemplateValid {
return nil, errors.New("invalid user template")
}
singleton.Conf.Language = strings.Replace(sf.Language, "-", "_", -1)
2024-10-27 01:10:07 -04:00
singleton.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification
singleton.Conf.EnablePlainIPInNotification = sf.EnablePlainIPInNotification
singleton.Conf.Cover = sf.Cover
singleton.Conf.InstallHost = sf.InstallHost
singleton.Conf.IgnoredIPNotification = sf.IgnoredIPNotification
singleton.Conf.IPChangeNotificationGroupID = sf.IPChangeNotificationGroupID
singleton.Conf.SiteName = sf.SiteName
singleton.Conf.DNSServers = sf.DNSServers
2024-10-27 01:10:07 -04:00
singleton.Conf.CustomCode = sf.CustomCode
singleton.Conf.CustomCodeDashboard = sf.CustomCodeDashboard
2024-11-23 03:36:16 -05:00
singleton.Conf.RealIPHeader = sf.RealIPHeader
singleton.Conf.AgentTLS = sf.AgentTLS
2024-12-06 10:19:28 -05:00
singleton.Conf.UserTemplate = sf.UserTemplate
2024-10-27 01:10:07 -04:00
if err := singleton.Conf.Save(); err != nil {
2024-10-31 17:07:04 -04:00
return nil, newGormError("%v", err)
2024-10-27 01:10:07 -04:00
}
singleton.OnNameserverUpdate()
2024-10-31 17:07:04 -04:00
singleton.OnUpdateLang(singleton.Conf.Language)
2024-10-27 01:10:07 -04:00
return nil, nil
}