access.go 6.7 KB

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