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.

157 lines
4.1 KiB

  1. // Copyright 2020 The Gitea Authors.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package admin
  5. import (
  6. "bytes"
  7. "net/url"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/util"
  14. "github.com/unknwon/com"
  15. )
  16. const (
  17. tplEmails base.TplName = "admin/emails/list"
  18. )
  19. // Emails show all emails
  20. func Emails(ctx *context.Context) {
  21. ctx.Data["Title"] = ctx.Tr("admin.emails")
  22. ctx.Data["PageIsAdmin"] = true
  23. ctx.Data["PageIsAdminEmails"] = true
  24. opts := &models.SearchEmailOptions{
  25. ListOptions: models.ListOptions{
  26. PageSize: setting.UI.Admin.UserPagingNum,
  27. Page: ctx.QueryInt("page"),
  28. },
  29. }
  30. if opts.Page <= 1 {
  31. opts.Page = 1
  32. }
  33. type ActiveEmail struct {
  34. models.SearchEmailResult
  35. CanChange bool
  36. }
  37. var (
  38. baseEmails []*models.SearchEmailResult
  39. emails []ActiveEmail
  40. count int64
  41. err error
  42. orderBy models.SearchEmailOrderBy
  43. )
  44. ctx.Data["SortType"] = ctx.Query("sort")
  45. switch ctx.Query("sort") {
  46. case "email":
  47. orderBy = models.SearchEmailOrderByEmail
  48. case "reverseemail":
  49. orderBy = models.SearchEmailOrderByEmailReverse
  50. case "username":
  51. orderBy = models.SearchEmailOrderByName
  52. case "reverseusername":
  53. orderBy = models.SearchEmailOrderByNameReverse
  54. default:
  55. ctx.Data["SortType"] = "email"
  56. orderBy = models.SearchEmailOrderByEmail
  57. }
  58. opts.Keyword = ctx.QueryTrim("q")
  59. opts.SortType = orderBy
  60. if len(ctx.Query("is_activated")) != 0 {
  61. opts.IsActivated = util.OptionalBoolOf(ctx.QueryBool("activated"))
  62. }
  63. if len(ctx.Query("is_primary")) != 0 {
  64. opts.IsPrimary = util.OptionalBoolOf(ctx.QueryBool("primary"))
  65. }
  66. if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
  67. baseEmails, count, err = models.SearchEmails(opts)
  68. if err != nil {
  69. ctx.ServerError("SearchEmails", err)
  70. return
  71. }
  72. emails = make([]ActiveEmail, len(baseEmails))
  73. for i := range baseEmails {
  74. emails[i].SearchEmailResult = *baseEmails[i]
  75. // Don't let the admin deactivate its own primary email address
  76. // We already know the user is admin
  77. emails[i].CanChange = ctx.User.ID != emails[i].UID || !emails[i].IsPrimary
  78. }
  79. }
  80. ctx.Data["Keyword"] = opts.Keyword
  81. ctx.Data["Total"] = count
  82. ctx.Data["Emails"] = emails
  83. pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
  84. pager.SetDefaultParams(ctx)
  85. ctx.Data["Page"] = pager
  86. ctx.HTML(200, tplEmails)
  87. }
  88. var (
  89. nullByte = []byte{0x00}
  90. )
  91. func isKeywordValid(keyword string) bool {
  92. return !bytes.Contains([]byte(keyword), nullByte)
  93. }
  94. // ActivateEmail serves a POST request for activating/deactivating a user's email
  95. func ActivateEmail(ctx *context.Context) {
  96. truefalse := map[string]bool{"1": true, "0": false}
  97. uid := com.StrTo(ctx.Query("uid")).MustInt64()
  98. email := ctx.Query("email")
  99. primary, okp := truefalse[ctx.Query("primary")]
  100. activate, oka := truefalse[ctx.Query("activate")]
  101. if uid == 0 || len(email) == 0 || !okp || !oka {
  102. ctx.Error(400)
  103. return
  104. }
  105. log.Info("Changing activation for User ID: %d, email: %s, primary: %v to %v", uid, email, primary, activate)
  106. if err := models.ActivateUserEmail(uid, email, primary, activate); err != nil {
  107. log.Error("ActivateUserEmail(%v,%v,%v,%v): %v", uid, email, primary, activate, err)
  108. if models.IsErrEmailAlreadyUsed(err) {
  109. ctx.Flash.Error(ctx.Tr("admin.emails.duplicate_active"))
  110. } else {
  111. ctx.Flash.Error(ctx.Tr("admin.emails.not_updated", err))
  112. }
  113. } else {
  114. log.Info("Activation for User ID: %d, email: %s, primary: %v changed to %v", uid, email, primary, activate)
  115. ctx.Flash.Info(ctx.Tr("admin.emails.updated"))
  116. }
  117. redirect, _ := url.Parse(setting.AppSubURL + "/admin/emails")
  118. q := url.Values{}
  119. if val := ctx.QueryTrim("q"); len(val) > 0 {
  120. q.Set("q", val)
  121. }
  122. if val := ctx.QueryTrim("sort"); len(val) > 0 {
  123. q.Set("sort", val)
  124. }
  125. if val := ctx.QueryTrim("is_primary"); len(val) > 0 {
  126. q.Set("is_primary", val)
  127. }
  128. if val := ctx.QueryTrim("is_activated"); len(val) > 0 {
  129. q.Set("is_activated", val)
  130. }
  131. redirect.RawQuery = q.Encode()
  132. ctx.Redirect(redirect.String())
  133. }