mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-03 17:48:45 +00:00
cf89ecd887
The `gitea.com/go-chi/session` package only exists for Gitea, so it moves into `modules/session` to fix its bugs directly. Fixes the flake in https://github.com/go-gitea/gitea/actions/runs/36726154500/job/109923538400. - Sessions are only written back when changed, so a read-only request can't revert a concurrent change or restore a logged-out session, like https://github.com/go-macaron/session/commit/ae808a4a4660c802965c834299ab08f167effd12 - The session cookie is only set once a session holds data - Every backend refreshes the expiry on load and file sessions are written atomically - Also fix https://github.com/go-gitea/gitea/issues/36176 ## ⚠️ BREAKING ⚠️ * the `mysql`, `postgres`, `couchbase` and `memcache` session providers are removed, use `file`, `db` or `redis` instead * login-related cookies are renamed to `gitea_session` and `gitea_remember`, if you'd like to use the old names, set `COOKIE_NAME` and `COOKIE_REMEMBER_NAME` in app.ini --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
162 lines
4.6 KiB
Go
162 lines
4.6 KiB
Go
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package admin
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
system_model "gitea.dev/models/system"
|
|
"gitea.dev/modules/cache"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/json"
|
|
"gitea.dev/modules/log"
|
|
"gitea.dev/modules/setting"
|
|
"gitea.dev/modules/setting/config"
|
|
"gitea.dev/modules/templates"
|
|
"gitea.dev/modules/util"
|
|
"gitea.dev/services/context"
|
|
"gitea.dev/services/mailer"
|
|
)
|
|
|
|
const (
|
|
tplConfig templates.TplName = "admin/config"
|
|
tplConfigSettings templates.TplName = "admin/config_settings/config_settings"
|
|
)
|
|
|
|
// SendTestMail send test mail to confirm mail service is OK
|
|
func SendTestMail(ctx *context.Context) {
|
|
email := ctx.FormString("email")
|
|
// Send a test email to the user's email address and redirect back to Config
|
|
if err := mailer.SendTestMail(email); err != nil {
|
|
ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err))
|
|
} else {
|
|
ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email))
|
|
}
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/-/admin/config")
|
|
}
|
|
|
|
// TestCache test the cache settings
|
|
func TestCache(ctx *context.Context) {
|
|
elapsed, err := cache.Test()
|
|
if err != nil {
|
|
ctx.Flash.Error(ctx.Tr("admin.config.cache_test_failed", err))
|
|
} else {
|
|
if elapsed > cache.SlowCacheThreshold {
|
|
ctx.Flash.Warning(ctx.Tr("admin.config.cache_test_slow", elapsed))
|
|
} else {
|
|
ctx.Flash.Info(ctx.Tr("admin.config.cache_test_succeeded", elapsed))
|
|
}
|
|
}
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/-/admin/config")
|
|
}
|
|
|
|
// Config show admin config page
|
|
func Config(ctx *context.Context) {
|
|
ctx.Data["Title"] = ctx.Tr("admin.config_summary")
|
|
ctx.Data["PageIsAdminConfig"] = true
|
|
ctx.Data["PageIsAdminConfigSummary"] = true
|
|
|
|
ctx.Data["CustomConf"] = setting.CustomConf
|
|
ctx.Data["AppBuiltWith"] = setting.AppBuiltWith
|
|
ctx.Data["RunUser"] = setting.RunUser
|
|
ctx.Data["RunMode"] = util.ToTitleCase(setting.RunMode)
|
|
ctx.Data["GitVersion"] = git.DefaultFeatures().VersionInfo()
|
|
|
|
ctx.Data["AppDataPath"] = setting.AppDataPath
|
|
ctx.Data["RepoRootPath"] = setting.RepoRootPath
|
|
ctx.Data["CustomRootPath"] = setting.CustomPath
|
|
ctx.Data["LogRootPath"] = setting.Log.RootPath
|
|
ctx.Data["ScriptType"] = setting.ScriptType
|
|
ctx.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
|
|
ctx.Data["ReverseProxyAuthEmail"] = setting.ReverseProxyAuthEmail
|
|
|
|
ctx.Data["SSH"] = setting.SSH
|
|
ctx.Data["LFS"] = setting.LFS
|
|
|
|
ctx.Data["Service"] = setting.Service
|
|
ctx.Data["DbCfg"] = setting.Database
|
|
ctx.Data["Webhook"] = setting.Webhook
|
|
ctx.Data["MailerEnabled"] = false
|
|
if setting.MailService != nil {
|
|
ctx.Data["MailerEnabled"] = true
|
|
ctx.Data["Mailer"] = setting.MailService
|
|
}
|
|
|
|
ctx.Data["CacheAdapter"] = setting.CacheService.Adapter
|
|
ctx.Data["CacheInterval"] = setting.CacheService.Interval
|
|
ctx.Data["CacheItemTTL"] = setting.CacheService.TTL
|
|
|
|
sessionCfg := setting.SessionConfig
|
|
sessionCfg.ProviderConfig = ""
|
|
ctx.Data["SessionConfig"] = sessionCfg
|
|
|
|
ctx.Data["Git"] = setting.Git
|
|
ctx.Data["AccessLogTemplate"] = setting.Log.AccessLogTemplate
|
|
ctx.Data["LogSQL"] = setting.Database.LogSQL
|
|
|
|
ctx.Data["Loggers"] = log.GetManager().DumpLoggers()
|
|
config.GetDynGetter().InvalidateCache()
|
|
prepareStartupProblemsAlert(ctx)
|
|
|
|
ctx.HTML(http.StatusOK, tplConfig)
|
|
}
|
|
|
|
func ConfigSettings(ctx *context.Context) {
|
|
ctx.Data["Title"] = ctx.Tr("admin.config_settings")
|
|
ctx.Data["PageIsAdminConfig"] = true
|
|
ctx.Data["PageIsAdminConfigSettings"] = true
|
|
ctx.HTML(http.StatusOK, tplConfigSettings)
|
|
}
|
|
|
|
func validateConfigKeyValue(dynKey, input string) error {
|
|
opt := config.GetConfigOption(dynKey)
|
|
if opt == nil {
|
|
return util.NewInvalidArgumentErrorf("unknown config key: %s", dynKey)
|
|
}
|
|
|
|
const limit = 64 * 1024
|
|
if len(input) > limit {
|
|
return util.NewInvalidArgumentErrorf("value length exceeds limit of %d", limit)
|
|
}
|
|
|
|
if !json.Valid([]byte(input)) {
|
|
return util.NewInvalidArgumentErrorf("invalid json value for key: %s", dynKey)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ChangeConfig(ctx *context.Context) {
|
|
_ = ctx.Req.ParseForm()
|
|
configKeys := ctx.Req.Form["key"]
|
|
configValues := ctx.Req.Form["value"]
|
|
configSettings := map[string]string{}
|
|
loop:
|
|
for i, key := range configKeys {
|
|
if i >= len(configValues) {
|
|
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
|
|
break loop
|
|
}
|
|
value := configValues[i]
|
|
|
|
err := validateConfigKeyValue(key, value)
|
|
if err != nil {
|
|
ctx.JSONErrorAuto(err)
|
|
break loop
|
|
}
|
|
configSettings[key] = value
|
|
}
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
if err := system_model.SetSettings(ctx, configSettings); err != nil {
|
|
ctx.ServerError("SetSettings", err)
|
|
return
|
|
}
|
|
config.GetDynGetter().InvalidateCache()
|
|
ctx.JSONOK()
|
|
}
|