perms.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. type AccessModeOptions struct {
  73. OwnerID int64 // The ID of the repository owner.
  74. Private bool // Whether the repository is private.
  75. }
  76. func (db *perms) AccessMode(ctx context.Context, userID, repoID int64, opts AccessModeOptions) (mode AccessMode) {
  77. if repoID <= 0 {
  78. return AccessModeNone
  79. }
  80. // Everyone has read access to public repository.
  81. if !opts.Private {
  82. mode = AccessModeRead
  83. }
  84. // Anonymous user gets the default access.
  85. if userID <= 0 {
  86. return mode
  87. }
  88. if userID == opts.OwnerID {
  89. return AccessModeOwner
  90. }
  91. access := new(Access)
  92. err := db.WithContext(ctx).Where("user_id = ? AND repo_id = ?", userID, repoID).First(access).Error
  93. if err != nil {
  94. if err != gorm.ErrRecordNotFound {
  95. log.Error("Failed to get access [user_id: %d, repo_id: %d]: %v", userID, repoID, err)
  96. }
  97. return mode
  98. }
  99. return access.Mode
  100. }
  101. func (db *perms) Authorize(ctx context.Context, userID, repoID int64, desired AccessMode, opts AccessModeOptions) bool {
  102. return desired <= db.AccessMode(ctx, userID, repoID, opts)
  103. }
  104. func (db *perms) SetRepoPerms(ctx context.Context, repoID int64, accessMap map[int64]AccessMode) error {
  105. records := make([]*Access, 0, len(accessMap))
  106. for userID, mode := range accessMap {
  107. records = append(records, &Access{
  108. UserID: userID,
  109. RepoID: repoID,
  110. Mode: mode,
  111. })
  112. }
  113. return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
  114. err := tx.Where("repo_id = ?", repoID).Delete(new(Access)).Error
  115. if err != nil {
  116. return err
  117. }
  118. return tx.Create(&records).Error
  119. })
  120. }