mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-23 20:17:03 +01:00
34283a74e8
Fix #19513 This PR introduce a new db method `InTransaction(context.Context)`, and also builtin check on `db.TxContext` and `db.WithTx`. There is also a new method `db.AutoTx` has been introduced but could be used by other PRs. `WithTx` will always open a new transaction, if a transaction exist in context, return an error. `AutoTx` will try to open a new transaction if no transaction exist in context. That means it will always enter a transaction if there is no error. Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: 6543 <6543@obermui.de>
68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package org
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"code.gitea.io/gitea/models"
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/models/organization"
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/storage"
|
|
"code.gitea.io/gitea/modules/util"
|
|
)
|
|
|
|
// DeleteOrganization completely and permanently deletes everything of organization.
|
|
func DeleteOrganization(org *organization.Organization) error {
|
|
ctx, commiter, err := db.TxContext(db.DefaultContext)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer commiter.Close()
|
|
|
|
// Check ownership of repository.
|
|
count, err := repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{OwnerID: org.ID})
|
|
if err != nil {
|
|
return fmt.Errorf("GetRepositoryCount: %w", err)
|
|
} else if count > 0 {
|
|
return models.ErrUserOwnRepos{UID: org.ID}
|
|
}
|
|
|
|
// Check ownership of packages.
|
|
if ownsPackages, err := packages_model.HasOwnerPackages(ctx, org.ID); err != nil {
|
|
return fmt.Errorf("HasOwnerPackages: %w", err)
|
|
} else if ownsPackages {
|
|
return models.ErrUserOwnPackages{UID: org.ID}
|
|
}
|
|
|
|
if err := organization.DeleteOrganization(ctx, org); err != nil {
|
|
return fmt.Errorf("DeleteOrganization: %w", err)
|
|
}
|
|
|
|
if err := commiter.Commit(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// FIXME: system notice
|
|
// Note: There are something just cannot be roll back,
|
|
// so just keep error logs of those operations.
|
|
path := user_model.UserPath(org.Name)
|
|
|
|
if err := util.RemoveAll(path); err != nil {
|
|
return fmt.Errorf("Failed to RemoveAll %s: %w", path, err)
|
|
}
|
|
|
|
if len(org.Avatar) > 0 {
|
|
avatarPath := org.CustomAvatarRelativePath()
|
|
if err := storage.Avatars.Delete(avatarPath); err != nil {
|
|
return fmt.Errorf("Failed to remove %s: %w", avatarPath, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|