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

387 lines
10KB

  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. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/log"
  8. )
  9. // Permission contains all the permissions related variables to a repository for a user
  10. type Permission struct {
  11. AccessMode AccessMode
  12. Units []*RepoUnit
  13. UnitsMode map[UnitType]AccessMode
  14. }
  15. // IsOwner returns true if current user is the owner of repository.
  16. func (p *Permission) IsOwner() bool {
  17. return p.AccessMode >= AccessModeOwner
  18. }
  19. // IsAdmin returns true if current user has admin or higher access of repository.
  20. func (p *Permission) IsAdmin() bool {
  21. return p.AccessMode >= AccessModeAdmin
  22. }
  23. // HasAccess returns true if the current user has at least read access to any unit of this repository
  24. func (p *Permission) HasAccess() bool {
  25. if p.UnitsMode == nil {
  26. return p.AccessMode >= AccessModeRead
  27. }
  28. return len(p.UnitsMode) > 0
  29. }
  30. // UnitAccessMode returns current user accessmode to the specify unit of the repository
  31. func (p *Permission) UnitAccessMode(unitType UnitType) AccessMode {
  32. if p.UnitsMode == nil {
  33. for _, u := range p.Units {
  34. if u.Type == unitType {
  35. return p.AccessMode
  36. }
  37. }
  38. return AccessModeNone
  39. }
  40. return p.UnitsMode[unitType]
  41. }
  42. // CanAccess returns true if user has mode access to the unit of the repository
  43. func (p *Permission) CanAccess(mode AccessMode, unitType UnitType) bool {
  44. return p.UnitAccessMode(unitType) >= mode
  45. }
  46. // CanAccessAny returns true if user has mode access to any of the units of the repository
  47. func (p *Permission) CanAccessAny(mode AccessMode, unitTypes ...UnitType) bool {
  48. for _, u := range unitTypes {
  49. if p.CanAccess(mode, u) {
  50. return true
  51. }
  52. }
  53. return false
  54. }
  55. // CanRead returns true if user could read to this unit
  56. func (p *Permission) CanRead(unitType UnitType) bool {
  57. return p.CanAccess(AccessModeRead, unitType)
  58. }
  59. // CanReadAny returns true if user has read access to any of the units of the repository
  60. func (p *Permission) CanReadAny(unitTypes ...UnitType) bool {
  61. return p.CanAccessAny(AccessModeRead, unitTypes...)
  62. }
  63. // CanReadIssuesOrPulls returns true if isPull is true and user could read pull requests and
  64. // returns true if isPull is false and user could read to issues
  65. func (p *Permission) CanReadIssuesOrPulls(isPull bool) bool {
  66. if isPull {
  67. return p.CanRead(UnitTypePullRequests)
  68. }
  69. return p.CanRead(UnitTypeIssues)
  70. }
  71. // CanWrite returns true if user could write to this unit
  72. func (p *Permission) CanWrite(unitType UnitType) bool {
  73. return p.CanAccess(AccessModeWrite, unitType)
  74. }
  75. // CanWriteIssuesOrPulls returns true if isPull is true and user could write to pull requests and
  76. // returns true if isPull is false and user could write to issues
  77. func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
  78. if isPull {
  79. return p.CanWrite(UnitTypePullRequests)
  80. }
  81. return p.CanWrite(UnitTypeIssues)
  82. }
  83. // ColorFormat writes a colored string for these Permissions
  84. func (p *Permission) ColorFormat(s fmt.State) {
  85. noColor := log.ColorBytes(log.Reset)
  86. format := "AccessMode: %-v, %d Units, %d UnitsMode(s): [ "
  87. args := []interface{}{
  88. p.AccessMode,
  89. log.NewColoredValueBytes(len(p.Units), &noColor),
  90. log.NewColoredValueBytes(len(p.UnitsMode), &noColor),
  91. }
  92. if s.Flag('+') {
  93. for i, unit := range p.Units {
  94. config := ""
  95. if unit.Config != nil {
  96. configBytes, err := unit.Config.ToDB()
  97. config = string(configBytes)
  98. if err != nil {
  99. config = err.Error()
  100. }
  101. }
  102. format += "\nUnits[%d]: ID: %d RepoID: %d Type: %-v Config: %s"
  103. args = append(args,
  104. log.NewColoredValueBytes(i, &noColor),
  105. log.NewColoredIDValue(unit.ID),
  106. log.NewColoredIDValue(unit.RepoID),
  107. unit.Type,
  108. config)
  109. }
  110. for key, value := range p.UnitsMode {
  111. format += "\nUnitMode[%-v]: %-v"
  112. args = append(args,
  113. key,
  114. value)
  115. }
  116. } else {
  117. format += "..."
  118. }
  119. format += " ]"
  120. log.ColorFprintf(s, format, args...)
  121. }
  122. // GetUserRepoPermission returns the user permissions to the repository
  123. func GetUserRepoPermission(repo *Repository, user *User) (Permission, error) {
  124. return getUserRepoPermission(x, repo, user)
  125. }
  126. func getUserRepoPermission(e Engine, repo *Repository, user *User) (perm Permission, err error) {
  127. if log.IsTrace() {
  128. defer func() {
  129. if user == nil {
  130. log.Trace("Permission Loaded for anonymous user in %-v:\nPermissions: %-+v",
  131. repo,
  132. perm)
  133. return
  134. }
  135. log.Trace("Permission Loaded for %-v in %-v:\nPermissions: %-+v",
  136. user,
  137. repo,
  138. perm)
  139. }()
  140. }
  141. // anonymous user visit private repo.
  142. // TODO: anonymous user visit public unit of private repo???
  143. if user == nil && repo.IsPrivate {
  144. perm.AccessMode = AccessModeRead
  145. //return
  146. }
  147. if repo.Owner == nil {
  148. repo.mustOwner(e)
  149. }
  150. var isCollaborator bool
  151. if user != nil {
  152. isCollaborator, err = repo.isCollaborator(e, user.ID)
  153. if err != nil {
  154. return perm, err
  155. }
  156. }
  157. // Prevent strangers from checking out public repo of private orginization
  158. // Allow user if they are collaborator of a repo within a private orginization but not a member of the orginization itself
  159. if repo.Owner.IsOrganization() && !HasOrgVisible(repo.Owner, user) && !isCollaborator {
  160. perm.AccessMode = AccessModeNone
  161. return
  162. }
  163. if err = repo.getUnits(e); err != nil {
  164. return
  165. }
  166. perm.Units = repo.Units
  167. perm.UnitsMode = make(map[UnitType]AccessMode)
  168. // anonymous visit all repo
  169. if user == nil {
  170. perm.AccessMode = AccessModeRead
  171. //匿名用户开放wiki只读权限
  172. perm.UnitsMode[UnitTypeWiki] = perm.AccessMode
  173. return
  174. }
  175. // Admin or the owner has super access to the repository
  176. if user.IsAdmin || user.ID == repo.OwnerID {
  177. perm.AccessMode = AccessModeOwner
  178. //owner库code、issue、PRs等赋予默认owner权限
  179. for _, u := range repo.Units {
  180. perm.UnitsMode[u.Type] = perm.AccessMode
  181. }
  182. return
  183. }
  184. // plain user
  185. perm.AccessMode, err = accessLevel(e, user.ID, repo)
  186. if err != nil {
  187. return
  188. }
  189. //私有库公开wik权限
  190. if repo.IsPrivate {
  191. perm.UnitsMode[UnitTypeWiki] = perm.AccessMode
  192. }else{
  193. //公开库code、issue、PRs等赋予默认只读权限
  194. for _, u := range repo.Units {
  195. perm.UnitsMode[u.Type] = perm.AccessMode
  196. }
  197. }
  198. if err = repo.getOwner(e); err != nil {
  199. return
  200. }
  201. // Collaborators on organization
  202. if isCollaborator {
  203. for _, u := range repo.Units {
  204. perm.UnitsMode[u.Type] = perm.AccessMode
  205. }
  206. }
  207. if !repo.Owner.IsOrganization() {
  208. return
  209. }
  210. // get units mode from teams
  211. teams, err := getUserRepoTeams(e, repo.OwnerID, user.ID, repo.ID)
  212. if err != nil {
  213. return
  214. }
  215. // if user in an owner team
  216. for _, team := range teams {
  217. if team.Authorize >= AccessModeOwner {
  218. perm.AccessMode = AccessModeOwner
  219. perm.UnitsMode = nil
  220. return
  221. }
  222. }
  223. for _, u := range repo.Units {
  224. var found bool
  225. for _, team := range teams {
  226. if team.unitEnabled(e, u.Type) {
  227. m := perm.UnitsMode[u.Type]
  228. if m < team.Authorize {
  229. perm.UnitsMode[u.Type] = team.Authorize
  230. }
  231. found = true
  232. }
  233. }
  234. // for a public repo on an organization, user have read permission on non-team defined units.
  235. if !found && !repo.IsPrivate {
  236. if _, ok := perm.UnitsMode[u.Type]; !ok {
  237. perm.UnitsMode[u.Type] = AccessModeRead
  238. }
  239. }
  240. }
  241. // remove no permission units
  242. perm.Units = make([]*RepoUnit, 0, len(repo.Units))
  243. for t := range perm.UnitsMode {
  244. for _, u := range repo.Units {
  245. if u.Type == t {
  246. perm.Units = append(perm.Units, u)
  247. }
  248. }
  249. }
  250. return
  251. }
  252. // IsUserRepoAdmin return ture if user has admin right of a repo
  253. func IsUserRepoAdmin(repo *Repository, user *User) (bool, error) {
  254. return isUserRepoAdmin(x, repo, user)
  255. }
  256. func isUserRepoAdmin(e Engine, repo *Repository, user *User) (bool, error) {
  257. if user == nil || repo == nil {
  258. return false, nil
  259. }
  260. if user.IsAdmin {
  261. return true, nil
  262. }
  263. mode, err := accessLevel(e, user.ID, repo)
  264. if err != nil {
  265. return false, err
  266. }
  267. if mode >= AccessModeAdmin {
  268. return true, nil
  269. }
  270. teams, err := getUserRepoTeams(e, repo.OwnerID, user.ID, repo.ID)
  271. if err != nil {
  272. return false, err
  273. }
  274. for _, team := range teams {
  275. if team.Authorize >= AccessModeAdmin {
  276. return true, nil
  277. }
  278. }
  279. return false, nil
  280. }
  281. // AccessLevel returns the Access a user has to a repository. Will return NoneAccess if the
  282. // user does not have access.
  283. func AccessLevel(user *User, repo *Repository) (AccessMode, error) {
  284. return accessLevelUnit(x, user, repo, UnitTypeCode)
  285. }
  286. // AccessLevelUnit returns the Access a user has to a repository's. Will return NoneAccess if the
  287. // user does not have access.
  288. func AccessLevelUnit(user *User, repo *Repository, unitType UnitType) (AccessMode, error) {
  289. return accessLevelUnit(x, user, repo, unitType)
  290. }
  291. func accessLevelUnit(e Engine, user *User, repo *Repository, unitType UnitType) (AccessMode, error) {
  292. perm, err := getUserRepoPermission(e, repo, user)
  293. if err != nil {
  294. return AccessModeNone, err
  295. }
  296. return perm.UnitAccessMode(unitType), nil
  297. }
  298. func hasAccessUnit(e Engine, user *User, repo *Repository, unitType UnitType, testMode AccessMode) (bool, error) {
  299. mode, err := accessLevelUnit(e, user, repo, unitType)
  300. return testMode <= mode, err
  301. }
  302. // HasAccessUnit returns ture if user has testMode to the unit of the repository
  303. func HasAccessUnit(user *User, repo *Repository, unitType UnitType, testMode AccessMode) (bool, error) {
  304. return hasAccessUnit(x, user, repo, unitType, testMode)
  305. }
  306. // CanBeAssigned return true if user can be assigned to issue or pull requests in repo
  307. // Currently any write access (code, issues or pr's) is assignable, to match assignee list in user interface.
  308. // FIXME: user could send PullRequest also could be assigned???
  309. func CanBeAssigned(user *User, repo *Repository, isPull bool) (bool, error) {
  310. if user.IsOrganization() {
  311. return false, fmt.Errorf("Organization can't be added as assignee [user_id: %d, repo_id: %d]", user.ID, repo.ID)
  312. }
  313. perm, err := GetUserRepoPermission(repo, user)
  314. if err != nil {
  315. return false, err
  316. }
  317. return perm.CanAccessAny(AccessModeWrite, UnitTypeCode, UnitTypeIssues, UnitTypePullRequests), nil
  318. }
  319. func hasAccess(e Engine, userID int64, repo *Repository) (bool, error) {
  320. var user *User
  321. var err error
  322. if userID > 0 {
  323. user, err = getUserByID(e, userID)
  324. if err != nil {
  325. return false, err
  326. }
  327. }
  328. perm, err := getUserRepoPermission(e, repo, user)
  329. if err != nil {
  330. return false, err
  331. }
  332. return perm.HasAccess(), nil
  333. }
  334. // HasAccess returns true if user has access to repo
  335. func HasAccess(userID int64, repo *Repository) (bool, error) {
  336. return hasAccess(x, userID, repo)
  337. }
上海开阖软件有限公司 沪ICP备12045867号-1