A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

284 lines
7.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>
3 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>
3 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>
3 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>
3 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>
3 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/markup/markdown"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/util"
  15. "code.gitea.io/gitea/routers/org"
  16. )
  17. // GetUserByName get user by name
  18. func GetUserByName(ctx *context.Context, name string) *models.User {
  19. user, err := models.GetUserByName(name)
  20. if err != nil {
  21. if models.IsErrUserNotExist(err) {
  22. ctx.NotFound("GetUserByName", nil)
  23. } else {
  24. ctx.ServerError("GetUserByName", err)
  25. }
  26. return nil
  27. }
  28. return user
  29. }
  30. // GetUserByParams returns user whose name is presented in URL paramenter.
  31. func GetUserByParams(ctx *context.Context) *models.User {
  32. return GetUserByName(ctx, ctx.Params(":username"))
  33. }
  34. // Profile render user's profile page
  35. func Profile(ctx *context.Context) {
  36. uname := ctx.Params(":username")
  37. // Special handle for FireFox requests favicon.ico.
  38. if uname == "favicon.ico" {
  39. ctx.ServeFile(path.Join(setting.StaticRootPath, "public/img/favicon.png"))
  40. return
  41. } else if strings.HasSuffix(uname, ".png") {
  42. ctx.Error(404)
  43. return
  44. }
  45. isShowKeys := false
  46. if strings.HasSuffix(uname, ".keys") {
  47. isShowKeys = true
  48. uname = strings.TrimSuffix(uname, ".keys")
  49. }
  50. isShowGPG := false
  51. if strings.HasSuffix(uname, ".gpg") {
  52. isShowGPG = true
  53. uname = strings.TrimSuffix(uname, ".gpg")
  54. }
  55. ctxUser := GetUserByName(ctx, uname)
  56. if ctx.Written() {
  57. return
  58. }
  59. // Show SSH keys.
  60. if isShowKeys {
  61. ShowSSHKeys(ctx, ctxUser.ID)
  62. return
  63. }
  64. // Show GPG keys.
  65. if isShowGPG {
  66. ShowGPGKeys(ctx, ctxUser.ID)
  67. return
  68. }
  69. if ctxUser.IsOrganization() {
  70. org.Home(ctx)
  71. return
  72. }
  73. // Show OpenID URIs
  74. openIDs, err := models.GetUserOpenIDs(ctxUser.ID)
  75. if err != nil {
  76. ctx.ServerError("GetUserOpenIDs", err)
  77. return
  78. }
  79. ctx.Data["Title"] = ctxUser.DisplayName()
  80. ctx.Data["PageIsUserProfile"] = true
  81. ctx.Data["Owner"] = ctxUser
  82. ctx.Data["OpenIDs"] = openIDs
  83. // no heatmap access for admins; GetUserHeatmapDataByUser ignores the calling user
  84. // so everyone would get the same empty heatmap
  85. ctx.Data["EnableHeatmap"] = setting.Service.EnableUserHeatmap && !ctxUser.KeepActivityPrivate
  86. ctx.Data["HeatmapUser"] = ctxUser.Name
  87. if len(ctxUser.Description) != 0 {
  88. ctx.Data["RenderedDescription"] = string(markdown.Render([]byte(ctxUser.Description), ctx.Repo.RepoLink, map[string]string{"mode": "document"}))
  89. }
  90. showPrivate := ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == ctxUser.ID)
  91. orgs, err := models.GetOrgsByUserID(ctxUser.ID, showPrivate)
  92. if err != nil {
  93. ctx.ServerError("GetOrgsByUserIDDesc", err)
  94. return
  95. }
  96. ctx.Data["Orgs"] = orgs
  97. ctx.Data["HasOrgsVisible"] = models.HasOrgsVisible(orgs, ctx.User)
  98. tab := ctx.Query("tab")
  99. ctx.Data["TabName"] = tab
  100. page := ctx.QueryInt("page")
  101. if page <= 0 {
  102. page = 1
  103. }
  104. topicOnly := ctx.QueryBool("topic")
  105. var (
  106. repos []*models.Repository
  107. count int64
  108. total int
  109. orderBy models.SearchOrderBy
  110. )
  111. ctx.Data["SortType"] = ctx.Query("sort")
  112. switch ctx.Query("sort") {
  113. case "newest":
  114. orderBy = models.SearchOrderByNewest
  115. case "oldest":
  116. orderBy = models.SearchOrderByOldest
  117. case "recentupdate":
  118. orderBy = models.SearchOrderByRecentUpdated
  119. case "leastupdate":
  120. orderBy = models.SearchOrderByLeastUpdated
  121. case "reversealphabetically":
  122. orderBy = models.SearchOrderByAlphabeticallyReverse
  123. case "alphabetically":
  124. orderBy = models.SearchOrderByAlphabetically
  125. case "moststars":
  126. orderBy = models.SearchOrderByStarsReverse
  127. case "feweststars":
  128. orderBy = models.SearchOrderByStars
  129. case "mostforks":
  130. orderBy = models.SearchOrderByForksReverse
  131. case "fewestforks":
  132. orderBy = models.SearchOrderByForks
  133. default:
  134. ctx.Data["SortType"] = "recentupdate"
  135. orderBy = models.SearchOrderByRecentUpdated
  136. }
  137. keyword := strings.Trim(ctx.Query("q"), " ")
  138. ctx.Data["Keyword"] = keyword
  139. switch tab {
  140. case "followers":
  141. items, err := ctxUser.GetFollowers(models.ListOptions{
  142. PageSize: setting.UI.User.RepoPagingNum,
  143. Page: page,
  144. })
  145. if err != nil {
  146. ctx.ServerError("GetFollowers", err)
  147. return
  148. }
  149. ctx.Data["Cards"] = items
  150. total = ctxUser.NumFollowers
  151. case "following":
  152. items, err := ctxUser.GetFollowing(models.ListOptions{
  153. PageSize: setting.UI.User.RepoPagingNum,
  154. Page: page,
  155. })
  156. if err != nil {
  157. ctx.ServerError("GetFollowing", err)
  158. return
  159. }
  160. ctx.Data["Cards"] = items
  161. total = ctxUser.NumFollowing
  162. case "activity":
  163. retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser,
  164. Actor: ctx.User,
  165. IncludePrivate: showPrivate,
  166. OnlyPerformedBy: true,
  167. IncludeDeleted: false,
  168. })
  169. if ctx.Written() {
  170. return
  171. }
  172. case "stars":
  173. ctx.Data["PageIsProfileStarList"] = true
  174. repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
  175. ListOptions: models.ListOptions{
  176. PageSize: setting.UI.User.RepoPagingNum,
  177. Page: page,
  178. },
  179. Actor: ctx.User,
  180. Keyword: keyword,
  181. OrderBy: orderBy,
  182. Private: ctx.IsSigned,
  183. StarredByID: ctxUser.ID,
  184. Collaborate: util.OptionalBoolFalse,
  185. TopicOnly: topicOnly,
  186. IncludeDescription: setting.UI.SearchRepoDescription,
  187. })
  188. if err != nil {
  189. ctx.ServerError("SearchRepository", err)
  190. return
  191. }
  192. total = int(count)
  193. case "projects":
  194. ctx.Data["OpenProjects"], _, err = models.GetProjects(models.ProjectSearchOptions{
  195. Page: -1,
  196. IsClosed: util.OptionalBoolFalse,
  197. Type: models.ProjectTypeIndividual,
  198. })
  199. if err != nil {
  200. ctx.ServerError("GetProjects", err)
  201. return
  202. }
  203. default:
  204. repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
  205. ListOptions: models.ListOptions{
  206. PageSize: setting.UI.User.RepoPagingNum,
  207. Page: page,
  208. },
  209. Actor: ctx.User,
  210. Keyword: keyword,
  211. OwnerID: ctxUser.ID,
  212. OrderBy: orderBy,
  213. Private: ctx.IsSigned,
  214. Collaborate: util.OptionalBoolFalse,
  215. TopicOnly: topicOnly,
  216. IncludeDescription: setting.UI.SearchRepoDescription,
  217. })
  218. if err != nil {
  219. ctx.ServerError("SearchRepository", err)
  220. return
  221. }
  222. total = int(count)
  223. }
  224. ctx.Data["Repos"] = repos
  225. ctx.Data["Total"] = total
  226. pager := context.NewPagination(total, setting.UI.User.RepoPagingNum, page, 5)
  227. pager.SetDefaultParams(ctx)
  228. ctx.Data["Page"] = pager
  229. ctx.Data["ShowUserEmail"] = len(ctxUser.Email) > 0 && ctx.IsSigned && (!ctxUser.KeepEmailPrivate || ctxUser.ID == ctx.User.ID)
  230. ctx.HTML(200, tplProfile)
  231. }
  232. // Action response for follow/unfollow user request
  233. func Action(ctx *context.Context) {
  234. u := GetUserByParams(ctx)
  235. if ctx.Written() {
  236. return
  237. }
  238. var err error
  239. switch ctx.Params(":action") {
  240. case "follow":
  241. err = models.FollowUser(ctx.User.ID, u.ID)
  242. case "unfollow":
  243. err = models.UnfollowUser(ctx.User.ID, u.ID)
  244. }
  245. if err != nil {
  246. ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
  247. return
  248. }
  249. ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink())
  250. }