2021-11-18 23:12:27 +05:30
|
|
|
// 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"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-03-30 14:12:47 +05:30
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-18 23:12:27 +05:30
|
|
|
"code.gitea.io/gitea/modules/storage"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteOrganization completely and permanently deletes everything of organization.
|
2022-03-29 11:59:02 +05:30
|
|
|
func DeleteOrganization(org *organization.Organization) error {
|
2022-11-13 01:48:50 +05:30
|
|
|
ctx, commiter, err := db.TxContext(db.DefaultContext)
|
2021-11-18 23:12:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer commiter.Close()
|
|
|
|
|
|
|
|
// Check ownership of repository.
|
2022-05-20 19:38:52 +05:30
|
|
|
count, err := repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{OwnerID: org.ID})
|
2021-11-18 23:12:27 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("GetRepositoryCount: %w", err)
|
2021-11-18 23:12:27 +05:30
|
|
|
} else if count > 0 {
|
|
|
|
return models.ErrUserOwnRepos{UID: org.ID}
|
|
|
|
}
|
|
|
|
|
2022-03-30 14:12:47 +05:30
|
|
|
// Check ownership of packages.
|
|
|
|
if ownsPackages, err := packages_model.HasOwnerPackages(ctx, org.ID); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("HasOwnerPackages: %w", err)
|
2022-03-30 14:12:47 +05:30
|
|
|
} else if ownsPackages {
|
|
|
|
return models.ErrUserOwnPackages{UID: org.ID}
|
|
|
|
}
|
|
|
|
|
2022-03-29 11:59:02 +05:30
|
|
|
if err := organization.DeleteOrganization(ctx, org); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("DeleteOrganization: %w", err)
|
2021-11-18 23:12:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2021-11-24 15:19:20 +05:30
|
|
|
path := user_model.UserPath(org.Name)
|
2021-11-18 23:12:27 +05:30
|
|
|
|
|
|
|
if err := util.RemoveAll(path); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("Failed to RemoveAll %s: %w", path, err)
|
2021-11-18 23:12:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if len(org.Avatar) > 0 {
|
|
|
|
avatarPath := org.CustomAvatarRelativePath()
|
|
|
|
if err := storage.Avatars.Delete(avatarPath); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("Failed to remove %s: %w", avatarPath, err)
|
2021-11-18 23:12:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|