You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
8.7 KiB

Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. // AccessMode specifies the users access mode
  11. type AccessMode int
  12. const (
  13. // AccessModeNone no access
  14. AccessModeNone AccessMode = iota // 0
  15. // AccessModeRead read access
  16. AccessModeRead // 1
  17. // AccessModeWrite write access
  18. AccessModeWrite // 2
  19. // AccessModeAdmin admin access
  20. AccessModeAdmin // 3
  21. // AccessModeOwner owner access
  22. AccessModeOwner // 4
  23. )
  24. func (mode AccessMode) String() string {
  25. switch mode {
  26. case AccessModeRead:
  27. return "read"
  28. case AccessModeWrite:
  29. return "write"
  30. case AccessModeAdmin:
  31. return "admin"
  32. case AccessModeOwner:
  33. return "owner"
  34. default:
  35. return "none"
  36. }
  37. }
  38. // ColorFormat provides a ColorFormatted version of this AccessMode
  39. func (mode AccessMode) ColorFormat(s fmt.State) {
  40. log.ColorFprintf(s, "%d:%s",
  41. log.NewColoredIDValue(mode),
  42. mode)
  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. // Access represents the highest access level of a user to the repository. The only access type
  56. // that is not in this table is the real owner of a repository. In case of an organization
  57. // repository, the members of the owners team are in this table.
  58. type Access struct {
  59. ID int64 `xorm:"pk autoincr"`
  60. UserID int64 `xorm:"UNIQUE(s)"`
  61. RepoID int64 `xorm:"UNIQUE(s)"`
  62. Mode AccessMode
  63. }
  64. func accessLevel(e Engine, user *User, repo *Repository) (AccessMode, error) {
  65. mode := AccessModeNone
  66. var userID int64
  67. restricted := false
  68. if user != nil {
  69. userID = user.ID
  70. restricted = user.IsRestricted
  71. }
  72. if !restricted && !repo.IsPrivate {
  73. mode = AccessModeRead
  74. }
  75. if userID == 0 {
  76. return mode, nil
  77. }
  78. if userID == repo.OwnerID {
  79. return AccessModeOwner, nil
  80. }
  81. a := &Access{UserID: userID, RepoID: repo.ID}
  82. if has, err := e.Get(a); !has || err != nil {
  83. return mode, err
  84. }
  85. return a.Mode, nil
  86. }
  87. type repoAccess struct {
  88. Access `xorm:"extends"`
  89. Repository `xorm:"extends"`
  90. }
  91. func (repoAccess) TableName() string {
  92. return "access"
  93. }
  94. // GetRepositoryAccesses finds all repositories with their access mode where a user has access but does not own.
  95. func (user *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
  96. rows, err := x.
  97. Join("INNER", "repository", "repository.id = access.repo_id").
  98. Where("access.user_id = ?", user.ID).
  99. And("repository.owner_id <> ?", user.ID).
  100. Rows(new(repoAccess))
  101. if err != nil {
  102. return nil, err
  103. }
  104. defer rows.Close()
  105. var repos = make(map[*Repository]AccessMode, 10)
  106. var ownerCache = make(map[int64]*User, 10)
  107. for rows.Next() {
  108. var repo repoAccess
  109. err = rows.Scan(&repo)
  110. if err != nil {
  111. return nil, err
  112. }
  113. var ok bool
  114. if repo.Owner, ok = ownerCache[repo.OwnerID]; !ok {
  115. if err = repo.GetOwner(); err != nil {
  116. return nil, err
  117. }
  118. ownerCache[repo.OwnerID] = repo.Owner
  119. }
  120. repos[&repo.Repository] = repo.Access.Mode
  121. }
  122. return repos, nil
  123. }
  124. // GetAccessibleRepositories finds repositories which the user has access but does not own.
  125. // If limit is smaller than 1 means returns all found results.
  126. func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) {
  127. sess := x.
  128. Where("owner_id !=? ", user.ID).
  129. Desc("updated_unix")
  130. if limit > 0 {
  131. sess.Limit(limit)
  132. repos = make([]*Repository, 0, limit)
  133. } else {
  134. repos = make([]*Repository, 0, 10)
  135. }
  136. return repos, sess.
  137. Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).
  138. Find(&repos)
  139. }
  140. func maxAccessMode(modes ...AccessMode) AccessMode {
  141. max := AccessModeNone
  142. for _, mode := range modes {
  143. if mode > max {
  144. max = mode
  145. }
  146. }
  147. return max
  148. }
  149. type userAccess struct {
  150. User *User
  151. Mode AccessMode
  152. }
  153. // updateUserAccess updates an access map so that user has at least mode
  154. func updateUserAccess(accessMap map[int64]*userAccess, user *User, mode AccessMode) {
  155. if ua, ok := accessMap[user.ID]; ok {
  156. ua.Mode = maxAccessMode(ua.Mode, mode)
  157. } else {
  158. accessMap[user.ID] = &userAccess{User: user, Mode: mode}
  159. }
  160. }
  161. // FIXME: do cross-comparison so reduce deletions and additions to the minimum?
  162. func (repo *Repository) refreshAccesses(e Engine, accessMap map[int64]*userAccess) (err error) {
  163. minMode := AccessModeRead
  164. if !repo.IsPrivate {
  165. minMode = AccessModeWrite
  166. }
  167. newAccesses := make([]Access, 0, len(accessMap))
  168. for userID, ua := range accessMap {
  169. if ua.Mode < minMode && !ua.User.IsRestricted {
  170. continue
  171. }
  172. newAccesses = append(newAccesses, Access{
  173. UserID: userID,
  174. RepoID: repo.ID,
  175. Mode: ua.Mode,
  176. })
  177. }
  178. // Delete old accesses and insert new ones for repository.
  179. if _, err = e.Delete(&Access{RepoID: repo.ID}); err != nil {
  180. return fmt.Errorf("delete old accesses: %v", err)
  181. } else if _, err = e.Insert(newAccesses); err != nil {
  182. return fmt.Errorf("insert new accesses: %v", err)
  183. }
  184. return nil
  185. }
  186. // refreshCollaboratorAccesses retrieves repository collaborations with their access modes.
  187. func (repo *Repository) refreshCollaboratorAccesses(e Engine, accessMap map[int64]*userAccess) error {
  188. collaborators, err := repo.getCollaborators(e)
  189. if err != nil {
  190. return fmt.Errorf("getCollaborations: %v", err)
  191. }
  192. for _, c := range collaborators {
  193. updateUserAccess(accessMap, c.User, c.Collaboration.Mode)
  194. }
  195. return nil
  196. }
  197. // recalculateTeamAccesses recalculates new accesses for teams of an organization
  198. // except the team whose ID is given. It is used to assign a team ID when
  199. // remove repository from that team.
  200. func (repo *Repository) recalculateTeamAccesses(e Engine, ignTeamID int64) (err error) {
  201. accessMap := make(map[int64]*userAccess, 20)
  202. if err = repo.getOwner(e); err != nil {
  203. return err
  204. } else if !repo.Owner.IsOrganization() {
  205. return fmt.Errorf("owner is not an organization: %d", repo.OwnerID)
  206. }
  207. if err = repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  208. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  209. }
  210. if err = repo.Owner.getTeams(e); err != nil {
  211. return err
  212. }
  213. for _, t := range repo.Owner.Teams {
  214. if t.ID == ignTeamID {
  215. continue
  216. }
  217. // Owner team gets owner access, and skip for teams that do not
  218. // have relations with repository.
  219. if t.IsOwnerTeam() {
  220. t.Authorize = AccessModeOwner
  221. } else if !t.hasRepository(e, repo.ID) {
  222. continue
  223. }
  224. if err = t.getMembers(e); err != nil {
  225. return fmt.Errorf("getMembers '%d': %v", t.ID, err)
  226. }
  227. for _, m := range t.Members {
  228. updateUserAccess(accessMap, m, t.Authorize)
  229. }
  230. }
  231. return repo.refreshAccesses(e, accessMap)
  232. }
  233. // recalculateUserAccess recalculates new access for a single user
  234. // Usable if we know access only affected one user
  235. func (repo *Repository) recalculateUserAccess(e Engine, uid int64) (err error) {
  236. minMode := AccessModeRead
  237. if !repo.IsPrivate {
  238. minMode = AccessModeWrite
  239. }
  240. accessMode := AccessModeNone
  241. collaborator, err := repo.getCollaboration(e, uid)
  242. if err != nil {
  243. return err
  244. } else if collaborator != nil {
  245. accessMode = collaborator.Mode
  246. }
  247. if err = repo.getOwner(e); err != nil {
  248. return err
  249. } else if repo.Owner.IsOrganization() {
  250. var teams []Team
  251. if err := e.Join("INNER", "team_repo", "team_repo.team_id = team.id").
  252. Join("INNER", "team_user", "team_user.team_id = team.id").
  253. Where("team.org_id = ?", repo.OwnerID).
  254. And("team_repo.repo_id=?", repo.ID).
  255. And("team_user.uid=?", uid).
  256. Find(&teams); err != nil {
  257. return err
  258. }
  259. for _, t := range teams {
  260. if t.IsOwnerTeam() {
  261. t.Authorize = AccessModeOwner
  262. }
  263. accessMode = maxAccessMode(accessMode, t.Authorize)
  264. }
  265. }
  266. // Delete old user accesses and insert new one for repository.
  267. if _, err = e.Delete(&Access{RepoID: repo.ID, UserID: uid}); err != nil {
  268. return fmt.Errorf("delete old user accesses: %v", err)
  269. } else if accessMode >= minMode {
  270. if _, err = e.Insert(&Access{RepoID: repo.ID, UserID: uid, Mode: accessMode}); err != nil {
  271. return fmt.Errorf("insert new user accesses: %v", err)
  272. }
  273. }
  274. return nil
  275. }
  276. func (repo *Repository) recalculateAccesses(e Engine) error {
  277. if repo.Owner.IsOrganization() {
  278. return repo.recalculateTeamAccesses(e, 0)
  279. }
  280. accessMap := make(map[int64]*userAccess, 20)
  281. if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  282. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  283. }
  284. return repo.refreshAccesses(e, accessMap)
  285. }
  286. // RecalculateAccesses recalculates all accesses for repository.
  287. func (repo *Repository) RecalculateAccesses() error {
  288. return repo.recalculateAccesses(x)
  289. }