access.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2014 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 models
  5. import (
  6. "fmt"
  7. "github.com/gogits/gogs/modules/log"
  8. )
  9. type AccessMode int
  10. const (
  11. ACCESS_MODE_NONE AccessMode = iota // 0
  12. ACCESS_MODE_READ // 1
  13. ACCESS_MODE_WRITE // 2
  14. ACCESS_MODE_ADMIN // 3
  15. ACCESS_MODE_OWNER // 4
  16. )
  17. // Access represents the highest access level of a user to the repository. The only access type
  18. // that is not in this table is the real owner of a repository. In case of an organization
  19. // repository, the members of the owners team are in this table.
  20. type Access struct {
  21. ID int64 `xorm:"pk autoincr"`
  22. UserID int64 `xorm:"UNIQUE(s)"`
  23. RepoID int64 `xorm:"UNIQUE(s)"`
  24. Mode AccessMode
  25. }
  26. func accessLevel(e Engine, u *User, repo *Repository) (AccessMode, error) {
  27. mode := ACCESS_MODE_NONE
  28. if !repo.IsPrivate {
  29. mode = ACCESS_MODE_READ
  30. }
  31. if u == nil {
  32. return mode, nil
  33. }
  34. if u.Id == repo.OwnerID {
  35. return ACCESS_MODE_OWNER, nil
  36. }
  37. a := &Access{UserID: u.Id, RepoID: repo.ID}
  38. if has, err := e.Get(a); !has || err != nil {
  39. return mode, err
  40. }
  41. return a.Mode, nil
  42. }
  43. // AccessLevel returns the Access a user has to a repository. Will return NoneAccess if the
  44. // user does not have access. User can be nil!
  45. func AccessLevel(u *User, repo *Repository) (AccessMode, error) {
  46. return accessLevel(x, u, repo)
  47. }
  48. func hasAccess(e Engine, u *User, repo *Repository, testMode AccessMode) (bool, error) {
  49. mode, err := accessLevel(e, u, repo)
  50. return testMode <= mode, err
  51. }
  52. // HasAccess returns true if someone has the request access level. User can be nil!
  53. func HasAccess(u *User, repo *Repository, testMode AccessMode) (bool, error) {
  54. return hasAccess(x, u, repo, testMode)
  55. }
  56. // GetRepositoryAccesses finds all repositories with their access mode where a user has access but does not own.
  57. func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
  58. accesses := make([]*Access, 0, 10)
  59. if err := x.Find(&accesses, &Access{UserID: u.Id}); err != nil {
  60. return nil, err
  61. }
  62. repos := make(map[*Repository]AccessMode, len(accesses))
  63. for _, access := range accesses {
  64. repo, err := GetRepositoryByID(access.RepoID)
  65. if err != nil {
  66. if IsErrRepoNotExist(err) {
  67. log.Error(4, "GetRepositoryByID: %v", err)
  68. continue
  69. }
  70. return nil, err
  71. }
  72. if err = repo.GetOwner(); err != nil {
  73. return nil, err
  74. } else if repo.OwnerID == u.Id {
  75. continue
  76. }
  77. repos[repo] = access.Mode
  78. }
  79. return repos, nil
  80. }
  81. // GetAccessibleRepositories finds all repositories where a user has access but does not own.
  82. func (u *User) GetAccessibleRepositories() ([]*Repository, error) {
  83. accesses := make([]*Access, 0, 10)
  84. if err := x.Find(&accesses, &Access{UserID: u.Id}); err != nil {
  85. return nil, err
  86. }
  87. if len(accesses) == 0 {
  88. return []*Repository{}, nil
  89. }
  90. repoIDs := make([]int64, 0, len(accesses))
  91. for _, access := range accesses {
  92. repoIDs = append(repoIDs, access.RepoID)
  93. }
  94. repos := make([]*Repository, 0, len(repoIDs))
  95. return repos, x.Where("owner_id != ?", u.Id).In("id", repoIDs).Desc("updated").Find(&repos)
  96. }
  97. func maxAccessMode(modes ...AccessMode) AccessMode {
  98. max := ACCESS_MODE_NONE
  99. for _, mode := range modes {
  100. if mode > max {
  101. max = mode
  102. }
  103. }
  104. return max
  105. }
  106. // FIXME: do corss-comparison so reduce deletions and additions to the minimum?
  107. func (repo *Repository) refreshAccesses(e Engine, accessMap map[int64]AccessMode) (err error) {
  108. minMode := ACCESS_MODE_READ
  109. if !repo.IsPrivate {
  110. minMode = ACCESS_MODE_WRITE
  111. }
  112. newAccesses := make([]Access, 0, len(accessMap))
  113. for userID, mode := range accessMap {
  114. if mode < minMode {
  115. continue
  116. }
  117. newAccesses = append(newAccesses, Access{
  118. UserID: userID,
  119. RepoID: repo.ID,
  120. Mode: mode,
  121. })
  122. }
  123. // Delete old accesses and insert new ones for repository.
  124. if _, err = e.Delete(&Access{RepoID: repo.ID}); err != nil {
  125. return fmt.Errorf("delete old accesses: %v", err)
  126. } else if _, err = e.Insert(newAccesses); err != nil {
  127. return fmt.Errorf("insert new accesses: %v", err)
  128. }
  129. return nil
  130. }
  131. // refreshCollaboratorAccesses retrieves repository collaborations with their access modes.
  132. func (repo *Repository) refreshCollaboratorAccesses(e Engine, accessMap map[int64]AccessMode) error {
  133. collaborations, err := repo.getCollaborations(e)
  134. if err != nil {
  135. return fmt.Errorf("getCollaborations: %v", err)
  136. }
  137. for _, c := range collaborations {
  138. accessMap[c.UserID] = c.Mode
  139. }
  140. return nil
  141. }
  142. // recalculateTeamAccesses recalculates new accesses for teams of an organization
  143. // except the team whose ID is given. It is used to assign a team ID when
  144. // remove repository from that team.
  145. func (repo *Repository) recalculateTeamAccesses(e Engine, ignTeamID int64) (err error) {
  146. accessMap := make(map[int64]AccessMode, 20)
  147. if err = repo.getOwner(e); err != nil {
  148. return err
  149. } else if !repo.Owner.IsOrganization() {
  150. return fmt.Errorf("owner is not an organization: %d", repo.OwnerID)
  151. }
  152. if err = repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  153. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  154. }
  155. if err = repo.Owner.getTeams(e); err != nil {
  156. return err
  157. }
  158. for _, t := range repo.Owner.Teams {
  159. if t.ID == ignTeamID {
  160. continue
  161. }
  162. // Owner team gets owner access, and skip for teams that do not
  163. // have relations with repository.
  164. if t.IsOwnerTeam() {
  165. t.Authorize = ACCESS_MODE_OWNER
  166. } else if !t.hasRepository(e, repo.ID) {
  167. continue
  168. }
  169. if err = t.getMembers(e); err != nil {
  170. return fmt.Errorf("getMembers '%d': %v", t.ID, err)
  171. }
  172. for _, m := range t.Members {
  173. accessMap[m.Id] = maxAccessMode(accessMap[m.Id], t.Authorize)
  174. }
  175. }
  176. return repo.refreshAccesses(e, accessMap)
  177. }
  178. func (repo *Repository) recalculateAccesses(e Engine) error {
  179. if repo.Owner.IsOrganization() {
  180. return repo.recalculateTeamAccesses(e, 0)
  181. }
  182. accessMap := make(map[int64]AccessMode, 20)
  183. if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  184. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  185. }
  186. return repo.refreshAccesses(e, accessMap)
  187. }
  188. // RecalculateAccesses recalculates all accesses for repository.
  189. func (r *Repository) RecalculateAccesses() error {
  190. return r.recalculateAccesses(x)
  191. }