store.go 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package repo
  2. import (
  3. "context"
  4. "gogs.io/gogs/internal/database"
  5. )
  6. // Store is the data layer carrier for context middleware. This interface is
  7. // meant to abstract away and limit the exposure of the underlying data layer to
  8. // the handler through a thin-wrapper.
  9. type Store interface {
  10. // GetAccessTokenBySHA1 returns the access token with given SHA1. It returns
  11. // database.ErrAccessTokenNotExist when not found.
  12. GetAccessTokenBySHA1(ctx context.Context, sha1 string) (*database.AccessToken, error)
  13. // TouchAccessTokenByID updates the updated time of the given access token to
  14. // the current time.
  15. TouchAccessTokenByID(ctx context.Context, id int64) error
  16. // GetRepositoryByName returns the repository with given owner and name. It
  17. // returns database.ErrRepoNotExist when not found.
  18. GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*database.Repository, error)
  19. // IsTwoFactorEnabled returns true if the user has enabled 2FA.
  20. IsTwoFactorEnabled(ctx context.Context, userID int64) bool
  21. // GetUserByID returns the user with given ID. It returns
  22. // database.ErrUserNotExist when not found.
  23. GetUserByID(ctx context.Context, id int64) (*database.User, error)
  24. // GetUserByUsername returns the user with given username. It returns
  25. // database.ErrUserNotExist when not found.
  26. GetUserByUsername(ctx context.Context, username string) (*database.User, error)
  27. // CreateUser creates a new user and persists to database. It returns
  28. // database.ErrNameNotAllowed if the given name or pattern of the name is not
  29. // allowed as a username, or database.ErrUserAlreadyExist when a user with same
  30. // name already exists, or database.ErrEmailAlreadyUsed if the email has been
  31. // verified by another user.
  32. CreateUser(ctx context.Context, username, email string, opts database.CreateUserOptions) (*database.User, error)
  33. // AuthenticateUser validates username and password via given login source ID.
  34. // It returns database.ErrUserNotExist when the user was not found.
  35. //
  36. // When the "loginSourceID" is negative, it aborts the process and returns
  37. // database.ErrUserNotExist if the user was not found in the database.
  38. //
  39. // When the "loginSourceID" is non-negative, it returns
  40. // database.ErrLoginSourceMismatch if the user has different login source ID
  41. // than the "loginSourceID".
  42. //
  43. // When the "loginSourceID" is positive, it tries to authenticate via given
  44. // login source and creates a new user when not yet exists in the database.
  45. AuthenticateUser(ctx context.Context, login, password string, loginSourceID int64) (*database.User, error)
  46. }
  47. type store struct{}
  48. // NewStore returns a new Store using the global database handle.
  49. func NewStore() Store {
  50. return &store{}
  51. }
  52. func (*store) GetAccessTokenBySHA1(ctx context.Context, sha1 string) (*database.AccessToken, error) {
  53. return database.Handle.AccessTokens().GetBySHA1(ctx, sha1)
  54. }
  55. func (*store) TouchAccessTokenByID(ctx context.Context, id int64) error {
  56. return database.Handle.AccessTokens().Touch(ctx, id)
  57. }
  58. func (*store) GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*database.Repository, error) {
  59. return database.Handle.Repositories().GetByName(ctx, ownerID, name)
  60. }
  61. func (*store) IsTwoFactorEnabled(ctx context.Context, userID int64) bool {
  62. return database.Handle.TwoFactors().IsEnabled(ctx, userID)
  63. }
  64. func (*store) GetUserByID(ctx context.Context, id int64) (*database.User, error) {
  65. return database.Handle.Users().GetByID(ctx, id)
  66. }
  67. func (*store) GetUserByUsername(ctx context.Context, username string) (*database.User, error) {
  68. return database.Handle.Users().GetByUsername(ctx, username)
  69. }
  70. func (*store) CreateUser(ctx context.Context, username, email string, opts database.CreateUserOptions) (*database.User, error) {
  71. return database.Handle.Users().Create(ctx, username, email, opts)
  72. }
  73. func (*store) AuthenticateUser(ctx context.Context, login, password string, loginSourceID int64) (*database.User, error) {
  74. return database.Handle.Users().Authenticate(ctx, login, password, loginSourceID)
  75. }