store.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2022 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE and LICENSE.gogs file.
  4. // Copyright 2025 Huan-Gogs Authors. All rights reserved.
  5. // Use of this source code is governed by a MIT-style
  6. // license that can be found in the LICENSE file.
  7. package context
  8. import (
  9. "context"
  10. "github.com/SongZihuan/huan-gogs/internal/database"
  11. )
  12. // Store is the data layer carrier for context middleware. This interface is
  13. // meant to abstract away and limit the exposure of the underlying data layer to
  14. // the handler through a thin-wrapper.
  15. type Store interface {
  16. // GetAccessTokenBySHA1 returns the access token with given SHA1. It returns
  17. // database.ErrAccessTokenNotExist when not found.
  18. GetAccessTokenBySHA1(ctx context.Context, sha1 string) (*database.AccessToken, error)
  19. // TouchAccessTokenByID updates the updated time of the given access token to
  20. // the current time.
  21. TouchAccessTokenByID(ctx context.Context, id int64) error
  22. // GetUserByID returns the user with given ID. It returns
  23. // database.ErrUserNotExist when not found.
  24. GetUserByID(ctx context.Context, id int64) (*database.User, error)
  25. // GetUserByUsername returns the user with given username. It returns
  26. // database.ErrUserNotExist when not found.
  27. GetUserByUsername(ctx context.Context, username string) (*database.User, error)
  28. // CreateUser creates a new user and persists to database. It returns
  29. // database.ErrNameNotAllowed if the given name or pattern of the name is not
  30. // allowed as a username, or database.ErrUserAlreadyExist when a user with same
  31. // name already exists, or database.ErrEmailAlreadyUsed if the email has been
  32. // verified by another user.
  33. CreateUser(ctx context.Context, username, email, publicEmail string, opts database.CreateUserOptions) (*database.User, error)
  34. // AuthenticateUser validates username and password via given login source ID.
  35. // It returns database.ErrUserNotExist when the user was not found.
  36. //
  37. // When the "loginSourceID" is negative, it aborts the process and returns
  38. // database.ErrUserNotExist if the user was not found in the database.
  39. //
  40. // When the "loginSourceID" is non-negative, it returns
  41. // database.ErrLoginSourceMismatch if the user has different login source ID
  42. // than the "loginSourceID".
  43. //
  44. // When the "loginSourceID" is positive, it tries to authenticate via given
  45. // login source and creates a new user when not yet exists in the database.
  46. AuthenticateUser(ctx context.Context, login, password string, loginSourceID int64) (*database.User, error)
  47. }
  48. type store struct{}
  49. // NewStore returns a new Store using the global database handle.
  50. func NewStore() Store {
  51. return &store{}
  52. }
  53. func (*store) GetAccessTokenBySHA1(ctx context.Context, sha1 string) (*database.AccessToken, error) {
  54. return database.Handle.AccessTokens().GetBySHA1(ctx, sha1)
  55. }
  56. func (*store) TouchAccessTokenByID(ctx context.Context, id int64) error {
  57. return database.Handle.AccessTokens().Touch(ctx, id)
  58. }
  59. func (*store) GetUserByID(ctx context.Context, id int64) (*database.User, error) {
  60. return database.Handle.Users().GetByID(ctx, id)
  61. }
  62. func (*store) GetUserByUsername(ctx context.Context, username string) (*database.User, error) {
  63. return database.Handle.Users().GetByUsername(ctx, username)
  64. }
  65. func (*store) CreateUser(ctx context.Context, username, email, publicEmail string, opts database.CreateUserOptions) (*database.User, error) {
  66. return database.Handle.Users().Create(ctx, username, email, opts)
  67. }
  68. func (*store) AuthenticateUser(ctx context.Context, login, password string, loginSourceID int64) (*database.User, error) {
  69. return database.Handle.Users().Authenticate(ctx, login, password, loginSourceID)
  70. }