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.

267 lines
6.5 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
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
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
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
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 2015 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 user
  6. import (
  7. "fmt"
  8. "path"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/util"
  14. "code.gitea.io/gitea/routers/org"
  15. )
  16. // GetUserByName get user by name
  17. func GetUserByName(ctx *context.Context, name string) *models.User {
  18. user, err := models.GetUserByName(name)
  19. if err != nil {
  20. if models.IsErrUserNotExist(err) {
  21. ctx.NotFound("GetUserByName", nil)
  22. } else {
  23. ctx.ServerError("GetUserByName", err)
  24. }
  25. return nil
  26. }
  27. return user
  28. }
  29. // GetUserByParams returns user whose name is presented in URL paramenter.
  30. func GetUserByParams(ctx *context.Context) *models.User {
  31. return GetUserByName(ctx, ctx.Params(":username"))
  32. }
  33. // Profile render user's profile page
  34. func Profile(ctx *context.Context) {
  35. uname := ctx.Params(":username")
  36. // Special handle for FireFox requests favicon.ico.
  37. if uname == "favicon.ico" {
  38. ctx.ServeFile(path.Join(setting.StaticRootPath, "public/img/favicon.png"))
  39. return
  40. } else if strings.HasSuffix(uname, ".png") {
  41. ctx.Error(404)
  42. return
  43. }
  44. isShowKeys := false
  45. if strings.HasSuffix(uname, ".keys") {
  46. isShowKeys = true
  47. uname = strings.TrimSuffix(uname, ".keys")
  48. }
  49. isShowGPG := false
  50. if strings.HasSuffix(uname, ".gpg") {
  51. isShowGPG = true
  52. uname = strings.TrimSuffix(uname, ".gpg")
  53. }
  54. ctxUser := GetUserByName(ctx, uname)
  55. if ctx.Written() {
  56. return
  57. }
  58. // Show SSH keys.
  59. if isShowKeys {
  60. ShowSSHKeys(ctx, ctxUser.ID)
  61. return
  62. }
  63. // Show GPG keys.
  64. if isShowGPG {
  65. ShowGPGKeys(ctx, ctxUser.ID)
  66. return
  67. }
  68. if ctxUser.IsOrganization() {
  69. org.Home(ctx)
  70. return
  71. }
  72. // Show OpenID URIs
  73. openIDs, err := models.GetUserOpenIDs(ctxUser.ID)
  74. if err != nil {
  75. ctx.ServerError("GetUserOpenIDs", err)
  76. return
  77. }
  78. ctx.Data["Title"] = ctxUser.DisplayName()
  79. ctx.Data["PageIsUserProfile"] = true
  80. ctx.Data["Owner"] = ctxUser
  81. ctx.Data["OpenIDs"] = openIDs
  82. ctx.Data["EnableHeatmap"] = setting.Service.EnableUserHeatmap
  83. ctx.Data["HeatmapUser"] = ctxUser.Name
  84. showPrivate := ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == ctxUser.ID)
  85. orgs, err := models.GetOrgsByUserID(ctxUser.ID, showPrivate)
  86. if err != nil {
  87. ctx.ServerError("GetOrgsByUserIDDesc", err)
  88. return
  89. }
  90. ctx.Data["Orgs"] = orgs
  91. ctx.Data["HasOrgsVisible"] = models.HasOrgsVisible(orgs, ctx.User)
  92. tab := ctx.Query("tab")
  93. ctx.Data["TabName"] = tab
  94. page := ctx.QueryInt("page")
  95. if page <= 0 {
  96. page = 1
  97. }
  98. topicOnly := ctx.QueryBool("topic")
  99. var (
  100. repos []*models.Repository
  101. count int64
  102. total int
  103. orderBy models.SearchOrderBy
  104. )
  105. ctx.Data["SortType"] = ctx.Query("sort")
  106. switch ctx.Query("sort") {
  107. case "newest":
  108. orderBy = models.SearchOrderByNewest
  109. case "oldest":
  110. orderBy = models.SearchOrderByOldest
  111. case "recentupdate":
  112. orderBy = models.SearchOrderByRecentUpdated
  113. case "leastupdate":
  114. orderBy = models.SearchOrderByLeastUpdated
  115. case "reversealphabetically":
  116. orderBy = models.SearchOrderByAlphabeticallyReverse
  117. case "alphabetically":
  118. orderBy = models.SearchOrderByAlphabetically
  119. case "moststars":
  120. orderBy = models.SearchOrderByStarsReverse
  121. case "feweststars":
  122. orderBy = models.SearchOrderByStars
  123. case "mostforks":
  124. orderBy = models.SearchOrderByForksReverse
  125. case "fewestforks":
  126. orderBy = models.SearchOrderByForks
  127. default:
  128. ctx.Data["SortType"] = "recentupdate"
  129. orderBy = models.SearchOrderByRecentUpdated
  130. }
  131. keyword := strings.Trim(ctx.Query("q"), " ")
  132. ctx.Data["Keyword"] = keyword
  133. switch tab {
  134. case "followers":
  135. items, err := ctxUser.GetFollowers(models.ListOptions{
  136. PageSize: setting.UI.User.RepoPagingNum,
  137. Page: page,
  138. })
  139. if err != nil {
  140. ctx.ServerError("GetFollowers", err)
  141. return
  142. }
  143. ctx.Data["Cards"] = items
  144. total = ctxUser.NumFollowers
  145. case "following":
  146. items, err := ctxUser.GetFollowing(models.ListOptions{
  147. PageSize: setting.UI.User.RepoPagingNum,
  148. Page: page,
  149. })
  150. if err != nil {
  151. ctx.ServerError("GetFollowing", err)
  152. return
  153. }
  154. ctx.Data["Cards"] = items
  155. total = ctxUser.NumFollowing
  156. case "activity":
  157. retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser,
  158. Actor: ctx.User,
  159. IncludePrivate: showPrivate,
  160. OnlyPerformedBy: true,
  161. IncludeDeleted: false,
  162. })
  163. if ctx.Written() {
  164. return
  165. }
  166. case "stars":
  167. ctx.Data["PageIsProfileStarList"] = true
  168. repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
  169. ListOptions: models.ListOptions{
  170. PageSize: setting.UI.User.RepoPagingNum,
  171. Page: page,
  172. },
  173. Actor: ctx.User,
  174. Keyword: keyword,
  175. OrderBy: orderBy,
  176. Private: ctx.IsSigned,
  177. StarredByID: ctxUser.ID,
  178. Collaborate: util.OptionalBoolFalse,
  179. TopicOnly: topicOnly,
  180. IncludeDescription: setting.UI.SearchRepoDescription,
  181. })
  182. if err != nil {
  183. ctx.ServerError("SearchRepository", err)
  184. return
  185. }
  186. total = int(count)
  187. default:
  188. repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
  189. ListOptions: models.ListOptions{
  190. PageSize: setting.UI.User.RepoPagingNum,
  191. Page: page,
  192. },
  193. Actor: ctx.User,
  194. Keyword: keyword,
  195. OwnerID: ctxUser.ID,
  196. OrderBy: orderBy,
  197. Private: ctx.IsSigned,
  198. Collaborate: util.OptionalBoolFalse,
  199. TopicOnly: topicOnly,
  200. IncludeDescription: setting.UI.SearchRepoDescription,
  201. })
  202. if err != nil {
  203. ctx.ServerError("SearchRepository", err)
  204. return
  205. }
  206. total = int(count)
  207. }
  208. ctx.Data["Repos"] = repos
  209. ctx.Data["Total"] = total
  210. pager := context.NewPagination(total, setting.UI.User.RepoPagingNum, page, 5)
  211. pager.SetDefaultParams(ctx)
  212. ctx.Data["Page"] = pager
  213. ctx.Data["ShowUserEmail"] = len(ctxUser.Email) > 0 && ctx.IsSigned && (!ctxUser.KeepEmailPrivate || ctxUser.ID == ctx.User.ID)
  214. ctx.HTML(200, tplProfile)
  215. }
  216. // Action response for follow/unfollow user request
  217. func Action(ctx *context.Context) {
  218. u := GetUserByParams(ctx)
  219. if ctx.Written() {
  220. return
  221. }
  222. var err error
  223. switch ctx.Params(":action") {
  224. case "follow":
  225. err = models.FollowUser(ctx.User.ID, u.ID)
  226. case "unfollow":
  227. err = models.UnfollowUser(ctx.User.ID, u.ID)
  228. }
  229. if err != nil {
  230. ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
  231. return
  232. }
  233. ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink())
  234. }