access.go 6.6 KB

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