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.

132 lines
3.1 KiB

Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
  1. // Copyright 2019 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 org
  5. import (
  6. "strings"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. const (
  13. tplOrgHome base.TplName = "org/home"
  14. )
  15. // Home show organization home page
  16. func Home(ctx *context.Context) {
  17. ctx.SetParams(":org", ctx.Params(":username"))
  18. context.HandleOrgAssignment(ctx)
  19. if ctx.Written() {
  20. return
  21. }
  22. org := ctx.Org.Organization
  23. if !models.HasOrgVisible(org, ctx.User) {
  24. ctx.NotFound("HasOrgVisible", nil)
  25. return
  26. }
  27. ctx.Data["Title"] = org.DisplayName()
  28. var orderBy models.SearchOrderBy
  29. ctx.Data["SortType"] = ctx.Query("sort")
  30. switch ctx.Query("sort") {
  31. case "newest":
  32. orderBy = models.SearchOrderByNewest
  33. case "oldest":
  34. orderBy = models.SearchOrderByOldest
  35. case "recentupdate":
  36. orderBy = models.SearchOrderByRecentUpdated
  37. case "leastupdate":
  38. orderBy = models.SearchOrderByLeastUpdated
  39. case "reversealphabetically":
  40. orderBy = models.SearchOrderByAlphabeticallyReverse
  41. case "alphabetically":
  42. orderBy = models.SearchOrderByAlphabetically
  43. case "moststars":
  44. orderBy = models.SearchOrderByStarsReverse
  45. case "feweststars":
  46. orderBy = models.SearchOrderByStars
  47. case "mostforks":
  48. orderBy = models.SearchOrderByForksReverse
  49. case "fewestforks":
  50. orderBy = models.SearchOrderByForks
  51. default:
  52. ctx.Data["SortType"] = "recentupdate"
  53. orderBy = models.SearchOrderByRecentUpdated
  54. }
  55. keyword := strings.Trim(ctx.Query("q"), " ")
  56. ctx.Data["Keyword"] = keyword
  57. page := ctx.QueryInt("page")
  58. if page <= 0 {
  59. page = 1
  60. }
  61. var (
  62. repos []*models.Repository
  63. count int64
  64. err error
  65. )
  66. repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
  67. Keyword: keyword,
  68. OwnerID: org.ID,
  69. OrderBy: orderBy,
  70. Private: ctx.IsSigned,
  71. Actor: ctx.User,
  72. Page: page,
  73. IsProfile: true,
  74. PageSize: setting.UI.User.RepoPagingNum,
  75. IncludeDescription: setting.UI.SearchRepoDescription,
  76. })
  77. if err != nil {
  78. ctx.ServerError("SearchRepository", err)
  79. return
  80. }
  81. var opts = models.FindOrgMembersOpts{
  82. OrgID: org.ID,
  83. PublicOnly: true,
  84. Limit: 25,
  85. }
  86. if ctx.User != nil {
  87. isMember, err := org.IsOrgMember(ctx.User.ID)
  88. if err != nil {
  89. ctx.Error(500, "IsOrgMember")
  90. return
  91. }
  92. opts.PublicOnly = !isMember && !ctx.User.IsAdmin
  93. }
  94. members, _, err := models.FindOrgMembers(opts)
  95. if err != nil {
  96. ctx.ServerError("FindOrgMembers", err)
  97. return
  98. }
  99. membersCount, err := models.CountOrgMembers(opts)
  100. if err != nil {
  101. ctx.ServerError("CountOrgMembers", err)
  102. return
  103. }
  104. ctx.Data["Repos"] = repos
  105. ctx.Data["Total"] = count
  106. ctx.Data["MembersTotal"] = membersCount
  107. ctx.Data["Members"] = members
  108. ctx.Data["Teams"] = org.Teams
  109. pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5)
  110. pager.SetDefaultParams(ctx)
  111. ctx.Data["Page"] = pager
  112. ctx.HTML(200, tplOrgHome)
  113. }