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.

265 lines
7.1 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, user *User, repo *Repository) (AccessMode, error) {
  55. mode := AccessModeNone
  56. if !repo.IsPrivate {
  57. mode = AccessModeRead
  58. }
  59. if user == nil {
  60. return mode, nil
  61. }
  62. if user.ID == repo.OwnerID {
  63. return AccessModeOwner, nil
  64. }
  65. a := &Access{UserID: user.ID, 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. // AccessLevel returns the Access a user has to a repository. Will return NoneAccess if the
  72. // user does not have access. User can be nil!
  73. func AccessLevel(user *User, repo *Repository) (AccessMode, error) {
  74. return accessLevel(x, user, repo)
  75. }
  76. func hasAccess(e Engine, user *User, repo *Repository, testMode AccessMode) (bool, error) {
  77. mode, err := accessLevel(e, user, repo)
  78. return testMode <= mode, err
  79. }
  80. // HasAccess returns true if someone has the request access level. User can be nil!
  81. func HasAccess(user *User, repo *Repository, testMode AccessMode) (bool, error) {
  82. return hasAccess(x, user, repo, testMode)
  83. }
  84. // GetRepositoryAccesses finds all repositories with their access mode where a user has access but does not own.
  85. func (user *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
  86. accesses := make([]*Access, 0, 10)
  87. type RepoAccess struct {
  88. Access `xorm:"extends"`
  89. Repository `xorm:"extends"`
  90. }
  91. rows, err := x.
  92. Join("INNER", "repository", "respository.id = access.repo_id").
  93. Where("access.user_id = ?", user.ID).
  94. And("repository.owner_id <> ?", user.ID).
  95. Rows(new(RepoAccess))
  96. if err != nil {
  97. return nil, err
  98. }
  99. defer rows.Close()
  100. var repos = make(map[*Repository]AccessMode, len(accesses))
  101. var ownerCache = make(map[int64]*User, len(accesses))
  102. for rows.Next() {
  103. var repo RepoAccess
  104. err = rows.Scan(&repo)
  105. if err != nil {
  106. return nil, err
  107. }
  108. var ok bool
  109. if repo.Owner, ok = ownerCache[repo.OwnerID]; !ok {
  110. if err = repo.GetOwner(); err != nil {
  111. return nil, err
  112. }
  113. ownerCache[repo.OwnerID] = repo.Owner
  114. }
  115. repos[&repo.Repository] = repo.Access.Mode
  116. }
  117. return repos, nil
  118. }
  119. // GetAccessibleRepositories finds repositories which the user has access but does not own.
  120. // If limit is smaller than 1 means returns all found results.
  121. func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) {
  122. sess := x.
  123. Where("owner_id !=? ", user.ID).
  124. Desc("updated_unix")
  125. if limit > 0 {
  126. sess.Limit(limit)
  127. repos = make([]*Repository, 0, limit)
  128. } else {
  129. repos = make([]*Repository, 0, 10)
  130. }
  131. return repos, sess.
  132. Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).
  133. Find(&repos)
  134. }
  135. func maxAccessMode(modes ...AccessMode) AccessMode {
  136. max := AccessModeNone
  137. for _, mode := range modes {
  138. if mode > max {
  139. max = mode
  140. }
  141. }
  142. return max
  143. }
  144. // FIXME: do corss-comparison so reduce deletions and additions to the minimum?
  145. func (repo *Repository) refreshAccesses(e Engine, accessMap map[int64]AccessMode) (err error) {
  146. minMode := AccessModeRead
  147. if !repo.IsPrivate {
  148. minMode = AccessModeWrite
  149. }
  150. newAccesses := make([]Access, 0, len(accessMap))
  151. for userID, mode := range accessMap {
  152. if mode < minMode {
  153. continue
  154. }
  155. newAccesses = append(newAccesses, Access{
  156. UserID: userID,
  157. RepoID: repo.ID,
  158. Mode: mode,
  159. })
  160. }
  161. // Delete old accesses and insert new ones for repository.
  162. if _, err = e.Delete(&Access{RepoID: repo.ID}); err != nil {
  163. return fmt.Errorf("delete old accesses: %v", err)
  164. } else if _, err = e.Insert(newAccesses); err != nil {
  165. return fmt.Errorf("insert new accesses: %v", err)
  166. }
  167. return nil
  168. }
  169. // refreshCollaboratorAccesses retrieves repository collaborations with their access modes.
  170. func (repo *Repository) refreshCollaboratorAccesses(e Engine, accessMap map[int64]AccessMode) error {
  171. collaborations, err := repo.getCollaborations(e)
  172. if err != nil {
  173. return fmt.Errorf("getCollaborations: %v", err)
  174. }
  175. for _, c := range collaborations {
  176. accessMap[c.UserID] = c.Mode
  177. }
  178. return nil
  179. }
  180. // recalculateTeamAccesses recalculates new accesses for teams of an organization
  181. // except the team whose ID is given. It is used to assign a team ID when
  182. // remove repository from that team.
  183. func (repo *Repository) recalculateTeamAccesses(e Engine, ignTeamID int64) (err error) {
  184. accessMap := make(map[int64]AccessMode, 20)
  185. if err = repo.getOwner(e); err != nil {
  186. return err
  187. } else if !repo.Owner.IsOrganization() {
  188. return fmt.Errorf("owner is not an organization: %d", repo.OwnerID)
  189. }
  190. if err = repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  191. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  192. }
  193. if err = repo.Owner.getTeams(e); err != nil {
  194. return err
  195. }
  196. for _, t := range repo.Owner.Teams {
  197. if t.ID == ignTeamID {
  198. continue
  199. }
  200. // Owner team gets owner access, and skip for teams that do not
  201. // have relations with repository.
  202. if t.IsOwnerTeam() {
  203. t.Authorize = AccessModeOwner
  204. } else if !t.hasRepository(e, repo.ID) {
  205. continue
  206. }
  207. if err = t.getMembers(e); err != nil {
  208. return fmt.Errorf("getMembers '%d': %v", t.ID, err)
  209. }
  210. for _, m := range t.Members {
  211. accessMap[m.ID] = maxAccessMode(accessMap[m.ID], t.Authorize)
  212. }
  213. }
  214. return repo.refreshAccesses(e, accessMap)
  215. }
  216. func (repo *Repository) recalculateAccesses(e Engine) error {
  217. if repo.Owner.IsOrganization() {
  218. return repo.recalculateTeamAccesses(e, 0)
  219. }
  220. accessMap := make(map[int64]AccessMode, 20)
  221. if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  222. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  223. }
  224. return repo.refreshAccesses(e, accessMap)
  225. }
  226. // RecalculateAccesses recalculates all accesses for repository.
  227. func (repo *Repository) RecalculateAccesses() error {
  228. return repo.recalculateAccesses(x)
  229. }