gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package user
  4. import (
  5. "context"
  6. "fmt"
  7. "time"
  8. _ "image/jpeg" // Needed for jpeg support
  9. actions_model "code.gitea.io/gitea/models/actions"
  10. activities_model "code.gitea.io/gitea/models/activities"
  11. asymkey_model "code.gitea.io/gitea/models/asymkey"
  12. auth_model "code.gitea.io/gitea/models/auth"
  13. "code.gitea.io/gitea/models/db"
  14. git_model "code.gitea.io/gitea/models/git"
  15. issues_model "code.gitea.io/gitea/models/issues"
  16. "code.gitea.io/gitea/models/organization"
  17. access_model "code.gitea.io/gitea/models/perm/access"
  18. pull_model "code.gitea.io/gitea/models/pull"
  19. repo_model "code.gitea.io/gitea/models/repo"
  20. user_model "code.gitea.io/gitea/models/user"
  21. "code.gitea.io/gitea/modules/setting"
  22. "xorm.io/builder"
  23. )
  24. // deleteUser deletes models associated to an user.
  25. func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error) {
  26. e := db.GetEngine(ctx)
  27. // ***** START: Watch *****
  28. watchedRepoIDs, err := db.FindIDs(ctx, "watch", "watch.repo_id",
  29. builder.Eq{"watch.user_id": u.ID}.
  30. And(builder.Neq{"watch.mode": repo_model.WatchModeDont}))
  31. if err != nil {
  32. return fmt.Errorf("get all watches: %w", err)
  33. }
  34. if err = db.DecrByIDs(ctx, watchedRepoIDs, "num_watches", new(repo_model.Repository)); err != nil {
  35. return fmt.Errorf("decrease repository num_watches: %w", err)
  36. }
  37. // ***** END: Watch *****
  38. // ***** START: Star *****
  39. starredRepoIDs, err := db.FindIDs(ctx, "star", "star.repo_id",
  40. builder.Eq{"star.uid": u.ID})
  41. if err != nil {
  42. return fmt.Errorf("get all stars: %w", err)
  43. } else if err = db.DecrByIDs(ctx, starredRepoIDs, "num_stars", new(repo_model.Repository)); err != nil {
  44. return fmt.Errorf("decrease repository num_stars: %w", err)
  45. }
  46. // ***** END: Star *****
  47. // ***** START: Follow *****
  48. followeeIDs, err := db.FindIDs(ctx, "follow", "follow.follow_id",
  49. builder.Eq{"follow.user_id": u.ID})
  50. if err != nil {
  51. return fmt.Errorf("get all followees: %w", err)
  52. } else if err = db.DecrByIDs(ctx, followeeIDs, "num_followers", new(user_model.User)); err != nil {
  53. return fmt.Errorf("decrease user num_followers: %w", err)
  54. }
  55. followerIDs, err := db.FindIDs(ctx, "follow", "follow.user_id",
  56. builder.Eq{"follow.follow_id": u.ID})
  57. if err != nil {
  58. return fmt.Errorf("get all followers: %w", err)
  59. } else if err = db.DecrByIDs(ctx, followerIDs, "num_following", new(user_model.User)); err != nil {
  60. return fmt.Errorf("decrease user num_following: %w", err)
  61. }
  62. // ***** END: Follow *****
  63. if err = db.DeleteBeans(ctx,
  64. &auth_model.AccessToken{UID: u.ID},
  65. &repo_model.Collaboration{UserID: u.ID},
  66. &access_model.Access{UserID: u.ID},
  67. &repo_model.Watch{UserID: u.ID},
  68. &repo_model.Star{UID: u.ID},
  69. &user_model.Follow{UserID: u.ID},
  70. &user_model.Follow{FollowID: u.ID},
  71. &activities_model.Action{UserID: u.ID},
  72. &issues_model.IssueUser{UID: u.ID},
  73. &user_model.EmailAddress{UID: u.ID},
  74. &user_model.UserOpenID{UID: u.ID},
  75. &issues_model.Reaction{UserID: u.ID},
  76. &organization.TeamUser{UID: u.ID},
  77. &issues_model.Stopwatch{UserID: u.ID},
  78. &user_model.Setting{UserID: u.ID},
  79. &user_model.UserBadge{UserID: u.ID},
  80. &pull_model.AutoMerge{DoerID: u.ID},
  81. &pull_model.ReviewState{UserID: u.ID},
  82. &user_model.Redirect{RedirectUserID: u.ID},
  83. &actions_model.ActionRunner{OwnerID: u.ID},
  84. &user_model.Blocking{BlockerID: u.ID},
  85. &user_model.Blocking{BlockeeID: u.ID},
  86. &actions_model.ActionRunnerToken{OwnerID: u.ID},
  87. ); err != nil {
  88. return fmt.Errorf("deleteBeans: %w", err)
  89. }
  90. if err := auth_model.DeleteOAuth2RelictsByUserID(ctx, u.ID); err != nil {
  91. return err
  92. }
  93. if purge || (setting.Service.UserDeleteWithCommentsMaxTime != 0 &&
  94. u.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now())) {
  95. // Delete Comments
  96. const batchSize = 50
  97. for {
  98. comments := make([]*issues_model.Comment, 0, batchSize)
  99. if err = e.Where("type=? AND poster_id=?", issues_model.CommentTypeComment, u.ID).Limit(batchSize, 0).Find(&comments); err != nil {
  100. return err
  101. }
  102. if len(comments) == 0 {
  103. break
  104. }
  105. for _, comment := range comments {
  106. if err = issues_model.DeleteComment(ctx, comment); err != nil {
  107. return err
  108. }
  109. }
  110. }
  111. // Delete Reactions
  112. if err = issues_model.DeleteReaction(ctx, &issues_model.ReactionOptions{DoerID: u.ID}); err != nil {
  113. return err
  114. }
  115. }
  116. // ***** START: Branch Protections *****
  117. {
  118. const batchSize = 50
  119. for start := 0; ; start += batchSize {
  120. protections := make([]*git_model.ProtectedBranch, 0, batchSize)
  121. // @perf: We can't filter on DB side by u.ID, as those IDs are serialized as JSON strings.
  122. // We could filter down with `WHERE repo_id IN (reposWithPushPermission(u))`,
  123. // though that query will be quite complex and tricky to maintain (compare `getRepoAssignees()`).
  124. // Also, as we didn't update branch protections when removing entries from `access` table,
  125. // it's safer to iterate all protected branches.
  126. if err = e.Limit(batchSize, start).Find(&protections); err != nil {
  127. return fmt.Errorf("findProtectedBranches: %w", err)
  128. }
  129. if len(protections) == 0 {
  130. break
  131. }
  132. for _, p := range protections {
  133. if err := git_model.RemoveUserIDFromProtectedBranch(ctx, p, u.ID); err != nil {
  134. return err
  135. }
  136. }
  137. }
  138. }
  139. // ***** END: Branch Protections *****
  140. // ***** START: PublicKey *****
  141. if _, err = db.DeleteByBean(ctx, &asymkey_model.PublicKey{OwnerID: u.ID}); err != nil {
  142. return fmt.Errorf("deletePublicKeys: %w", err)
  143. }
  144. // ***** END: PublicKey *****
  145. // ***** START: GPGPublicKey *****
  146. keys, err := db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
  147. OwnerID: u.ID,
  148. })
  149. if err != nil {
  150. return fmt.Errorf("ListGPGKeys: %w", err)
  151. }
  152. // Delete GPGKeyImport(s).
  153. for _, key := range keys {
  154. if _, err = db.DeleteByBean(ctx, &asymkey_model.GPGKeyImport{KeyID: key.KeyID}); err != nil {
  155. return fmt.Errorf("deleteGPGKeyImports: %w", err)
  156. }
  157. }
  158. if _, err = db.DeleteByBean(ctx, &asymkey_model.GPGKey{OwnerID: u.ID}); err != nil {
  159. return fmt.Errorf("deleteGPGKeys: %w", err)
  160. }
  161. // ***** END: GPGPublicKey *****
  162. // Clear assignee.
  163. if _, err = db.DeleteByBean(ctx, &issues_model.IssueAssignees{AssigneeID: u.ID}); err != nil {
  164. return fmt.Errorf("clear assignee: %w", err)
  165. }
  166. // ***** START: ExternalLoginUser *****
  167. if err = user_model.RemoveAllAccountLinks(ctx, u); err != nil {
  168. return fmt.Errorf("ExternalLoginUser: %w", err)
  169. }
  170. // ***** END: ExternalLoginUser *****
  171. if err := auth_model.DeleteAuthTokensByUserID(ctx, u.ID); err != nil {
  172. return fmt.Errorf("DeleteAuthTokensByUserID: %w", err)
  173. }
  174. if _, err = db.DeleteByID[user_model.User](ctx, u.ID); err != nil {
  175. return fmt.Errorf("delete: %w", err)
  176. }
  177. return nil
  178. }