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.

home.go 11 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 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
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
11 years ago
11 years ago
11 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
9 years ago
11 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
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
9 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // Copyright 2014 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 routers
  6. import (
  7. "bytes"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. code_indexer "code.gitea.io/gitea/modules/indexer/code"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/structs"
  16. "code.gitea.io/gitea/modules/util"
  17. "code.gitea.io/gitea/routers/user"
  18. )
  19. const (
  20. // tplHome home page template
  21. tplHome base.TplName = "home"
  22. // tplExploreRepos explore repositories page template
  23. tplExploreRepos base.TplName = "explore/repos"
  24. // tplExploreUsers explore users page template
  25. tplExploreUsers base.TplName = "explore/users"
  26. // tplExploreOrganizations explore organizations page template
  27. tplExploreOrganizations base.TplName = "explore/organizations"
  28. // tplExploreCode explore code page template
  29. tplExploreCode base.TplName = "explore/code"
  30. )
  31. // Home render home page
  32. func Home(ctx *context.Context) {
  33. if ctx.IsSigned {
  34. if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  35. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  36. ctx.HTML(200, user.TplActivate)
  37. } else if !ctx.User.IsActive || ctx.User.ProhibitLogin {
  38. log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
  39. ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
  40. ctx.HTML(200, "user/auth/prohibit_login")
  41. } else if ctx.User.MustChangePassword {
  42. ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
  43. ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password"
  44. ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL)
  45. ctx.Redirect(setting.AppSubURL + "/user/settings/change_password")
  46. } else {
  47. user.Dashboard(ctx)
  48. }
  49. return
  50. // Check non-logged users landing page.
  51. } else if setting.LandingPageURL != setting.LandingPageHome {
  52. ctx.Redirect(setting.AppSubURL + string(setting.LandingPageURL))
  53. return
  54. }
  55. // Check auto-login.
  56. uname := ctx.GetCookie(setting.CookieUserName)
  57. if len(uname) != 0 {
  58. ctx.Redirect(setting.AppSubURL + "/user/login")
  59. return
  60. }
  61. ctx.Data["PageIsHome"] = true
  62. ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
  63. ctx.HTML(200, tplHome)
  64. }
  65. // RepoSearchOptions when calling search repositories
  66. type RepoSearchOptions struct {
  67. OwnerID int64
  68. Private bool
  69. Restricted bool
  70. PageSize int
  71. TplName base.TplName
  72. }
  73. var (
  74. nullByte = []byte{0x00}
  75. )
  76. func isKeywordValid(keyword string) bool {
  77. return !bytes.Contains([]byte(keyword), nullByte)
  78. }
  79. // RenderRepoSearch render repositories search page
  80. func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
  81. page := ctx.QueryInt("page")
  82. if page <= 0 {
  83. page = 1
  84. }
  85. var (
  86. repos []*models.Repository
  87. count int64
  88. err error
  89. orderBy models.SearchOrderBy
  90. )
  91. ctx.Data["SortType"] = ctx.Query("sort")
  92. switch ctx.Query("sort") {
  93. case "newest":
  94. orderBy = models.SearchOrderByNewest
  95. case "oldest":
  96. orderBy = models.SearchOrderByOldest
  97. case "recentupdate":
  98. orderBy = models.SearchOrderByRecentUpdated
  99. case "leastupdate":
  100. orderBy = models.SearchOrderByLeastUpdated
  101. case "reversealphabetically":
  102. orderBy = models.SearchOrderByAlphabeticallyReverse
  103. case "alphabetically":
  104. orderBy = models.SearchOrderByAlphabetically
  105. case "reversesize":
  106. orderBy = models.SearchOrderBySizeReverse
  107. case "size":
  108. orderBy = models.SearchOrderBySize
  109. case "moststars":
  110. orderBy = models.SearchOrderByStarsReverse
  111. case "feweststars":
  112. orderBy = models.SearchOrderByStars
  113. case "mostforks":
  114. orderBy = models.SearchOrderByForksReverse
  115. case "fewestforks":
  116. orderBy = models.SearchOrderByForks
  117. default:
  118. ctx.Data["SortType"] = "recentupdate"
  119. orderBy = models.SearchOrderByRecentUpdated
  120. }
  121. keyword := strings.Trim(ctx.Query("q"), " ")
  122. topicOnly := ctx.QueryBool("topic")
  123. ctx.Data["TopicOnly"] = topicOnly
  124. repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
  125. Actor: ctx.User,
  126. Page: page,
  127. PageSize: opts.PageSize,
  128. OrderBy: orderBy,
  129. Private: opts.Private,
  130. Keyword: keyword,
  131. OwnerID: opts.OwnerID,
  132. AllPublic: true,
  133. AllLimited: true,
  134. TopicOnly: topicOnly,
  135. IncludeDescription: setting.UI.SearchRepoDescription,
  136. })
  137. if err != nil {
  138. ctx.ServerError("SearchRepository", err)
  139. return
  140. }
  141. ctx.Data["Keyword"] = keyword
  142. ctx.Data["Total"] = count
  143. ctx.Data["Repos"] = repos
  144. ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
  145. pager := context.NewPagination(int(count), opts.PageSize, page, 5)
  146. pager.SetDefaultParams(ctx)
  147. pager.AddParam(ctx, "topic", "TopicOnly")
  148. ctx.Data["Page"] = pager
  149. ctx.HTML(200, opts.TplName)
  150. }
  151. // ExploreRepos render explore repositories page
  152. func ExploreRepos(ctx *context.Context) {
  153. ctx.Data["Title"] = ctx.Tr("explore")
  154. ctx.Data["PageIsExplore"] = true
  155. ctx.Data["PageIsExploreRepositories"] = true
  156. ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
  157. var ownerID int64
  158. if ctx.User != nil && !ctx.User.IsAdmin {
  159. ownerID = ctx.User.ID
  160. }
  161. RenderRepoSearch(ctx, &RepoSearchOptions{
  162. PageSize: setting.UI.ExplorePagingNum,
  163. OwnerID: ownerID,
  164. Private: ctx.User != nil,
  165. TplName: tplExploreRepos,
  166. })
  167. }
  168. // RenderUserSearch render user search page
  169. func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplName base.TplName) {
  170. opts.Page = ctx.QueryInt("page")
  171. if opts.Page <= 1 {
  172. opts.Page = 1
  173. }
  174. opts.Actor = ctx.User
  175. var (
  176. users []*models.User
  177. count int64
  178. err error
  179. orderBy models.SearchOrderBy
  180. )
  181. ctx.Data["SortType"] = ctx.Query("sort")
  182. switch ctx.Query("sort") {
  183. case "newest":
  184. orderBy = models.SearchOrderByIDReverse
  185. case "oldest":
  186. orderBy = models.SearchOrderByID
  187. case "recentupdate":
  188. orderBy = models.SearchOrderByRecentUpdated
  189. case "leastupdate":
  190. orderBy = models.SearchOrderByLeastUpdated
  191. case "reversealphabetically":
  192. orderBy = models.SearchOrderByAlphabeticallyReverse
  193. case "alphabetically":
  194. orderBy = models.SearchOrderByAlphabetically
  195. default:
  196. ctx.Data["SortType"] = "alphabetically"
  197. orderBy = models.SearchOrderByAlphabetically
  198. }
  199. opts.Keyword = strings.Trim(ctx.Query("q"), " ")
  200. opts.OrderBy = orderBy
  201. if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
  202. users, count, err = models.SearchUsers(opts)
  203. if err != nil {
  204. ctx.ServerError("SearchUsers", err)
  205. return
  206. }
  207. }
  208. ctx.Data["Keyword"] = opts.Keyword
  209. ctx.Data["Total"] = count
  210. ctx.Data["Users"] = users
  211. ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
  212. ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
  213. pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
  214. pager.SetDefaultParams(ctx)
  215. ctx.Data["Page"] = pager
  216. ctx.HTML(200, tplName)
  217. }
  218. // ExploreUsers render explore users page
  219. func ExploreUsers(ctx *context.Context) {
  220. ctx.Data["Title"] = ctx.Tr("explore")
  221. ctx.Data["PageIsExplore"] = true
  222. ctx.Data["PageIsExploreUsers"] = true
  223. ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
  224. RenderUserSearch(ctx, &models.SearchUserOptions{
  225. Type: models.UserTypeIndividual,
  226. PageSize: setting.UI.ExplorePagingNum,
  227. IsActive: util.OptionalBoolTrue,
  228. Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate},
  229. }, tplExploreUsers)
  230. }
  231. // ExploreOrganizations render explore organizations page
  232. func ExploreOrganizations(ctx *context.Context) {
  233. ctx.Data["Title"] = ctx.Tr("explore")
  234. ctx.Data["PageIsExplore"] = true
  235. ctx.Data["PageIsExploreOrganizations"] = true
  236. ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
  237. visibleTypes := []structs.VisibleType{structs.VisibleTypePublic}
  238. if ctx.User != nil {
  239. visibleTypes = append(visibleTypes, structs.VisibleTypeLimited, structs.VisibleTypePrivate)
  240. }
  241. RenderUserSearch(ctx, &models.SearchUserOptions{
  242. Type: models.UserTypeOrganization,
  243. PageSize: setting.UI.ExplorePagingNum,
  244. Visible: visibleTypes,
  245. }, tplExploreOrganizations)
  246. }
  247. // ExploreCode render explore code page
  248. func ExploreCode(ctx *context.Context) {
  249. if !setting.Indexer.RepoIndexerEnabled {
  250. ctx.Redirect(setting.AppSubURL+"/explore", 302)
  251. return
  252. }
  253. ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
  254. ctx.Data["Title"] = ctx.Tr("explore")
  255. ctx.Data["PageIsExplore"] = true
  256. ctx.Data["PageIsExploreCode"] = true
  257. keyword := strings.TrimSpace(ctx.Query("q"))
  258. page := ctx.QueryInt("page")
  259. if page <= 0 {
  260. page = 1
  261. }
  262. var (
  263. repoIDs []int64
  264. err error
  265. isAdmin bool
  266. userID int64
  267. )
  268. if ctx.User != nil {
  269. userID = ctx.User.ID
  270. isAdmin = ctx.User.IsAdmin
  271. }
  272. // guest user or non-admin user
  273. if ctx.User == nil || !isAdmin {
  274. repoIDs, err = models.FindUserAccessibleRepoIDs(ctx.User)
  275. if err != nil {
  276. ctx.ServerError("SearchResults", err)
  277. return
  278. }
  279. }
  280. var (
  281. total int
  282. searchResults []*code_indexer.Result
  283. )
  284. // if non-admin login user, we need check UnitTypeCode at first
  285. if ctx.User != nil && len(repoIDs) > 0 {
  286. repoMaps, err := models.GetRepositoriesMapByIDs(repoIDs)
  287. if err != nil {
  288. ctx.ServerError("SearchResults", err)
  289. return
  290. }
  291. var rightRepoMap = make(map[int64]*models.Repository, len(repoMaps))
  292. repoIDs = make([]int64, 0, len(repoMaps))
  293. for id, repo := range repoMaps {
  294. if repo.CheckUnitUser(userID, isAdmin, models.UnitTypeCode) {
  295. rightRepoMap[id] = repo
  296. repoIDs = append(repoIDs, id)
  297. }
  298. }
  299. ctx.Data["RepoMaps"] = rightRepoMap
  300. total, searchResults, err = code_indexer.PerformSearch(repoIDs, keyword, page, setting.UI.RepoSearchPagingNum)
  301. if err != nil {
  302. ctx.ServerError("SearchResults", err)
  303. return
  304. }
  305. // if non-login user or isAdmin, no need to check UnitTypeCode
  306. } else if (ctx.User == nil && len(repoIDs) > 0) || isAdmin {
  307. total, searchResults, err = code_indexer.PerformSearch(repoIDs, keyword, page, setting.UI.RepoSearchPagingNum)
  308. if err != nil {
  309. ctx.ServerError("SearchResults", err)
  310. return
  311. }
  312. var loadRepoIDs = make([]int64, 0, len(searchResults))
  313. for _, result := range searchResults {
  314. var find bool
  315. for _, id := range loadRepoIDs {
  316. if id == result.RepoID {
  317. find = true
  318. break
  319. }
  320. }
  321. if !find {
  322. loadRepoIDs = append(loadRepoIDs, result.RepoID)
  323. }
  324. }
  325. repoMaps, err := models.GetRepositoriesMapByIDs(loadRepoIDs)
  326. if err != nil {
  327. ctx.ServerError("SearchResults", err)
  328. return
  329. }
  330. ctx.Data["RepoMaps"] = repoMaps
  331. }
  332. ctx.Data["Keyword"] = keyword
  333. ctx.Data["SearchResults"] = searchResults
  334. ctx.Data["RequireHighlightJS"] = true
  335. ctx.Data["PageIsViewCode"] = true
  336. pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
  337. pager.SetDefaultParams(ctx)
  338. ctx.Data["Page"] = pager
  339. ctx.HTML(200, tplExploreCode)
  340. }
  341. // NotFound render 404 page
  342. func NotFound(ctx *context.Context) {
  343. ctx.Data["Title"] = "Page Not Found"
  344. ctx.NotFound("home.NotFound", nil)
  345. }