1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-03 09:07:19 +00:00

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})
```
This commit is contained in:
Lunny Xiao
2023-12-07 15:27:36 +08:00
committed by GitHub
parent beb71f5ef6
commit dd30d9d5c0
28 changed files with 189 additions and 174 deletions

View File

@ -10,6 +10,8 @@ import (
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
)
// IssueAssignees saves all issue assignees
@ -59,7 +61,7 @@ func GetAssigneeIDsByIssue(ctx context.Context, issueID int64) ([]int64, error)
// IsUserAssignedToIssue returns true when the user is assigned to the issue
func IsUserAssignedToIssue(ctx context.Context, issue *Issue, user *user_model.User) (isAssigned bool, err error) {
return db.GetByBean(ctx, &IssueAssignees{IssueID: issue.ID, AssigneeID: user.ID})
return db.Exist[IssueAssignees](ctx, builder.Eq{"assignee_id": user.ID, "issue_id": issue.ID})
}
// ToggleIssueAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.

View File

@ -304,15 +304,11 @@ func GetLabelInRepoByName(ctx context.Context, repoID int64, labelName string) (
return nil, ErrRepoLabelNotExist{0, repoID}
}
l := &Label{
Name: labelName,
RepoID: repoID,
}
has, err := db.GetByBean(ctx, l)
l, exist, err := db.Get[Label](ctx, builder.Eq{"name": labelName, "repo_id": repoID})
if err != nil {
return nil, err
} else if !has {
return nil, ErrRepoLabelNotExist{0, l.RepoID}
} else if !exist {
return nil, ErrRepoLabelNotExist{0, repoID}
}
return l, nil
}
@ -323,15 +319,11 @@ func GetLabelInRepoByID(ctx context.Context, repoID, labelID int64) (*Label, err
return nil, ErrRepoLabelNotExist{labelID, repoID}
}
l := &Label{
ID: labelID,
RepoID: repoID,
}
has, err := db.GetByBean(ctx, l)
l, exist, err := db.Get[Label](ctx, builder.Eq{"id": labelID, "repo_id": repoID})
if err != nil {
return nil, err
} else if !has {
return nil, ErrRepoLabelNotExist{l.ID, l.RepoID}
} else if !exist {
return nil, ErrRepoLabelNotExist{labelID, repoID}
}
return l, nil
}
@ -408,15 +400,11 @@ func GetLabelInOrgByName(ctx context.Context, orgID int64, labelName string) (*L
return nil, ErrOrgLabelNotExist{0, orgID}
}
l := &Label{
Name: labelName,
OrgID: orgID,
}
has, err := db.GetByBean(ctx, l)
l, exist, err := db.Get[Label](ctx, builder.Eq{"name": labelName, "org_id": orgID})
if err != nil {
return nil, err
} else if !has {
return nil, ErrOrgLabelNotExist{0, l.OrgID}
} else if !exist {
return nil, ErrOrgLabelNotExist{0, orgID}
}
return l, nil
}
@ -427,15 +415,11 @@ func GetLabelInOrgByID(ctx context.Context, orgID, labelID int64) (*Label, error
return nil, ErrOrgLabelNotExist{labelID, orgID}
}
l := &Label{
ID: labelID,
OrgID: orgID,
}
has, err := db.GetByBean(ctx, l)
l, exist, err := db.Get[Label](ctx, builder.Eq{"id": labelID, "org_id": orgID})
if err != nil {
return nil, err
} else if !has {
return nil, ErrOrgLabelNotExist{l.ID, l.OrgID}
} else if !exist {
return nil, ErrOrgLabelNotExist{labelID, orgID}
}
return l, nil
}

View File

@ -660,13 +660,10 @@ func GetPullRequestByIssueIDWithNoAttributes(ctx context.Context, issueID int64)
// GetPullRequestByIssueID returns pull request by given issue ID.
func GetPullRequestByIssueID(ctx context.Context, issueID int64) (*PullRequest, error) {
pr := &PullRequest{
IssueID: issueID,
}
has, err := db.GetByBean(ctx, pr)
pr, exist, err := db.Get[PullRequest](ctx, builder.Eq{"issue_id": issueID})
if err != nil {
return nil, err
} else if !has {
} else if !exist {
return nil, ErrPullRequestNotExist{0, issueID, 0, 0, "", ""}
}
return pr, pr.LoadAttributes(ctx)