home.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 user
  6. import (
  7. "bytes"
  8. "fmt"
  9. "sort"
  10. "strings"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/util"
  17. "github.com/Unknwon/com"
  18. "github.com/keybase/go-crypto/openpgp"
  19. "github.com/keybase/go-crypto/openpgp/armor"
  20. )
  21. const (
  22. tplDashboard base.TplName = "user/dashboard/dashboard"
  23. tplIssues base.TplName = "user/dashboard/issues"
  24. tplProfile base.TplName = "user/profile"
  25. tplOrgHome base.TplName = "org/home"
  26. )
  27. // getDashboardContextUser finds out dashboard is viewing as which context user.
  28. func getDashboardContextUser(ctx *context.Context) *models.User {
  29. ctxUser := ctx.User
  30. orgName := ctx.Params(":org")
  31. if len(orgName) > 0 {
  32. // Organization.
  33. org, err := models.GetUserByName(orgName)
  34. if err != nil {
  35. if models.IsErrUserNotExist(err) {
  36. ctx.NotFound("GetUserByName", err)
  37. } else {
  38. ctx.ServerError("GetUserByName", err)
  39. }
  40. return nil
  41. }
  42. ctxUser = org
  43. }
  44. ctx.Data["ContextUser"] = ctxUser
  45. if err := ctx.User.GetOrganizations(true); err != nil {
  46. ctx.ServerError("GetOrganizations", err)
  47. return nil
  48. }
  49. ctx.Data["Orgs"] = ctx.User.Orgs
  50. return ctxUser
  51. }
  52. // retrieveFeeds loads feeds for the specified user
  53. func retrieveFeeds(ctx *context.Context, options models.GetFeedsOptions) {
  54. actions, err := models.GetFeeds(options)
  55. if err != nil {
  56. ctx.ServerError("GetFeeds", err)
  57. return
  58. }
  59. userCache := map[int64]*models.User{options.RequestedUser.ID: options.RequestedUser}
  60. if ctx.User != nil {
  61. userCache[ctx.User.ID] = ctx.User
  62. }
  63. for _, act := range actions {
  64. if act.ActUser != nil {
  65. userCache[act.ActUserID] = act.ActUser
  66. }
  67. repoOwner, ok := userCache[act.Repo.OwnerID]
  68. if !ok {
  69. repoOwner, err = models.GetUserByID(act.Repo.OwnerID)
  70. if err != nil {
  71. if models.IsErrUserNotExist(err) {
  72. continue
  73. }
  74. ctx.ServerError("GetUserByID", err)
  75. return
  76. }
  77. userCache[repoOwner.ID] = repoOwner
  78. }
  79. act.Repo.Owner = repoOwner
  80. }
  81. ctx.Data["Feeds"] = actions
  82. }
  83. // Dashboard render the dashborad page
  84. func Dashboard(ctx *context.Context) {
  85. ctxUser := getDashboardContextUser(ctx)
  86. if ctx.Written() {
  87. return
  88. }
  89. ctx.Data["Title"] = ctxUser.DisplayName() + " - " + ctx.Tr("dashboard")
  90. ctx.Data["PageIsDashboard"] = true
  91. ctx.Data["PageIsNews"] = true
  92. ctx.Data["SearchLimit"] = setting.UI.User.RepoPagingNum
  93. ctx.Data["EnableHeatmap"] = setting.Service.EnableUserHeatmap
  94. ctx.Data["HeatmapUser"] = ctxUser.Name
  95. var err error
  96. var mirrors []*models.Repository
  97. if ctxUser.IsOrganization() {
  98. env, err := ctxUser.AccessibleReposEnv(ctx.User.ID)
  99. if err != nil {
  100. ctx.ServerError("AccessibleReposEnv", err)
  101. return
  102. }
  103. mirrors, err = env.MirrorRepos()
  104. if err != nil {
  105. ctx.ServerError("env.MirrorRepos", err)
  106. return
  107. }
  108. } else {
  109. mirrors, err = ctxUser.GetMirrorRepositories()
  110. if err != nil {
  111. ctx.ServerError("GetMirrorRepositories", err)
  112. return
  113. }
  114. }
  115. ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum
  116. if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
  117. ctx.ServerError("MirrorRepositoryList.LoadAttributes", err)
  118. return
  119. }
  120. ctx.Data["MirrorCount"] = len(mirrors)
  121. ctx.Data["Mirrors"] = mirrors
  122. retrieveFeeds(ctx, models.GetFeedsOptions{
  123. RequestedUser: ctxUser,
  124. IncludePrivate: true,
  125. OnlyPerformedBy: false,
  126. IncludeDeleted: false,
  127. })
  128. if ctx.Written() {
  129. return
  130. }
  131. ctx.HTML(200, tplDashboard)
  132. }
  133. // Issues render the user issues page
  134. func Issues(ctx *context.Context) {
  135. isPullList := ctx.Params(":type") == "pulls"
  136. if isPullList {
  137. ctx.Data["Title"] = ctx.Tr("pull_requests")
  138. ctx.Data["PageIsPulls"] = true
  139. } else {
  140. ctx.Data["Title"] = ctx.Tr("issues")
  141. ctx.Data["PageIsIssues"] = true
  142. }
  143. ctxUser := getDashboardContextUser(ctx)
  144. if ctx.Written() {
  145. return
  146. }
  147. // Organization does not have view type and filter mode.
  148. var (
  149. viewType string
  150. sortType = ctx.Query("sort")
  151. filterMode = models.FilterModeAll
  152. )
  153. if ctxUser.IsOrganization() {
  154. viewType = "all"
  155. } else {
  156. viewType = ctx.Query("type")
  157. switch viewType {
  158. case "assigned":
  159. filterMode = models.FilterModeAssign
  160. case "created_by":
  161. filterMode = models.FilterModeCreate
  162. case "all": // filterMode already set to All
  163. default:
  164. viewType = "all"
  165. }
  166. }
  167. page := ctx.QueryInt("page")
  168. if page <= 1 {
  169. page = 1
  170. }
  171. repoID := ctx.QueryInt64("repo")
  172. isShowClosed := ctx.Query("state") == "closed"
  173. // Get repositories.
  174. var err error
  175. var userRepoIDs []int64
  176. if ctxUser.IsOrganization() {
  177. env, err := ctxUser.AccessibleReposEnv(ctx.User.ID)
  178. if err != nil {
  179. ctx.ServerError("AccessibleReposEnv", err)
  180. return
  181. }
  182. userRepoIDs, err = env.RepoIDs(1, ctxUser.NumRepos)
  183. if err != nil {
  184. ctx.ServerError("env.RepoIDs", err)
  185. return
  186. }
  187. } else {
  188. unitType := models.UnitTypeIssues
  189. if isPullList {
  190. unitType = models.UnitTypePullRequests
  191. }
  192. userRepoIDs, err = ctxUser.GetAccessRepoIDs(unitType)
  193. if err != nil {
  194. ctx.ServerError("ctxUser.GetAccessRepoIDs", err)
  195. return
  196. }
  197. }
  198. if len(userRepoIDs) == 0 {
  199. userRepoIDs = []int64{-1}
  200. }
  201. opts := &models.IssuesOptions{
  202. IsClosed: util.OptionalBoolOf(isShowClosed),
  203. IsPull: util.OptionalBoolOf(isPullList),
  204. SortType: sortType,
  205. }
  206. if repoID > 0 {
  207. opts.RepoIDs = []int64{repoID}
  208. }
  209. switch filterMode {
  210. case models.FilterModeAll:
  211. if repoID > 0 {
  212. if !com.IsSliceContainsInt64(userRepoIDs, repoID) {
  213. // force an empty result
  214. opts.RepoIDs = []int64{-1}
  215. }
  216. } else {
  217. opts.RepoIDs = userRepoIDs
  218. }
  219. case models.FilterModeAssign:
  220. opts.AssigneeID = ctxUser.ID
  221. case models.FilterModeCreate:
  222. opts.PosterID = ctxUser.ID
  223. case models.FilterModeMention:
  224. opts.MentionedID = ctxUser.ID
  225. }
  226. counts, err := models.CountIssuesByRepo(opts)
  227. if err != nil {
  228. ctx.ServerError("CountIssuesByRepo", err)
  229. return
  230. }
  231. opts.Page = page
  232. opts.PageSize = setting.UI.IssuePagingNum
  233. var labelIDs []int64
  234. selectLabels := ctx.Query("labels")
  235. if len(selectLabels) > 0 && selectLabels != "0" {
  236. labelIDs, err = base.StringsToInt64s(strings.Split(selectLabels, ","))
  237. if err != nil {
  238. ctx.ServerError("StringsToInt64s", err)
  239. return
  240. }
  241. }
  242. opts.LabelIDs = labelIDs
  243. issues, err := models.Issues(opts)
  244. if err != nil {
  245. ctx.ServerError("Issues", err)
  246. return
  247. }
  248. showReposMap := make(map[int64]*models.Repository, len(counts))
  249. for repoID := range counts {
  250. repo, err := models.GetRepositoryByID(repoID)
  251. if err != nil {
  252. ctx.ServerError("GetRepositoryByID", err)
  253. return
  254. }
  255. showReposMap[repoID] = repo
  256. }
  257. if repoID > 0 {
  258. if _, ok := showReposMap[repoID]; !ok {
  259. repo, err := models.GetRepositoryByID(repoID)
  260. if models.IsErrRepoNotExist(err) {
  261. ctx.NotFound("GetRepositoryByID", err)
  262. return
  263. } else if err != nil {
  264. ctx.ServerError("GetRepositoryByID", fmt.Errorf("[%d]%v", repoID, err))
  265. return
  266. }
  267. showReposMap[repoID] = repo
  268. }
  269. repo := showReposMap[repoID]
  270. // Check if user has access to given repository.
  271. perm, err := models.GetUserRepoPermission(repo, ctxUser)
  272. if err != nil {
  273. ctx.ServerError("GetUserRepoPermission", fmt.Errorf("[%d]%v", repoID, err))
  274. return
  275. }
  276. if !perm.CanRead(models.UnitTypeIssues) {
  277. if log.IsTrace() {
  278. log.Trace("Permission Denied: User %-v cannot read %-v of repo %-v\n"+
  279. "User in repo has Permissions: %-+v",
  280. ctxUser,
  281. models.UnitTypeIssues,
  282. repo,
  283. perm)
  284. }
  285. ctx.Status(404)
  286. return
  287. }
  288. }
  289. showRepos := models.RepositoryListOfMap(showReposMap)
  290. sort.Sort(showRepos)
  291. if err = showRepos.LoadAttributes(); err != nil {
  292. ctx.ServerError("LoadAttributes", err)
  293. return
  294. }
  295. var commitStatus = make(map[int64]*models.CommitStatus, len(issues))
  296. for _, issue := range issues {
  297. issue.Repo = showReposMap[issue.RepoID]
  298. if isPullList {
  299. commitStatus[issue.PullRequest.ID], _ = issue.PullRequest.GetLastCommitStatus()
  300. }
  301. }
  302. issueStats, err := models.GetUserIssueStats(models.UserIssueStatsOptions{
  303. UserID: ctxUser.ID,
  304. RepoID: repoID,
  305. UserRepoIDs: userRepoIDs,
  306. FilterMode: filterMode,
  307. IsPull: isPullList,
  308. IsClosed: isShowClosed,
  309. })
  310. if err != nil {
  311. ctx.ServerError("GetUserIssueStats", err)
  312. return
  313. }
  314. var total int
  315. if !isShowClosed {
  316. total = int(issueStats.OpenCount)
  317. } else {
  318. total = int(issueStats.ClosedCount)
  319. }
  320. ctx.Data["Issues"] = issues
  321. ctx.Data["CommitStatus"] = commitStatus
  322. ctx.Data["Repos"] = showRepos
  323. ctx.Data["Counts"] = counts
  324. ctx.Data["IssueStats"] = issueStats
  325. ctx.Data["ViewType"] = viewType
  326. ctx.Data["SortType"] = sortType
  327. ctx.Data["RepoID"] = repoID
  328. ctx.Data["IsShowClosed"] = isShowClosed
  329. if isShowClosed {
  330. ctx.Data["State"] = "closed"
  331. } else {
  332. ctx.Data["State"] = "open"
  333. }
  334. pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, 5)
  335. pager.AddParam(ctx, "type", "ViewType")
  336. pager.AddParam(ctx, "repo", "RepoID")
  337. pager.AddParam(ctx, "sort", "SortType")
  338. pager.AddParam(ctx, "state", "State")
  339. pager.AddParam(ctx, "labels", "SelectLabels")
  340. pager.AddParam(ctx, "milestone", "MilestoneID")
  341. pager.AddParam(ctx, "assignee", "AssigneeID")
  342. ctx.Data["Page"] = pager
  343. ctx.HTML(200, tplIssues)
  344. }
  345. // ShowSSHKeys output all the ssh keys of user by uid
  346. func ShowSSHKeys(ctx *context.Context, uid int64) {
  347. keys, err := models.ListPublicKeys(uid)
  348. if err != nil {
  349. ctx.ServerError("ListPublicKeys", err)
  350. return
  351. }
  352. var buf bytes.Buffer
  353. for i := range keys {
  354. buf.WriteString(keys[i].OmitEmail())
  355. buf.WriteString("\n")
  356. }
  357. ctx.PlainText(200, buf.Bytes())
  358. }
  359. // ShowGPGKeys output all the public GPG keys of user by uid
  360. func ShowGPGKeys(ctx *context.Context, uid int64) {
  361. keys, err := models.ListGPGKeys(uid)
  362. if err != nil {
  363. ctx.ServerError("ListGPGKeys", err)
  364. return
  365. }
  366. entities := make([]*openpgp.Entity, 0)
  367. failedEntitiesID := make([]string, 0)
  368. for _, k := range keys {
  369. e, err := models.GPGKeyToEntity(k)
  370. if err != nil {
  371. if models.IsErrGPGKeyImportNotExist(err) {
  372. failedEntitiesID = append(failedEntitiesID, k.KeyID)
  373. continue //Skip previous import without backup of imported armored key
  374. }
  375. ctx.ServerError("ShowGPGKeys", err)
  376. return
  377. }
  378. entities = append(entities, e)
  379. }
  380. var buf bytes.Buffer
  381. headers := make(map[string]string)
  382. if len(failedEntitiesID) > 0 { //If some key need re-import to be exported
  383. headers["Note"] = fmt.Sprintf("The keys with the following IDs couldn't be exported and need to be reuploaded %s", strings.Join(failedEntitiesID, ", "))
  384. }
  385. writer, _ := armor.Encode(&buf, "PGP PUBLIC KEY BLOCK", headers)
  386. for _, e := range entities {
  387. err = e.Serialize(writer) //TODO find why key are exported with a different cipherTypeByte as original (should not be blocking but strange)
  388. if err != nil {
  389. ctx.ServerError("ShowGPGKeys", err)
  390. return
  391. }
  392. }
  393. writer.Close()
  394. ctx.PlainText(200, buf.Bytes())
  395. }
  396. func showOrgProfile(ctx *context.Context) {
  397. ctx.SetParams(":org", ctx.Params(":username"))
  398. context.HandleOrgAssignment(ctx)
  399. if ctx.Written() {
  400. return
  401. }
  402. org := ctx.Org.Organization
  403. if !models.HasOrgVisible(org, ctx.User) {
  404. ctx.NotFound("HasOrgVisible", nil)
  405. return
  406. }
  407. ctx.Data["Title"] = org.DisplayName()
  408. var orderBy models.SearchOrderBy
  409. ctx.Data["SortType"] = ctx.Query("sort")
  410. switch ctx.Query("sort") {
  411. case "newest":
  412. orderBy = models.SearchOrderByNewest
  413. case "oldest":
  414. orderBy = models.SearchOrderByOldest
  415. case "recentupdate":
  416. orderBy = models.SearchOrderByRecentUpdated
  417. case "leastupdate":
  418. orderBy = models.SearchOrderByLeastUpdated
  419. case "reversealphabetically":
  420. orderBy = models.SearchOrderByAlphabeticallyReverse
  421. case "alphabetically":
  422. orderBy = models.SearchOrderByAlphabetically
  423. case "moststars":
  424. orderBy = models.SearchOrderByStarsReverse
  425. case "feweststars":
  426. orderBy = models.SearchOrderByStars
  427. case "mostforks":
  428. orderBy = models.SearchOrderByForksReverse
  429. case "fewestforks":
  430. orderBy = models.SearchOrderByForks
  431. default:
  432. ctx.Data["SortType"] = "recentupdate"
  433. orderBy = models.SearchOrderByRecentUpdated
  434. }
  435. keyword := strings.Trim(ctx.Query("q"), " ")
  436. ctx.Data["Keyword"] = keyword
  437. page := ctx.QueryInt("page")
  438. if page <= 0 {
  439. page = 1
  440. }
  441. var (
  442. repos []*models.Repository
  443. count int64
  444. err error
  445. )
  446. repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
  447. Keyword: keyword,
  448. OwnerID: org.ID,
  449. OrderBy: orderBy,
  450. Private: ctx.IsSigned,
  451. UserIsAdmin: ctx.IsUserSiteAdmin(),
  452. UserID: ctx.Data["SignedUserID"].(int64),
  453. Page: page,
  454. IsProfile: true,
  455. PageSize: setting.UI.User.RepoPagingNum,
  456. })
  457. if err != nil {
  458. ctx.ServerError("SearchRepositoryByName", err)
  459. return
  460. }
  461. if err := org.GetMembers(); err != nil {
  462. ctx.ServerError("GetMembers", err)
  463. return
  464. }
  465. ctx.Data["Repos"] = repos
  466. ctx.Data["Total"] = count
  467. ctx.Data["Members"] = org.Members
  468. ctx.Data["Teams"] = org.Teams
  469. pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5)
  470. pager.SetDefaultParams(ctx)
  471. ctx.Data["Page"] = pager
  472. ctx.HTML(200, tplOrgHome)
  473. }
  474. // Email2User show user page via email
  475. func Email2User(ctx *context.Context) {
  476. u, err := models.GetUserByEmail(ctx.Query("email"))
  477. if err != nil {
  478. if models.IsErrUserNotExist(err) {
  479. ctx.NotFound("GetUserByEmail", err)
  480. } else {
  481. ctx.ServerError("GetUserByEmail", err)
  482. }
  483. return
  484. }
  485. ctx.Redirect(setting.AppSubURL + "/user/" + u.Name)
  486. }