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.

252 lines
6.5 KiB

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