store.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package context
  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. // GetUserByID returns the user with given ID. It returns
  17. // database.ErrUserNotExist when not found.
  18. GetUserByID(ctx context.Context, id int64) (*database.User, error)
  19. // GetUserByUsername returns the user with given username. It returns
  20. // database.ErrUserNotExist when not found.
  21. GetUserByUsername(ctx context.Context, username string) (*database.User, error)
  22. // CreateUser creates a new user and persists to database. It returns
  23. // database.ErrNameNotAllowed if the given name or pattern of the name is not
  24. // allowed as a username, or database.ErrUserAlreadyExist when a user with same
  25. // name already exists, or database.ErrEmailAlreadyUsed if the email has been
  26. // verified by another user.
  27. CreateUser(ctx context.Context, username, email string, opts database.CreateUserOptions) (*database.User, error)
  28. // AuthenticateUser validates username and password via given login source ID.
  29. // It returns database.ErrUserNotExist when the user was not found.
  30. //
  31. // When the "loginSourceID" is negative, it aborts the process and returns
  32. // database.ErrUserNotExist if the user was not found in the database.
  33. //
  34. // When the "loginSourceID" is non-negative, it returns
  35. // database.ErrLoginSourceMismatch if the user has different login source ID
  36. // than the "loginSourceID".
  37. //
  38. // When the "loginSourceID" is positive, it tries to authenticate via given
  39. // login source and creates a new user when not yet exists in the database.
  40. AuthenticateUser(ctx context.Context, login, password string, loginSourceID int64) (*database.User, error)
  41. }
  42. type store struct{}
  43. // NewStore returns a new Store using the global database handle.
  44. func NewStore() Store {
  45. return &store{}
  46. }
  47. func (*store) GetAccessTokenBySHA1(ctx context.Context, sha1 string) (*database.AccessToken, error) {
  48. return database.Handle.AccessTokens().GetBySHA1(ctx, sha1)
  49. }
  50. func (*store) TouchAccessTokenByID(ctx context.Context, id int64) error {
  51. return database.Handle.AccessTokens().Touch(ctx, id)
  52. }
  53. func (*store) GetUserByID(ctx context.Context, id int64) (*database.User, error) {
  54. return database.Handle.Users().GetByID(ctx, id)
  55. }
  56. func (*store) GetUserByUsername(ctx context.Context, username string) (*database.User, error) {
  57. return database.Handle.Users().GetByUsername(ctx, username)
  58. }
  59. func (*store) CreateUser(ctx context.Context, username, email string, opts database.CreateUserOptions) (*database.User, error) {
  60. return database.Handle.Users().Create(ctx, username, email, opts)
  61. }
  62. func (*store) AuthenticateUser(ctx context.Context, login, password string, loginSourceID int64) (*database.User, error) {
  63. return database.Handle.Users().Authenticate(ctx, login, password, loginSourceID)
  64. }