perms.go 4.0 KB

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