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.

279 lines
7.5 KiB

  1. // Copyright 2018 The Gitea 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. // Permission contains all the permissions related variables to a repository for a user
  6. type Permission struct {
  7. AccessMode AccessMode
  8. Units []*RepoUnit
  9. UnitsMode map[UnitType]AccessMode
  10. }
  11. // IsOwner returns true if current user is the owner of repository.
  12. func (p *Permission) IsOwner() bool {
  13. return p.AccessMode >= AccessModeOwner
  14. }
  15. // IsAdmin returns true if current user has admin or higher access of repository.
  16. func (p *Permission) IsAdmin() bool {
  17. return p.AccessMode >= AccessModeAdmin
  18. }
  19. // HasAccess returns true if the current user has at least read access to any unit of this repository
  20. func (p *Permission) HasAccess() bool {
  21. if p.UnitsMode == nil {
  22. return p.AccessMode >= AccessModeRead
  23. }
  24. return len(p.UnitsMode) > 0
  25. }
  26. // UnitAccessMode returns current user accessmode to the specify unit of the repository
  27. func (p *Permission) UnitAccessMode(unitType UnitType) AccessMode {
  28. if p.UnitsMode == nil {
  29. for _, u := range p.Units {
  30. if u.Type == unitType {
  31. return p.AccessMode
  32. }
  33. }
  34. return AccessModeNone
  35. }
  36. return p.UnitsMode[unitType]
  37. }
  38. // CanAccess returns true if user has mode access to the unit of the repository
  39. func (p *Permission) CanAccess(mode AccessMode, unitType UnitType) bool {
  40. return p.UnitAccessMode(unitType) >= mode
  41. }
  42. // CanAccessAny returns true if user has mode access to any of the units of the repository
  43. func (p *Permission) CanAccessAny(mode AccessMode, unitTypes ...UnitType) bool {
  44. for _, u := range unitTypes {
  45. if p.CanAccess(mode, u) {
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. // CanRead returns true if user could read to this unit
  52. func (p *Permission) CanRead(unitType UnitType) bool {
  53. return p.CanAccess(AccessModeRead, unitType)
  54. }
  55. // CanReadAny returns true if user has read access to any of the units of the repository
  56. func (p *Permission) CanReadAny(unitTypes ...UnitType) bool {
  57. return p.CanAccessAny(AccessModeRead, unitTypes...)
  58. }
  59. // CanReadIssuesOrPulls returns true if isPull is true and user could read pull requests and
  60. // returns true if isPull is false and user could read to issues
  61. func (p *Permission) CanReadIssuesOrPulls(isPull bool) bool {
  62. if isPull {
  63. return p.CanRead(UnitTypePullRequests)
  64. }
  65. return p.CanRead(UnitTypeIssues)
  66. }
  67. // CanWrite returns true if user could write to this unit
  68. func (p *Permission) CanWrite(unitType UnitType) bool {
  69. return p.CanAccess(AccessModeWrite, unitType)
  70. }
  71. // CanWriteIssuesOrPulls returns true if isPull is true and user could write to pull requests and
  72. // returns true if isPull is false and user could write to issues
  73. func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
  74. if isPull {
  75. return p.CanWrite(UnitTypePullRequests)
  76. }
  77. return p.CanWrite(UnitTypeIssues)
  78. }
  79. // GetUserRepoPermission returns the user permissions to the repository
  80. func GetUserRepoPermission(repo *Repository, user *User) (Permission, error) {
  81. return getUserRepoPermission(x, repo, user)
  82. }
  83. func getUserRepoPermission(e Engine, repo *Repository, user *User) (perm Permission, err error) {
  84. // anonymous user visit private repo.
  85. // TODO: anonymous user visit public unit of private repo???
  86. if user == nil && repo.IsPrivate {
  87. perm.AccessMode = AccessModeNone
  88. return
  89. }
  90. if err = repo.getUnits(e); err != nil {
  91. return
  92. }
  93. perm.Units = repo.Units
  94. // anonymous visit public repo
  95. if user == nil {
  96. perm.AccessMode = AccessModeRead
  97. return
  98. }
  99. // Admin or the owner has super access to the repository
  100. if user.IsAdmin || user.ID == repo.OwnerID {
  101. perm.AccessMode = AccessModeOwner
  102. return
  103. }
  104. // plain user
  105. perm.AccessMode, err = accessLevel(e, user.ID, repo)
  106. if err != nil {
  107. return
  108. }
  109. if err = repo.getOwner(e); err != nil {
  110. return
  111. }
  112. if !repo.Owner.IsOrganization() {
  113. return
  114. }
  115. perm.UnitsMode = make(map[UnitType]AccessMode)
  116. // Collaborators on organization
  117. if isCollaborator, err := repo.isCollaborator(e, user.ID); err != nil {
  118. return perm, err
  119. } else if isCollaborator {
  120. for _, u := range repo.Units {
  121. perm.UnitsMode[u.Type] = perm.AccessMode
  122. }
  123. }
  124. // get units mode from teams
  125. teams, err := getUserRepoTeams(e, repo.OwnerID, user.ID, repo.ID)
  126. if err != nil {
  127. return
  128. }
  129. // if user in an owner team
  130. for _, team := range teams {
  131. if team.Authorize >= AccessModeOwner {
  132. perm.AccessMode = AccessModeOwner
  133. perm.UnitsMode = nil
  134. return
  135. }
  136. }
  137. for _, u := range repo.Units {
  138. var found bool
  139. for _, team := range teams {
  140. if team.unitEnabled(e, u.Type) {
  141. m := perm.UnitsMode[u.Type]
  142. if m < team.Authorize {
  143. perm.UnitsMode[u.Type] = team.Authorize
  144. }
  145. found = true
  146. }
  147. }
  148. // for a public repo on an organization, user have read permission on non-team defined units.
  149. if !found && !repo.IsPrivate {
  150. if _, ok := perm.UnitsMode[u.Type]; !ok {
  151. perm.UnitsMode[u.Type] = AccessModeRead
  152. }
  153. }
  154. }
  155. // remove no permission units
  156. perm.Units = make([]*RepoUnit, 0, len(repo.Units))
  157. for t := range perm.UnitsMode {
  158. for _, u := range repo.Units {
  159. if u.Type == t {
  160. perm.Units = append(perm.Units, u)
  161. }
  162. }
  163. }
  164. return
  165. }
  166. // IsUserRepoAdmin return ture if user has admin right of a repo
  167. func IsUserRepoAdmin(repo *Repository, user *User) (bool, error) {
  168. return isUserRepoAdmin(x, repo, user)
  169. }
  170. func isUserRepoAdmin(e Engine, repo *Repository, user *User) (bool, error) {
  171. if user == nil || repo == nil {
  172. return false, nil
  173. }
  174. if user.IsAdmin {
  175. return true, nil
  176. }
  177. mode, err := accessLevel(e, user.ID, repo)
  178. if err != nil {
  179. return false, err
  180. }
  181. if mode >= AccessModeAdmin {
  182. return true, nil
  183. }
  184. teams, err := getUserRepoTeams(e, repo.OwnerID, user.ID, repo.ID)
  185. if err != nil {
  186. return false, err
  187. }
  188. for _, team := range teams {
  189. if team.Authorize >= AccessModeAdmin {
  190. return true, nil
  191. }
  192. }
  193. return false, nil
  194. }
  195. // AccessLevel returns the Access a user has to a repository. Will return NoneAccess if the
  196. // user does not have access.
  197. func AccessLevel(user *User, repo *Repository) (AccessMode, error) {
  198. return accessLevelUnit(x, user, repo, UnitTypeCode)
  199. }
  200. func accessLevelUnit(e Engine, user *User, repo *Repository, unitType UnitType) (AccessMode, error) {
  201. perm, err := getUserRepoPermission(e, repo, user)
  202. if err != nil {
  203. return AccessModeNone, err
  204. }
  205. return perm.UnitAccessMode(unitType), nil
  206. }
  207. func hasAccessUnit(e Engine, user *User, repo *Repository, unitType UnitType, testMode AccessMode) (bool, error) {
  208. mode, err := accessLevelUnit(e, user, repo, unitType)
  209. return testMode <= mode, err
  210. }
  211. // HasAccessUnit returns ture if user has testMode to the unit of the repository
  212. func HasAccessUnit(user *User, repo *Repository, unitType UnitType, testMode AccessMode) (bool, error) {
  213. return hasAccessUnit(x, user, repo, unitType, testMode)
  214. }
  215. // canBeAssigned return true if user could be assigned to a repo
  216. // FIXME: user could send PullRequest also could be assigned???
  217. func canBeAssigned(e Engine, user *User, repo *Repository) (bool, error) {
  218. return hasAccessUnit(e, user, repo, UnitTypeCode, AccessModeWrite)
  219. }
  220. func hasAccess(e Engine, userID int64, repo *Repository) (bool, error) {
  221. var user *User
  222. var err error
  223. if userID > 0 {
  224. user, err = getUserByID(e, userID)
  225. if err != nil {
  226. return false, err
  227. }
  228. }
  229. perm, err := getUserRepoPermission(e, repo, user)
  230. if err != nil {
  231. return false, err
  232. }
  233. return perm.HasAccess(), nil
  234. }
  235. // HasAccess returns true if user has access to repo
  236. func HasAccess(userID int64, repo *Repository) (bool, error) {
  237. return hasAccess(x, userID, repo)
  238. }