diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-12-07 15:27:36 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-07 15:27:36 +0800 |
commit | dd30d9d5c0f577cb6e084aae6de2752ad43474d8 (patch) | |
tree | 1e3799a672a23424484b849827ba39eae447856a /models/auth/session.go | |
parent | beb71f5ef6e8074dc744ac995c15f7b5947a3f2e (diff) | |
download | gitea-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/auth/session.go')
-rw-r--r-- | models/auth/session.go | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/models/auth/session.go b/models/auth/session.go index 28f25170ee..60fdeaba7c 100644 --- a/models/auth/session.go +++ b/models/auth/session.go @@ -9,6 +9,8 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/timeutil" + + "xorm.io/builder" ) // Session represents a session compatible for go-chi session @@ -33,34 +35,28 @@ func UpdateSession(ctx context.Context, key string, data []byte) error { // ReadSession reads the data for the provided session func ReadSession(ctx context.Context, key string) (*Session, error) { - session := Session{ - Key: key, - } - ctx, committer, err := db.TxContext(ctx) if err != nil { return nil, err } defer committer.Close() - if has, err := db.GetByBean(ctx, &session); err != nil { + session, exist, err := db.Get[Session](ctx, builder.Eq{"key": key}) + if err != nil { return nil, err - } else if !has { + } else if !exist { session.Expiry = timeutil.TimeStampNow() if err := db.Insert(ctx, &session); err != nil { return nil, err } } - return &session, committer.Commit() + return session, committer.Commit() } // ExistSession checks if a session exists func ExistSession(ctx context.Context, key string) (bool, error) { - session := Session{ - Key: key, - } - return db.GetEngine(ctx).Get(&session) + return db.Exist[Session](ctx, builder.Eq{"key": key}) } // DestroySession destroys a session @@ -79,17 +75,13 @@ func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, er } defer committer.Close() - if has, err := db.GetByBean(ctx, &Session{ - Key: newKey, - }); err != nil { + if has, err := db.Exist[Session](ctx, builder.Eq{"key": newKey}); err != nil { return nil, err } else if has { return nil, fmt.Errorf("session Key: %s already exists", newKey) } - if has, err := db.GetByBean(ctx, &Session{ - Key: oldKey, - }); err != nil { + if has, err := db.Exist[Session](ctx, builder.Eq{"key": oldKey}); err != nil { return nil, err } else if !has { if err := db.Insert(ctx, &Session{ @@ -104,14 +96,13 @@ func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, er return nil, err } - s := Session{ - Key: newKey, - } - if _, err := db.GetByBean(ctx, &s); err != nil { + s, _, err := db.Get[Session](ctx, builder.Eq{"key": newKey}) + if err != nil { + // is not exist, it should be impossible return nil, err } - return &s, committer.Commit() + return s, committer.Commit() } // CountSessions returns the number of sessions |