store.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. }
  22. type store struct{}
  23. // NewStore returns a new Store using the global database handle.
  24. func NewStore() Store {
  25. return &store{}
  26. }
  27. func (*store) GetAccessTokenBySHA1(ctx context.Context, sha1 string) (*database.AccessToken, error) {
  28. return database.Handle.AccessTokens().GetBySHA1(ctx, sha1)
  29. }
  30. func (*store) TouchAccessTokenByID(ctx context.Context, id int64) error {
  31. return database.Handle.AccessTokens().Touch(ctx, id)
  32. }
  33. func (*store) GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*database.Repository, error) {
  34. return database.Handle.Repositories().GetByName(ctx, ownerID, name)
  35. }
  36. func (*store) IsTwoFactorEnabled(ctx context.Context, userID int64) bool {
  37. return database.Handle.TwoFactors().IsEnabled(ctx, userID)
  38. }