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.

341 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
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
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. }
  182. if len(newAccesses) == 0 {
  183. return nil
  184. }
  185. if _, err = e.Insert(newAccesses); err != nil {
  186. return fmt.Errorf("insert new accesses: %v", err)
  187. }
  188. return nil
  189. }
  190. // refreshCollaboratorAccesses retrieves repository collaborations with their access modes.
  191. func (repo *Repository) refreshCollaboratorAccesses(e Engine, accessMap map[int64]*userAccess) error {
  192. collaborators, err := repo.getCollaborators(e, ListOptions{})
  193. if err != nil {
  194. return fmt.Errorf("getCollaborations: %v", err)
  195. }
  196. for _, c := range collaborators {
  197. updateUserAccess(accessMap, c.User, c.Collaboration.Mode)
  198. }
  199. return nil
  200. }
  201. // recalculateTeamAccesses recalculates new accesses for teams of an organization
  202. // except the team whose ID is given. It is used to assign a team ID when
  203. // remove repository from that team.
  204. func (repo *Repository) recalculateTeamAccesses(e Engine, ignTeamID int64) (err error) {
  205. accessMap := make(map[int64]*userAccess, 20)
  206. if err = repo.getOwner(e); err != nil {
  207. return err
  208. } else if !repo.Owner.IsOrganization() {
  209. return fmt.Errorf("owner is not an organization: %d", repo.OwnerID)
  210. }
  211. if err = repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  212. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  213. }
  214. if err = repo.Owner.getTeams(e); err != nil {
  215. return err
  216. }
  217. for _, t := range repo.Owner.Teams {
  218. if t.ID == ignTeamID {
  219. continue
  220. }
  221. // Owner team gets owner access, and skip for teams that do not
  222. // have relations with repository.
  223. if t.IsOwnerTeam() {
  224. t.Authorize = AccessModeOwner
  225. } else if !t.hasRepository(e, repo.ID) {
  226. continue
  227. }
  228. if err = t.getMembers(e); err != nil {
  229. return fmt.Errorf("getMembers '%d': %v", t.ID, err)
  230. }
  231. for _, m := range t.Members {
  232. updateUserAccess(accessMap, m, t.Authorize)
  233. }
  234. }
  235. return repo.refreshAccesses(e, accessMap)
  236. }
  237. // recalculateUserAccess recalculates new access for a single user
  238. // Usable if we know access only affected one user
  239. func (repo *Repository) recalculateUserAccess(e Engine, uid int64) (err error) {
  240. minMode := AccessModeRead
  241. if !repo.IsPrivate {
  242. minMode = AccessModeWrite
  243. }
  244. accessMode := AccessModeNone
  245. collaborator, err := repo.getCollaboration(e, uid)
  246. if err != nil {
  247. return err
  248. } else if collaborator != nil {
  249. accessMode = collaborator.Mode
  250. }
  251. if err = repo.getOwner(e); err != nil {
  252. return err
  253. } else if repo.Owner.IsOrganization() {
  254. var teams []Team
  255. if err := e.Join("INNER", "team_repo", "team_repo.team_id = team.id").
  256. Join("INNER", "team_user", "team_user.team_id = team.id").
  257. Where("team.org_id = ?", repo.OwnerID).
  258. And("team_repo.repo_id=?", repo.ID).
  259. And("team_user.uid=?", uid).
  260. Find(&teams); err != nil {
  261. return err
  262. }
  263. for _, t := range teams {
  264. if t.IsOwnerTeam() {
  265. t.Authorize = AccessModeOwner
  266. }
  267. accessMode = maxAccessMode(accessMode, t.Authorize)
  268. }
  269. }
  270. // Delete old user accesses and insert new one for repository.
  271. if _, err = e.Delete(&Access{RepoID: repo.ID, UserID: uid}); err != nil {
  272. return fmt.Errorf("delete old user accesses: %v", err)
  273. } else if accessMode >= minMode {
  274. if _, err = e.Insert(&Access{RepoID: repo.ID, UserID: uid, Mode: accessMode}); err != nil {
  275. return fmt.Errorf("insert new user accesses: %v", err)
  276. }
  277. }
  278. return nil
  279. }
  280. func (repo *Repository) recalculateAccesses(e Engine) error {
  281. if repo.Owner.IsOrganization() {
  282. return repo.recalculateTeamAccesses(e, 0)
  283. }
  284. accessMap := make(map[int64]*userAccess, 20)
  285. if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  286. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  287. }
  288. return repo.refreshAccesses(e, accessMap)
  289. }
  290. // RecalculateAccesses recalculates all accesses for repository.
  291. func (repo *Repository) RecalculateAccesses() error {
  292. return repo.recalculateAccesses(x)
  293. }