您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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