perms.go 4.0 KB

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