You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

profile.go 8.7 kB

4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
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>
5 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>
5 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>
5 years ago
4 years ago
4 years ago
3 years ago
3 years ago
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>
5 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>
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. "code.gitea.io/gitea/services/badge"
  8. "errors"
  9. "fmt"
  10. "path"
  11. "strings"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/util"
  16. "code.gitea.io/gitea/routers/org"
  17. )
  18. // GetUserByName get user by name
  19. func GetUserByName(ctx *context.Context, name string) *models.User {
  20. user, err := models.GetUserByName(name)
  21. if err != nil {
  22. if models.IsErrUserNotExist(err) {
  23. ctx.NotFound("GetUserByName", nil)
  24. } else {
  25. ctx.ServerError("GetUserByName", err)
  26. }
  27. return nil
  28. }
  29. return user
  30. }
  31. // GetUserByParams returns user whose name is presented in URL paramenter.
  32. func GetUserByParams(ctx *context.Context) *models.User {
  33. return GetUserByName(ctx, ctx.Params(":username"))
  34. }
  35. // Profile render user's profile page
  36. func Profile(ctx *context.Context) {
  37. uname := ctx.Params(":username")
  38. // Special handle for FireFox requests favicon.ico.
  39. if uname == "favicon.ico" {
  40. ctx.ServeFile(path.Join(setting.StaticRootPath, "public/img/favicon.png"))
  41. return
  42. } else if strings.HasSuffix(uname, ".png") {
  43. ctx.Error(404)
  44. return
  45. }
  46. isShowKeys := false
  47. if strings.HasSuffix(uname, ".keys") {
  48. isShowKeys = true
  49. uname = strings.TrimSuffix(uname, ".keys")
  50. }
  51. isShowGPG := false
  52. if strings.HasSuffix(uname, ".gpg") {
  53. isShowGPG = true
  54. uname = strings.TrimSuffix(uname, ".gpg")
  55. }
  56. ctxUser := GetUserByName(ctx, uname)
  57. if ctx.Written() {
  58. return
  59. }
  60. // Show SSH keys.
  61. if isShowKeys {
  62. ShowSSHKeys(ctx, ctxUser.ID)
  63. return
  64. }
  65. // Show GPG keys.
  66. if isShowGPG {
  67. ShowGPGKeys(ctx, ctxUser.ID)
  68. return
  69. }
  70. if ctxUser.IsOrganization() {
  71. org.Home(ctx)
  72. return
  73. }
  74. // Show OpenID URIs
  75. openIDs, err := models.GetUserOpenIDs(ctxUser.ID)
  76. if err != nil {
  77. ctx.ServerError("GetUserOpenIDs", err)
  78. return
  79. }
  80. // Show user badges
  81. badges, err := badge.GetUserBadges(ctxUser.ID, models.ListOptions{Page: 1, PageSize: 5})
  82. if err != nil {
  83. ctx.ServerError("GetUserBadges", err)
  84. return
  85. }
  86. ctx.Data["Title"] = ctxUser.DisplayName()
  87. ctx.Data["PageIsUserProfile"] = true
  88. ctx.Data["Owner"] = ctxUser
  89. ctx.Data["OpenIDs"] = openIDs
  90. ctx.Data["RecentBadges"] = badges
  91. ctx.Data["EnableHeatmap"] = setting.Service.EnableUserHeatmap
  92. ctx.Data["HeatmapUser"] = ctxUser.Name
  93. showPrivate := ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == ctxUser.ID)
  94. orgs, err := models.GetOrgsByUserID(ctxUser.ID, showPrivate)
  95. if err != nil {
  96. ctx.ServerError("GetOrgsByUserIDDesc", err)
  97. return
  98. }
  99. for _, org := range orgs {
  100. _, repoCount, err := models.SearchRepository(&models.SearchRepoOptions{
  101. OwnerID: org.ID,
  102. Private: ctx.IsSigned,
  103. Actor: ctx.User,
  104. })
  105. if err != nil {
  106. ctx.ServerError("SearchRepository", err)
  107. return
  108. }
  109. var opts = models.FindOrgMembersOpts{
  110. OrgID: org.ID,
  111. PublicOnly: true,
  112. }
  113. if ctx.User != nil {
  114. isMember, err := org.IsOrgMember(ctx.User.ID)
  115. if err != nil {
  116. ctx.Error(500, "IsOrgMember")
  117. return
  118. }
  119. opts.PublicOnly = !isMember && !ctx.User.IsAdmin
  120. }
  121. membersCount, err := models.CountOrgMembers(opts)
  122. if err != nil {
  123. ctx.ServerError("CountOrgMembers", err)
  124. return
  125. }
  126. org.NumMembers = int(membersCount)
  127. org.NumRepos = int(repoCount)
  128. }
  129. ctx.Data["Orgs"] = orgs
  130. ctx.Data["HasOrgsVisible"] = models.HasOrgsVisible(orgs, ctx.User)
  131. tab := ctx.Query("tab")
  132. if tab == "" {
  133. tab = "activity"
  134. }
  135. ctx.Data["TabName"] = tab
  136. page := ctx.QueryInt("page")
  137. if page <= 0 {
  138. page = 1
  139. }
  140. topicOnly := ctx.QueryBool("topic")
  141. var (
  142. repos []*models.Repository
  143. count int64
  144. total int
  145. orderBy models.SearchOrderBy
  146. )
  147. ctx.Data["SortType"] = ctx.Query("sort")
  148. switch ctx.Query("sort") {
  149. case "newest":
  150. orderBy = models.SearchOrderByNewest
  151. case "oldest":
  152. orderBy = models.SearchOrderByOldest
  153. case "recentupdate":
  154. orderBy = models.SearchOrderByRecentUpdated
  155. case "leastupdate":
  156. orderBy = models.SearchOrderByLeastUpdated
  157. case "reversealphabetically":
  158. orderBy = models.SearchOrderByAlphabeticallyReverse
  159. case "alphabetically":
  160. orderBy = models.SearchOrderByAlphabetically
  161. case "downloadtimes":
  162. orderBy = models.SearchOrderByDownloadTimes
  163. case "moststars":
  164. orderBy = models.SearchOrderByStarsReverse
  165. case "feweststars":
  166. orderBy = models.SearchOrderByStars
  167. case "mostforks":
  168. orderBy = models.SearchOrderByForksReverse
  169. case "fewestforks":
  170. orderBy = models.SearchOrderByForks
  171. default:
  172. ctx.Data["SortType"] = "recentupdate"
  173. orderBy = models.SearchOrderByRecentUpdated
  174. }
  175. keyword := strings.Trim(ctx.Query("q"), " ")
  176. ctx.Data["Keyword"] = keyword
  177. switch tab {
  178. case "followers":
  179. items, err := ctxUser.GetFollowers(models.ListOptions{
  180. PageSize: setting.UI.User.RepoPagingNum,
  181. Page: page,
  182. })
  183. if err != nil {
  184. ctx.ServerError("GetFollowers", err)
  185. return
  186. }
  187. ctx.Data["Cards"] = items
  188. total = ctxUser.NumFollowers
  189. case "following":
  190. items, err := ctxUser.GetFollowing(models.ListOptions{
  191. PageSize: setting.UI.User.RepoPagingNum,
  192. Page: page,
  193. })
  194. if err != nil {
  195. ctx.ServerError("GetFollowing", err)
  196. return
  197. }
  198. ctx.Data["Cards"] = items
  199. total = ctxUser.NumFollowing
  200. case "activity":
  201. retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser,
  202. Actor: ctx.User,
  203. IncludePrivate: showPrivate,
  204. OnlyPerformedBy: true,
  205. IncludeDeleted: false,
  206. })
  207. if ctx.Written() {
  208. return
  209. }
  210. case "stars":
  211. ctx.Data["PageIsProfileStarList"] = true
  212. repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
  213. ListOptions: models.ListOptions{
  214. PageSize: setting.UI.User.RepoPagingNum,
  215. Page: page,
  216. },
  217. Actor: ctx.User,
  218. Keyword: keyword,
  219. OrderBy: orderBy,
  220. Private: ctx.IsSigned,
  221. StarredByID: ctxUser.ID,
  222. Collaborate: util.OptionalBoolFalse,
  223. TopicOnly: topicOnly,
  224. IncludeDescription: setting.UI.SearchRepoDescription,
  225. })
  226. if err != nil {
  227. ctx.ServerError("SearchRepository", err)
  228. return
  229. }
  230. total = int(count)
  231. case "datasets":
  232. var isOwner = false
  233. if ctx.User != nil && ctx.User.ID == ctxUser.ID {
  234. isOwner = true
  235. }
  236. datasetSearchOptions := &models.SearchDatasetOptions{
  237. Keyword: keyword,
  238. OwnerID: ctxUser.ID,
  239. SearchOrderBy: orderBy,
  240. IsOwner: isOwner,
  241. ListOptions: models.ListOptions{
  242. Page: page,
  243. PageSize: setting.UI.User.RepoPagingNum,
  244. },
  245. CloudBrainType: -1,
  246. }
  247. if len(datasetSearchOptions.SearchOrderBy) == 0 {
  248. datasetSearchOptions.SearchOrderBy = models.SearchOrderByAlphabetically
  249. }
  250. datasets, count, err := models.SearchDataset(datasetSearchOptions)
  251. if err != nil {
  252. ctx.ServerError("SearchDatasets", err)
  253. }
  254. total = int(count)
  255. ctx.Data["Datasets"] = datasets
  256. case "repository":
  257. repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
  258. ListOptions: models.ListOptions{
  259. PageSize: setting.UI.User.RepoPagingNum,
  260. Page: page,
  261. },
  262. Actor: ctx.User,
  263. Keyword: keyword,
  264. OwnerID: ctxUser.ID,
  265. OrderBy: orderBy,
  266. Private: ctx.IsSigned,
  267. Collaborate: util.OptionalBoolFalse,
  268. TopicOnly: topicOnly,
  269. IncludeDescription: setting.UI.SearchRepoDescription,
  270. })
  271. if err != nil {
  272. ctx.ServerError("SearchRepository", err)
  273. return
  274. }
  275. total = int(count)
  276. case "badge":
  277. allBadges, err := badge.GetUserAllBadges(ctxUser.ID)
  278. if err != nil {
  279. ctx.ServerError("GetUserAllBadges", err)
  280. return
  281. }
  282. ctx.Data["AllBadges"] = allBadges
  283. default:
  284. ctx.ServerError("tab error", errors.New("tab error"))
  285. return
  286. }
  287. ctx.Data["Repos"] = repos
  288. ctx.Data["Total"] = total
  289. pager := context.NewPagination(total, setting.UI.User.RepoPagingNum, page, 5)
  290. pager.SetDefaultParams(ctx)
  291. ctx.Data["Page"] = pager
  292. ctx.Data["ShowUserEmail"] = len(ctxUser.Email) > 0 && ctx.IsSigned && (!ctxUser.KeepEmailPrivate || ctxUser.ID == ctx.User.ID)
  293. ctx.HTML(200, tplProfile)
  294. }
  295. // Action response for follow/unfollow user request
  296. func Action(ctx *context.Context) {
  297. u := GetUserByParams(ctx)
  298. if ctx.Written() {
  299. return
  300. }
  301. var err error
  302. switch ctx.Params(":action") {
  303. case "follow":
  304. err = models.FollowUser(ctx.User.ID, u.ID)
  305. case "unfollow":
  306. err = models.UnfollowUser(ctx.User.ID, u.ID)
  307. }
  308. if err != nil {
  309. ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
  310. return
  311. }
  312. ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink())
  313. }