access.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // Adds team members access.
  128. if repo.Owner.IsOrganization() {
  129. if err = repo.Owner.GetTeams(); err != nil {
  130. return fmt.Errorf("GetTeams: %v", err)
  131. }
  132. for _, t := range repo.Owner.Teams {
  133. if err = t.GetMembers(); err != nil {
  134. return fmt.Errorf("GetMembers: %v", err)
  135. }
  136. for _, m := range t.Members {
  137. if t.IsOwnerTeam() {
  138. accessMap[m.Id] = ACCESS_MODE_OWNER
  139. } else {
  140. accessMap[m.Id] = maxAccessMode(accessMap[m.Id], t.Authorize)
  141. }
  142. }
  143. }
  144. }
  145. return nil
  146. }
  147. // recalculateTeamAccesses recalculates new accesses for teams of an organization
  148. // except the team whose ID is given. It is used to assign a team ID when
  149. // remove repository from that team.
  150. func (repo *Repository) recalculateTeamAccesses(e Engine, ignTeamID int64) (err error) {
  151. accessMap := make(map[int64]AccessMode, 20)
  152. if err = repo.getOwner(e); err != nil {
  153. return err
  154. }
  155. if err = repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  156. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  157. }
  158. if repo.Owner.IsOrganization() {
  159. if err = repo.Owner.getTeams(e); err != nil {
  160. return err
  161. }
  162. for _, t := range repo.Owner.Teams {
  163. if t.ID == ignTeamID {
  164. continue
  165. }
  166. // Owner team gets owner access, and skip for teams that do not
  167. // have relations with repository.
  168. if t.IsOwnerTeam() {
  169. t.Authorize = ACCESS_MODE_OWNER
  170. } else if !t.hasRepository(e, repo.ID) {
  171. continue
  172. }
  173. if err = t.getMembers(e); err != nil {
  174. return fmt.Errorf("getMembers '%d': %v", t.ID, err)
  175. }
  176. for _, m := range t.Members {
  177. accessMap[m.Id] = maxAccessMode(accessMap[m.Id], t.Authorize)
  178. }
  179. }
  180. }
  181. return repo.refreshAccesses(e, accessMap)
  182. }
  183. func (repo *Repository) recalculateAccesses(e Engine) error {
  184. accessMap := make(map[int64]AccessMode, 20)
  185. if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  186. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  187. }
  188. return repo.refreshAccesses(e, accessMap)
  189. }
  190. // RecalculateAccesses recalculates all accesses for repository.
  191. func (r *Repository) RecalculateAccesses() error {
  192. return r.recalculateAccesses(x)
  193. }