access.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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
  12. ACCESS_MODE_READ
  13. ACCESS_MODE_WRITE
  14. ACCESS_MODE_ADMIN
  15. ACCESS_MODE_OWNER
  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. if u.Id == repo.OwnerID {
  33. return ACCESS_MODE_OWNER, nil
  34. }
  35. a := &Access{UserID: u.Id, RepoID: repo.ID}
  36. if has, err := e.Get(a); !has || err != nil {
  37. return mode, err
  38. }
  39. return a.Mode, nil
  40. }
  41. return 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. // GetAccessibleRepositories finds all repositories where a user has access to,
  57. // besides he/she owns.
  58. func (u *User) GetAccessibleRepositories() (map[*Repository]AccessMode, error) {
  59. accesses := make([]*Access, 0, 10)
  60. if err := x.Find(&accesses, &Access{UserID: u.Id}); err != nil {
  61. return nil, err
  62. }
  63. repos := make(map[*Repository]AccessMode, len(accesses))
  64. for _, access := range accesses {
  65. repo, err := GetRepositoryByID(access.RepoID)
  66. if err != nil {
  67. if IsErrRepoNotExist(err) {
  68. log.Error(4, "%v", err)
  69. continue
  70. }
  71. return nil, err
  72. }
  73. if err = repo.GetOwner(); err != nil {
  74. return nil, err
  75. } else if repo.OwnerID == u.Id {
  76. continue
  77. }
  78. repos[repo] = access.Mode
  79. }
  80. // FIXME: should we generate an ordered list here? Random looks weird.
  81. return repos, nil
  82. }
  83. func maxAccessMode(modes ...AccessMode) AccessMode {
  84. max := ACCESS_MODE_NONE
  85. for _, mode := range modes {
  86. if mode > max {
  87. max = mode
  88. }
  89. }
  90. return max
  91. }
  92. // FIXME: do corss-comparison so reduce deletions and additions to the minimum?
  93. func (repo *Repository) refreshAccesses(e Engine, accessMap map[int64]AccessMode) (err error) {
  94. minMode := ACCESS_MODE_READ
  95. if !repo.IsPrivate {
  96. minMode = ACCESS_MODE_WRITE
  97. }
  98. newAccesses := make([]Access, 0, len(accessMap))
  99. for userID, mode := range accessMap {
  100. if mode < minMode {
  101. continue
  102. }
  103. newAccesses = append(newAccesses, Access{
  104. UserID: userID,
  105. RepoID: repo.ID,
  106. Mode: mode,
  107. })
  108. }
  109. // Delete old accesses and insert new ones for repository.
  110. if _, err = e.Delete(&Access{RepoID: repo.ID}); err != nil {
  111. return fmt.Errorf("delete old accesses: %v", err)
  112. } else if _, err = e.Insert(newAccesses); err != nil {
  113. return fmt.Errorf("insert new accesses: %v", err)
  114. }
  115. return nil
  116. }
  117. // FIXME: should be able to have read-only access.
  118. // Give all collaborators write access.
  119. func (repo *Repository) refreshCollaboratorAccesses(e Engine, accessMap map[int64]AccessMode) error {
  120. collaborators, err := repo.getCollaborators(e)
  121. if err != nil {
  122. return fmt.Errorf("getCollaborators: %v", err)
  123. }
  124. for _, c := range collaborators {
  125. accessMap[c.Id] = ACCESS_MODE_WRITE
  126. }
  127. return nil
  128. }
  129. // recalculateTeamAccesses recalculates new accesses for teams of an organization
  130. // except the team whose ID is given. It is used to assign a team ID when
  131. // remove repository from that team.
  132. func (repo *Repository) recalculateTeamAccesses(e Engine, ignTeamID int64) (err error) {
  133. accessMap := make(map[int64]AccessMode, 20)
  134. if err = repo.getOwner(e); err != nil {
  135. return err
  136. } else if !repo.Owner.IsOrganization() {
  137. return fmt.Errorf("owner is not an organization: %d", repo.OwnerID)
  138. }
  139. if err = repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  140. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  141. }
  142. if err = repo.Owner.getTeams(e); err != nil {
  143. return err
  144. }
  145. for _, t := range repo.Owner.Teams {
  146. if t.ID == ignTeamID {
  147. continue
  148. }
  149. // Owner team gets owner access, and skip for teams that do not
  150. // have relations with repository.
  151. if t.IsOwnerTeam() {
  152. t.Authorize = ACCESS_MODE_OWNER
  153. } else if !t.hasRepository(e, repo.ID) {
  154. continue
  155. }
  156. if err = t.getMembers(e); err != nil {
  157. return fmt.Errorf("getMembers '%d': %v", t.ID, err)
  158. }
  159. for _, m := range t.Members {
  160. accessMap[m.Id] = maxAccessMode(accessMap[m.Id], t.Authorize)
  161. }
  162. }
  163. return repo.refreshAccesses(e, accessMap)
  164. }
  165. func (repo *Repository) recalculateAccesses(e Engine) error {
  166. if repo.Owner.IsOrganization() {
  167. return repo.recalculateTeamAccesses(e, 0)
  168. }
  169. accessMap := make(map[int64]AccessMode, 20)
  170. if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  171. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  172. }
  173. return repo.refreshAccesses(e, accessMap)
  174. }
  175. // RecalculateAccesses recalculates all accesses for repository.
  176. func (r *Repository) RecalculateAccesses() error {
  177. return r.recalculateAccesses(x)
  178. }