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.

200 lines
5.3 KiB

10 years ago
9 years ago
  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 his own.
  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.refreshCollaboratorAccesses(e, accessMap); err != nil {
  135. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  136. }
  137. if err = repo.getOwner(e); err != nil {
  138. return err
  139. }
  140. if repo.Owner.IsOrganization() {
  141. if err = repo.Owner.getTeams(e); err != nil {
  142. return err
  143. }
  144. for _, t := range repo.Owner.Teams {
  145. if t.ID == ignTeamID {
  146. continue
  147. }
  148. if t.IsOwnerTeam() {
  149. t.Authorize = ACCESS_MODE_OWNER
  150. }
  151. if err = t.getMembers(e); err != nil {
  152. return fmt.Errorf("getMembers '%d': %v", t.ID, err)
  153. }
  154. for _, m := range t.Members {
  155. accessMap[m.Id] = maxAccessMode(accessMap[m.Id], t.Authorize)
  156. }
  157. }
  158. }
  159. return repo.refreshAccesses(e, accessMap)
  160. }
  161. func (repo *Repository) recalculateAccesses(e Engine) error {
  162. accessMap := make(map[int64]AccessMode, 20)
  163. if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  164. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  165. }
  166. return repo.refreshAccesses(e, accessMap)
  167. }
  168. // RecalculateAccesses recalculates all accesses for repository.
  169. func (r *Repository) RecalculateAccesses() error {
  170. return r.recalculateAccesses(x)
  171. }