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.

user.go 6.5KB

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