本站源代码
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.

314 lines
8.1KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. // AccessMode specifies the users access mode
  11. type AccessMode int
  12. const (
  13. // AccessModeNone no access
  14. AccessModeNone AccessMode = iota // 0
  15. // AccessModeRead read access
  16. AccessModeRead // 1
  17. // AccessModeWrite write access
  18. AccessModeWrite // 2
  19. // AccessModeAdmin admin access
  20. AccessModeAdmin // 3
  21. // AccessModeOwner owner access
  22. AccessModeOwner // 4
  23. )
  24. func (mode AccessMode) String() string {
  25. switch mode {
  26. case AccessModeRead:
  27. return "read"
  28. case AccessModeWrite:
  29. return "write"
  30. case AccessModeAdmin:
  31. return "admin"
  32. case AccessModeOwner:
  33. return "owner"
  34. default:
  35. return "none"
  36. }
  37. }
  38. // ColorFormat provides a ColorFormatted version of this AccessMode
  39. func (mode AccessMode) ColorFormat(s fmt.State) {
  40. log.ColorFprintf(s, "%d:%s",
  41. log.NewColoredIDValue(mode),
  42. mode)
  43. }
  44. // ParseAccessMode returns corresponding access mode to given permission string.
  45. func ParseAccessMode(permission string) AccessMode {
  46. switch permission {
  47. case "write":
  48. return AccessModeWrite
  49. case "admin":
  50. return AccessModeAdmin
  51. default:
  52. return AccessModeRead
  53. }
  54. }
  55. // Access represents the highest access level of a user to the repository. The only access type
  56. // that is not in this table is the real owner of a repository. In case of an organization
  57. // repository, the members of the owners team are in this table.
  58. type Access struct {
  59. ID int64 `xorm:"pk autoincr"`
  60. UserID int64 `xorm:"UNIQUE(s)"`
  61. RepoID int64 `xorm:"UNIQUE(s)"`
  62. Mode AccessMode
  63. }
  64. func accessLevel(e Engine, userID int64, repo *Repository) (AccessMode, error) {
  65. mode := AccessModeRead
  66. if !repo.IsPrivate {
  67. mode = AccessModeRead
  68. }
  69. if userID == 0 {
  70. return mode, nil
  71. }
  72. if userID == repo.OwnerID {
  73. return AccessModeOwner, nil
  74. }
  75. a := &Access{UserID: userID, RepoID: repo.ID}
  76. if has, err := e.Get(a); !has || err != nil {
  77. return mode, err
  78. }
  79. return a.Mode, nil
  80. }
  81. type repoAccess struct {
  82. Access `xorm:"extends"`
  83. Repository `xorm:"extends"`
  84. }
  85. func (repoAccess) TableName() string {
  86. return "access"
  87. }
  88. // GetRepositoryAccesses finds all repositories with their access mode where a user has access but does not own.
  89. func (user *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
  90. rows, err := x.
  91. Join("INNER", "repository", "repository.id = access.repo_id").
  92. Where("access.user_id = ?", user.ID).
  93. And("repository.owner_id <> ?", user.ID).
  94. Rows(new(repoAccess))
  95. if err != nil {
  96. return nil, err
  97. }
  98. defer rows.Close()
  99. var repos = make(map[*Repository]AccessMode, 10)
  100. var ownerCache = make(map[int64]*User, 10)
  101. for rows.Next() {
  102. var repo repoAccess
  103. err = rows.Scan(&repo)
  104. if err != nil {
  105. return nil, err
  106. }
  107. var ok bool
  108. if repo.Owner, ok = ownerCache[repo.OwnerID]; !ok {
  109. if err = repo.GetOwner(); err != nil {
  110. return nil, err
  111. }
  112. ownerCache[repo.OwnerID] = repo.Owner
  113. }
  114. repos[&repo.Repository] = repo.Access.Mode
  115. }
  116. return repos, nil
  117. }
  118. // GetAccessibleRepositories finds repositories which the user has access but does not own.
  119. // If limit is smaller than 1 means returns all found results.
  120. func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) {
  121. sess := x.
  122. Where("owner_id !=? ", user.ID).
  123. Desc("updated_unix")
  124. if limit > 0 {
  125. sess.Limit(limit)
  126. repos = make([]*Repository, 0, limit)
  127. } else {
  128. repos = make([]*Repository, 0, 10)
  129. }
  130. return repos, sess.
  131. Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).
  132. Find(&repos)
  133. }
  134. func maxAccessMode(modes ...AccessMode) AccessMode {
  135. max := AccessModeNone
  136. for _, mode := range modes {
  137. if mode > max {
  138. max = mode
  139. }
  140. }
  141. return max
  142. }
  143. // FIXME: do cross-comparison so reduce deletions and additions to the minimum?
  144. func (repo *Repository) refreshAccesses(e Engine, accessMap map[int64]AccessMode) (err error) {
  145. minMode := AccessModeRead
  146. if !repo.IsPrivate {
  147. minMode = AccessModeWrite
  148. }
  149. newAccesses := make([]Access, 0, len(accessMap))
  150. for userID, mode := range accessMap {
  151. if mode < minMode {
  152. continue
  153. }
  154. newAccesses = append(newAccesses, Access{
  155. UserID: userID,
  156. RepoID: repo.ID,
  157. Mode: mode,
  158. })
  159. }
  160. // Delete old accesses and insert new ones for repository.
  161. if _, err = e.Delete(&Access{RepoID: repo.ID}); err != nil {
  162. return fmt.Errorf("delete old accesses: %v", err)
  163. } else if _, err = e.Insert(newAccesses); err != nil {
  164. return fmt.Errorf("insert new accesses: %v", err)
  165. }
  166. return nil
  167. }
  168. // refreshCollaboratorAccesses retrieves repository collaborations with their access modes.
  169. func (repo *Repository) refreshCollaboratorAccesses(e Engine, accessMap map[int64]AccessMode) error {
  170. collaborations, err := repo.getCollaborations(e)
  171. if err != nil {
  172. return fmt.Errorf("getCollaborations: %v", err)
  173. }
  174. for _, c := range collaborations {
  175. accessMap[c.UserID] = c.Mode
  176. }
  177. return nil
  178. }
  179. // recalculateTeamAccesses recalculates new accesses for teams of an organization
  180. // except the team whose ID is given. It is used to assign a team ID when
  181. // remove repository from that team.
  182. func (repo *Repository) recalculateTeamAccesses(e Engine, ignTeamID int64) (err error) {
  183. accessMap := make(map[int64]AccessMode, 20)
  184. if err = repo.getOwner(e); err != nil {
  185. return err
  186. } else if !repo.Owner.IsOrganization() {
  187. return fmt.Errorf("owner is not an organization: %d", repo.OwnerID)
  188. }
  189. if err = repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  190. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  191. }
  192. if err = repo.Owner.getTeams(e); err != nil {
  193. return err
  194. }
  195. for _, t := range repo.Owner.Teams {
  196. if t.ID == ignTeamID {
  197. continue
  198. }
  199. // Owner team gets owner access, and skip for teams that do not
  200. // have relations with repository.
  201. if t.IsOwnerTeam() {
  202. t.Authorize = AccessModeOwner
  203. } else if !t.hasRepository(e, repo.ID) {
  204. continue
  205. }
  206. if err = t.getMembers(e); err != nil {
  207. return fmt.Errorf("getMembers '%d': %v", t.ID, err)
  208. }
  209. for _, m := range t.Members {
  210. accessMap[m.ID] = maxAccessMode(accessMap[m.ID], t.Authorize)
  211. }
  212. }
  213. return repo.refreshAccesses(e, accessMap)
  214. }
  215. // recalculateUserAccess recalculates new access for a single user
  216. // Usable if we know access only affected one user
  217. func (repo *Repository) recalculateUserAccess(e Engine, uid int64) (err error) {
  218. minMode := AccessModeRead
  219. if !repo.IsPrivate {
  220. minMode = AccessModeWrite
  221. }
  222. accessMode := AccessModeNone
  223. collaborator, err := repo.getCollaboration(e, uid)
  224. if err != nil {
  225. return err
  226. } else if collaborator != nil {
  227. accessMode = collaborator.Mode
  228. }
  229. if err = repo.getOwner(e); err != nil {
  230. return err
  231. } else if repo.Owner.IsOrganization() {
  232. var teams []Team
  233. if err := e.Join("INNER", "team_repo", "team_repo.team_id = team.id").
  234. Join("INNER", "team_user", "team_user.team_id = team.id").
  235. Where("team.org_id = ?", repo.OwnerID).
  236. And("team_repo.repo_id=?", repo.ID).
  237. And("team_user.uid=?", uid).
  238. Find(&teams); err != nil {
  239. return err
  240. }
  241. for _, t := range teams {
  242. if t.IsOwnerTeam() {
  243. t.Authorize = AccessModeOwner
  244. }
  245. accessMode = maxAccessMode(accessMode, t.Authorize)
  246. }
  247. }
  248. // Delete old user accesses and insert new one for repository.
  249. if _, err = e.Delete(&Access{RepoID: repo.ID, UserID: uid}); err != nil {
  250. return fmt.Errorf("delete old user accesses: %v", err)
  251. } else if accessMode >= minMode {
  252. if _, err = e.Insert(&Access{RepoID: repo.ID, UserID: uid, Mode: accessMode}); err != nil {
  253. return fmt.Errorf("insert new user accesses: %v", err)
  254. }
  255. }
  256. return nil
  257. }
  258. func (repo *Repository) recalculateAccesses(e Engine) error {
  259. if repo.Owner.IsOrganization() {
  260. return repo.recalculateTeamAccesses(e, 0)
  261. }
  262. accessMap := make(map[int64]AccessMode, 20)
  263. if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
  264. return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
  265. }
  266. return repo.refreshAccesses(e, accessMap)
  267. }
  268. // RecalculateAccesses recalculates all accesses for repository.
  269. func (repo *Repository) RecalculateAccesses() error {
  270. return repo.recalculateAccesses(x)
  271. }
上海开阖软件有限公司 沪ICP备12045867号-1