aboutsummaryrefslogtreecommitdiffstats
path: root/models/system
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-12-07 15:27:36 +0800
committerGitHub <noreply@github.com>2023-12-07 15:27:36 +0800
commitdd30d9d5c0f577cb6e084aae6de2752ad43474d8 (patch)
tree1e3799a672a23424484b849827ba39eae447856a /models/system
parentbeb71f5ef6e8074dc744ac995c15f7b5947a3f2e (diff)
downloadgitea-dd30d9d5c0f577cb6e084aae6de2752ad43474d8.tar.gz
gitea-dd30d9d5c0f577cb6e084aae6de2752ad43474d8.zip
Remove GetByBean method because sometimes it's danger when query condition parameter is zero and also introduce new generic methods (#28220)
The function `GetByBean` has an obvious defect that when the fields are empty values, it will be ignored. Then users will get a wrong result which is possibly used to make a security problem. To avoid the possibility, this PR removed function `GetByBean` and all references. And some new generic functions have been introduced to be used. The recommand usage like below. ```go // if query an object according id obj, err := db.GetByID[Object](ctx, id) // query with other conditions obj, err := db.Get[Object](ctx, builder.Eq{"a": a, "b":b}) ```
Diffstat (limited to 'models/system')
-rw-r--r--models/system/setting.go11
-rw-r--r--models/system/setting_test.go11
2 files changed, 11 insertions, 11 deletions
diff --git a/models/system/setting.go b/models/system/setting.go
index cd1d373b98..4472b4c228 100644
--- a/models/system/setting.go
+++ b/models/system/setting.go
@@ -13,6 +13,8 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting/config"
"code.gitea.io/gitea/modules/timeutil"
+
+ "xorm.io/builder"
)
type Setting struct {
@@ -36,16 +38,17 @@ func init() {
const keyRevision = "revision"
func GetRevision(ctx context.Context) int {
- revision := &Setting{SettingKey: keyRevision}
- if has, err := db.GetByBean(ctx, revision); err != nil {
+ revision, exist, err := db.Get[Setting](ctx, builder.Eq{"setting_key": keyRevision})
+ if err != nil {
return 0
- } else if !has {
+ } else if !exist {
err = db.Insert(ctx, &Setting{SettingKey: keyRevision, Version: 1})
if err != nil {
return 0
}
return 1
- } else if revision.Version <= 0 || revision.Version >= math.MaxInt-1 {
+ }
+ if revision.Version <= 0 || revision.Version >= math.MaxInt-1 {
_, err = db.Exec(ctx, "UPDATE system_setting SET version=1 WHERE setting_key=?", keyRevision)
if err != nil {
return 0
diff --git a/models/system/setting_test.go b/models/system/setting_test.go
index 0316ea144d..8f04412fb4 100644
--- a/models/system/setting_test.go
+++ b/models/system/setting_test.go
@@ -41,14 +41,11 @@ func TestSettings(t *testing.T) {
assert.EqualValues(t, "false", settings[keyName])
// setting the same value should not trigger DuplicateKey error, and the "version" should be increased
- setting := &system.Setting{SettingKey: keyName}
- _, err = db.GetByBean(db.DefaultContext, setting)
- assert.NoError(t, err)
- assert.EqualValues(t, 2, setting.Version)
err = system.SetSettings(db.DefaultContext, map[string]string{keyName: "false"})
assert.NoError(t, err)
- setting = &system.Setting{SettingKey: keyName}
- _, err = db.GetByBean(db.DefaultContext, setting)
+
+ rev, settings, err = system.GetAllSettings(db.DefaultContext)
assert.NoError(t, err)
- assert.EqualValues(t, 3, setting.Version)
+ assert.Len(t, settings, 2)
+ assert.EqualValues(t, 4, rev)
}