mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 00:20:25 +08:00 
			
		
		
		
	Add option to purge users (#18064)
Add the ability to purge users when deleting them. Close #15588 Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
		@@ -107,7 +107,7 @@ func getVersionByNameAndVersion(ctx context.Context, ownerID int64, packageType
 | 
			
		||||
			ExactMatch: true,
 | 
			
		||||
			Value:      version,
 | 
			
		||||
		},
 | 
			
		||||
		IsInternal: isInternal,
 | 
			
		||||
		IsInternal: util.OptionalBoolOf(isInternal),
 | 
			
		||||
		Paginator:  db.NewAbsoluteListOptions(0, 1),
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -171,7 +171,7 @@ type PackageSearchOptions struct {
 | 
			
		||||
	Name            SearchValue       // only results with the specific name are found
 | 
			
		||||
	Version         SearchValue       // only results with the specific version are found
 | 
			
		||||
	Properties      map[string]string // only results are found which contain all listed version properties with the specific value
 | 
			
		||||
	IsInternal      bool
 | 
			
		||||
	IsInternal      util.OptionalBool
 | 
			
		||||
	HasFileWithName string            // only results are found which are associated with a file with the specific name
 | 
			
		||||
	HasFiles        util.OptionalBool // only results are found which have associated files
 | 
			
		||||
	Sort            string
 | 
			
		||||
@@ -179,7 +179,10 @@ type PackageSearchOptions struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (opts *PackageSearchOptions) toConds() builder.Cond {
 | 
			
		||||
	var cond builder.Cond = builder.Eq{"package_version.is_internal": opts.IsInternal}
 | 
			
		||||
	cond := builder.NewCond()
 | 
			
		||||
	if !opts.IsInternal.IsNone() {
 | 
			
		||||
		cond = builder.Eq{"package_version.is_internal": opts.IsInternal.IsTrue()}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if opts.OwnerID != 0 {
 | 
			
		||||
		cond = cond.And(builder.Eq{"package.owner_id": opts.OwnerID})
 | 
			
		||||
 
 | 
			
		||||
@@ -330,3 +330,40 @@ func DeleteProjectByIDCtx(ctx context.Context, id int64) error {
 | 
			
		||||
 | 
			
		||||
	return updateRepositoryProjectCount(ctx, p.RepoID)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func DeleteProjectByRepoIDCtx(ctx context.Context, repoID int64) error {
 | 
			
		||||
	switch {
 | 
			
		||||
	case setting.Database.UseSQLite3:
 | 
			
		||||
		if _, err := db.GetEngine(ctx).Exec("DELETE FROM project_issue WHERE project_issue.id IN (SELECT project_issue.id FROM project_issue INNER JOIN project WHERE project.id = project_issue.project_id AND project.repo_id = ?)", repoID); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if _, err := db.GetEngine(ctx).Exec("DELETE FROM project_board WHERE project_board.id IN (SELECT project_board.id FROM project_board INNER JOIN project WHERE project.id = project_board.project_id AND project.repo_id = ?)", repoID); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if _, err := db.GetEngine(ctx).Table("project").Where("repo_id = ? ", repoID).Delete(&Project{}); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	case setting.Database.UsePostgreSQL:
 | 
			
		||||
		if _, err := db.GetEngine(ctx).Exec("DELETE FROM project_issue USING project WHERE project.id = project_issue.project_id AND project.repo_id = ? ", repoID); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if _, err := db.GetEngine(ctx).Exec("DELETE FROM project_board USING project WHERE project.id = project_board.project_id AND project.repo_id = ? ", repoID); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if _, err := db.GetEngine(ctx).Table("project").Where("repo_id = ? ", repoID).Delete(&Project{}); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	default:
 | 
			
		||||
		if _, err := db.GetEngine(ctx).Exec("DELETE project_issue FROM project_issue INNER JOIN project ON project.id = project_issue.project_id WHERE project.repo_id = ? ", repoID); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if _, err := db.GetEngine(ctx).Exec("DELETE project_board FROM project_board INNER JOIN project ON project.id = project_board.project_id WHERE project.repo_id = ? ", repoID); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if _, err := db.GetEngine(ctx).Table("project").Where("repo_id = ? ", repoID).Delete(&Project{}); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return updateRepositoryProjectCount(ctx, repoID)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -342,16 +342,8 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	projects, _, err := project_model.GetProjects(ctx, project_model.SearchOptions{
 | 
			
		||||
		RepoID: repoID,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("get projects: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	for i := range projects {
 | 
			
		||||
		if err := project_model.DeleteProjectByIDCtx(ctx, projects[i].ID); err != nil {
 | 
			
		||||
			return fmt.Errorf("delete project [%d]: %v", projects[i].ID, err)
 | 
			
		||||
		}
 | 
			
		||||
	if err := project_model.DeleteProjectByRepoIDCtx(ctx, repoID); err != nil {
 | 
			
		||||
		return fmt.Errorf("unable to delete projects for repo[%d]: %v", repoID, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Remove LFS objects
 | 
			
		||||
 
 | 
			
		||||
@@ -27,7 +27,7 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// DeleteUser deletes models associated to an user.
 | 
			
		||||
func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
 | 
			
		||||
func DeleteUser(ctx context.Context, u *user_model.User, purge bool) (err error) {
 | 
			
		||||
	e := db.GetEngine(ctx)
 | 
			
		||||
 | 
			
		||||
	// ***** START: Watch *****
 | 
			
		||||
@@ -95,8 +95,8 @@ func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if setting.Service.UserDeleteWithCommentsMaxTime != 0 &&
 | 
			
		||||
		u.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now()) {
 | 
			
		||||
	if purge || (setting.Service.UserDeleteWithCommentsMaxTime != 0 &&
 | 
			
		||||
		u.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now())) {
 | 
			
		||||
 | 
			
		||||
		// Delete Comments
 | 
			
		||||
		const batchSize = 50
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user