permissions.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2020 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 file.
  4. package database
  5. import (
  6. "context"
  7. "github.com/pkg/errors"
  8. "gorm.io/gorm"
  9. log "unknwon.dev/clog/v2"
  10. )
  11. // Access represents the highest access level of a user has to a repository. The
  12. // only access type that is not in this table is the real owner of a repository.
  13. // In case of an organization repository, the members of the owners team are in
  14. // this table.
  15. type Access struct {
  16. ID int64 `gorm:"primaryKey"`
  17. UserID int64 `xorm:"UNIQUE(s)" gorm:"uniqueIndex:access_user_repo_unique;not null"`
  18. RepoID int64 `xorm:"UNIQUE(s)" gorm:"uniqueIndex:access_user_repo_unique;not null"`
  19. Mode AccessMode `gorm:"not null"`
  20. }
  21. // AccessMode is the access mode of a user has to a repository.
  22. type AccessMode int
  23. const (
  24. AccessModeNone AccessMode = iota // 0
  25. AccessModeRead // 1
  26. AccessModeWrite // 2
  27. AccessModeAdmin // 3
  28. AccessModeOwner // 4
  29. )
  30. func (mode AccessMode) String() string {
  31. switch mode {
  32. case AccessModeRead:
  33. return "read"
  34. case AccessModeWrite:
  35. return "write"
  36. case AccessModeAdmin:
  37. return "admin"
  38. case AccessModeOwner:
  39. return "owner"
  40. default:
  41. return "none"
  42. }
  43. }
  44. // ParseAccessMode returns corresponding access mode to given permission string.
  45. func ParseAccessMode(permission string) AccessMode {
  46. switch permission {
  47. case "write":
  48. return AccessModeWrite
  49. case "admin":
  50. return AccessModeAdmin
  51. default:
  52. return AccessModeRead
  53. }
  54. }
  55. // PermissionsStore is the storage layer for repository permissions.
  56. type PermissionsStore struct {
  57. db *gorm.DB
  58. }
  59. func newPermissionsStore(db *gorm.DB) *PermissionsStore {
  60. return &PermissionsStore{db: db}
  61. }
  62. type AccessModeOptions struct {
  63. OwnerID int64 // The ID of the repository owner.
  64. Private bool // Whether the repository is private.
  65. }
  66. // AccessMode returns the access mode of given user has to the repository.
  67. func (s *PermissionsStore) AccessMode(ctx context.Context, userID, repoID int64, opts AccessModeOptions) (mode AccessMode) {
  68. if repoID <= 0 {
  69. return AccessModeNone
  70. }
  71. // Everyone has read access to public repository.
  72. if !opts.Private {
  73. mode = AccessModeRead
  74. }
  75. // Anonymous user gets the default access.
  76. if userID <= 0 {
  77. return mode
  78. }
  79. if userID == opts.OwnerID {
  80. return AccessModeOwner
  81. }
  82. access := new(Access)
  83. err := s.db.WithContext(ctx).Where("user_id = ? AND repo_id = ?", userID, repoID).First(access).Error
  84. if err != nil {
  85. if !errors.Is(err, gorm.ErrRecordNotFound) {
  86. log.Error("Failed to get access [user_id: %d, repo_id: %d]: %v", userID, repoID, err)
  87. }
  88. return mode
  89. }
  90. return access.Mode
  91. }
  92. // Authorize returns true if the user has as good as desired access mode to the
  93. // repository.
  94. func (s *PermissionsStore) Authorize(ctx context.Context, userID, repoID int64, desired AccessMode, opts AccessModeOptions) bool {
  95. return desired <= s.AccessMode(ctx, userID, repoID, opts)
  96. }
  97. // SetRepoPerms does a full update to which users have which level of access to
  98. // given repository. Keys of the "accessMap" are user IDs.
  99. func (s *PermissionsStore) SetRepoPerms(ctx context.Context, repoID int64, accessMap map[int64]AccessMode) error {
  100. records := make([]*Access, 0, len(accessMap))
  101. for userID, mode := range accessMap {
  102. records = append(records, &Access{
  103. UserID: userID,
  104. RepoID: repoID,
  105. Mode: mode,
  106. })
  107. }
  108. return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
  109. err := tx.Where("repo_id = ?", repoID).Delete(new(Access)).Error
  110. if err != nil {
  111. return err
  112. }
  113. return tx.Create(&records).Error
  114. })
  115. }