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.

breaking.go 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package doctor
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/log"
  10. "xorm.io/builder"
  11. )
  12. func iterateUserAccounts(ctx context.Context, each func(*user.User) error) error {
  13. err := db.Iterate(
  14. ctx,
  15. builder.Gt{"id": 0},
  16. func(ctx context.Context, bean *user.User) error {
  17. return each(bean)
  18. },
  19. )
  20. return err
  21. }
  22. // Since 1.16.4 new restrictions has been set on email addresses. However users with invalid email
  23. // addresses would be currently facing a error due to their invalid email address.
  24. // Ref: https://github.com/go-gitea/gitea/pull/19085 & https://github.com/go-gitea/gitea/pull/17688
  25. func checkUserEmail(ctx context.Context, logger log.Logger, _ bool) error {
  26. // We could use quirky SQL to get all users that start without a [a-zA-Z0-9], but that would mean
  27. // DB provider-specific SQL and only works _now_. So instead we iterate through all user accounts
  28. // and use the user.ValidateEmail function to be future-proof.
  29. var invalidUserCount int64
  30. if err := iterateUserAccounts(ctx, func(u *user.User) error {
  31. // Only check for users, skip
  32. if u.Type != user.UserTypeIndividual {
  33. return nil
  34. }
  35. if err := user.ValidateEmail(u.Email); err != nil {
  36. invalidUserCount++
  37. logger.Warn("User[id=%d name=%q] have not a valid e-mail: %v", u.ID, u.Name, err)
  38. }
  39. return nil
  40. }); err != nil {
  41. return fmt.Errorf("iterateUserAccounts: %w", err)
  42. }
  43. if invalidUserCount == 0 {
  44. logger.Info("All users have a valid e-mail.")
  45. } else {
  46. logger.Warn("%d user(s) have a non-valid e-mail.", invalidUserCount)
  47. }
  48. return nil
  49. }
  50. // From time to time Gitea makes changes to the reserved usernames and which symbols
  51. // are allowed for various reasons. This check helps with detecting users that, according
  52. // to our reserved names, don't have a valid username.
  53. func checkUserName(ctx context.Context, logger log.Logger, _ bool) error {
  54. var invalidUserCount int64
  55. if err := iterateUserAccounts(ctx, func(u *user.User) error {
  56. if err := user.IsUsableUsername(u.Name); err != nil {
  57. invalidUserCount++
  58. logger.Warn("User[id=%d] does not have a valid username: %v", u.ID, err)
  59. }
  60. return nil
  61. }); err != nil {
  62. return fmt.Errorf("iterateUserAccounts: %w", err)
  63. }
  64. if invalidUserCount == 0 {
  65. logger.Info("All users have a valid username.")
  66. } else {
  67. logger.Warn("%d user(s) have a non-valid username.", invalidUserCount)
  68. }
  69. return nil
  70. }
  71. func init() {
  72. Register(&Check{
  73. Title: "Check if users has an valid email address",
  74. Name: "check-user-email",
  75. IsDefault: false,
  76. Run: checkUserEmail,
  77. Priority: 9,
  78. })
  79. Register(&Check{
  80. Title: "Check if users have a valid username",
  81. Name: "check-user-names",
  82. IsDefault: false,
  83. Run: checkUserName,
  84. Priority: 9,
  85. })
  86. }