store.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package lfs
  2. import (
  3. "context"
  4. "gogs.io/gogs/internal/database"
  5. "gogs.io/gogs/internal/lfsutil"
  6. )
  7. // Store is the data layer carrier for LFS endpoints. This interface is meant to
  8. // abstract away and limit the exposure of the underlying data layer to the
  9. // handler through a thin-wrapper.
  10. type Store interface {
  11. // GetAccessTokenBySHA1 returns the access token with given SHA1. It returns
  12. // database.ErrAccessTokenNotExist when not found.
  13. GetAccessTokenBySHA1(ctx context.Context, sha1 string) (*database.AccessToken, error)
  14. // TouchAccessTokenByID updates the updated time of the given access token to
  15. // the current time.
  16. TouchAccessTokenByID(ctx context.Context, id int64) error
  17. // CreateLFSObject creates an LFS object record in database.
  18. CreateLFSObject(ctx context.Context, repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error
  19. // GetLFSObjectByOID returns the LFS object with given OID. It returns
  20. // database.ErrLFSObjectNotExist when not found.
  21. GetLFSObjectByOID(ctx context.Context, repoID int64, oid lfsutil.OID) (*database.LFSObject, error)
  22. // GetLFSObjectsByOIDs returns LFS objects found within "oids". The returned
  23. // list could have fewer elements if some oids were not found.
  24. GetLFSObjectsByOIDs(ctx context.Context, repoID int64, oids ...lfsutil.OID) ([]*database.LFSObject, error)
  25. // AuthorizeRepositoryAccess returns true if the user has as good as desired
  26. // access mode to the repository.
  27. AuthorizeRepositoryAccess(ctx context.Context, userID, repoID int64, desired database.AccessMode, opts database.AccessModeOptions) bool
  28. // GetRepositoryByName returns the repository with given owner and name. It
  29. // returns database.ErrRepoNotExist when not found.
  30. GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*database.Repository, error)
  31. // IsTwoFactorEnabled returns true if the user has enabled 2FA.
  32. IsTwoFactorEnabled(ctx context.Context, userID int64) bool
  33. // GetUserByID returns the user with given ID. It returns
  34. // database.ErrUserNotExist when not found.
  35. GetUserByID(ctx context.Context, id int64) (*database.User, error)
  36. // GetUserByUsername returns the user with given username. It returns
  37. // database.ErrUserNotExist when not found.
  38. GetUserByUsername(ctx context.Context, username string) (*database.User, error)
  39. // CreateUser creates a new user and persists to database. It returns
  40. // database.ErrNameNotAllowed if the given name or pattern of the name is not
  41. // allowed as a username, or database.ErrUserAlreadyExist when a user with same
  42. // name already exists, or database.ErrEmailAlreadyUsed if the email has been
  43. // verified by another user.
  44. CreateUser(ctx context.Context, username, email string, opts database.CreateUserOptions) (*database.User, error)
  45. // AuthenticateUser validates username and password via given login source ID.
  46. // It returns database.ErrUserNotExist when the user was not found.
  47. //
  48. // When the "loginSourceID" is negative, it aborts the process and returns
  49. // database.ErrUserNotExist if the user was not found in the database.
  50. //
  51. // When the "loginSourceID" is non-negative, it returns
  52. // database.ErrLoginSourceMismatch if the user has different login source ID
  53. // than the "loginSourceID".
  54. //
  55. // When the "loginSourceID" is positive, it tries to authenticate via given
  56. // login source and creates a new user when not yet exists in the database.
  57. AuthenticateUser(ctx context.Context, login, password string, loginSourceID int64) (*database.User, error)
  58. }
  59. type store struct{}
  60. // NewStore returns a new Store using the global database handle.
  61. func NewStore() Store {
  62. return &store{}
  63. }
  64. func (*store) GetAccessTokenBySHA1(ctx context.Context, sha1 string) (*database.AccessToken, error) {
  65. return database.Handle.AccessTokens().GetBySHA1(ctx, sha1)
  66. }
  67. func (*store) TouchAccessTokenByID(ctx context.Context, id int64) error {
  68. return database.Handle.AccessTokens().Touch(ctx, id)
  69. }
  70. func (*store) CreateLFSObject(ctx context.Context, repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error {
  71. return database.Handle.LFS().CreateObject(ctx, repoID, oid, size, storage)
  72. }
  73. func (*store) GetLFSObjectByOID(ctx context.Context, repoID int64, oid lfsutil.OID) (*database.LFSObject, error) {
  74. return database.Handle.LFS().GetObjectByOID(ctx, repoID, oid)
  75. }
  76. func (*store) GetLFSObjectsByOIDs(ctx context.Context, repoID int64, oids ...lfsutil.OID) ([]*database.LFSObject, error) {
  77. return database.Handle.LFS().GetObjectsByOIDs(ctx, repoID, oids...)
  78. }
  79. func (*store) AuthorizeRepositoryAccess(ctx context.Context, userID, repoID int64, desired database.AccessMode, opts database.AccessModeOptions) bool {
  80. return database.Handle.Permissions().Authorize(ctx, userID, repoID, desired, opts)
  81. }
  82. func (*store) GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*database.Repository, error) {
  83. return database.Handle.Repositories().GetByName(ctx, ownerID, name)
  84. }
  85. func (*store) IsTwoFactorEnabled(ctx context.Context, userID int64) bool {
  86. return database.Handle.TwoFactors().IsEnabled(ctx, userID)
  87. }
  88. func (*store) GetUserByID(ctx context.Context, id int64) (*database.User, error) {
  89. return database.Handle.Users().GetByID(ctx, id)
  90. }
  91. func (*store) GetUserByUsername(ctx context.Context, username string) (*database.User, error) {
  92. return database.Handle.Users().GetByUsername(ctx, username)
  93. }
  94. func (*store) CreateUser(ctx context.Context, username, email string, opts database.CreateUserOptions) (*database.User, error) {
  95. return database.Handle.Users().Create(ctx, username, email, opts)
  96. }
  97. func (*store) AuthenticateUser(ctx context.Context, login, password string, loginSourceID int64) (*database.User, error) {
  98. return database.Handle.Users().Authenticate(ctx, login, password, loginSourceID)
  99. }