perms.go 3.7 KB

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