2021-11-18 18:42:27 +01:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-11-18 18:42:27 +01:00
|
|
|
|
|
|
|
package org
|
|
|
|
|
|
|
|
import (
|
2023-05-21 17:13:47 +02:00
|
|
|
"context"
|
2021-11-18 18:42:27 +01:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2023-05-21 17:13:47 +02:00
|
|
|
org_model "code.gitea.io/gitea/models/organization"
|
2022-03-30 10:42:47 +02:00
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-18 18:42:27 +01:00
|
|
|
"code.gitea.io/gitea/modules/storage"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-05-21 17:13:47 +02:00
|
|
|
user_service "code.gitea.io/gitea/services/user"
|
2021-11-18 18:42:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteOrganization completely and permanently deletes everything of organization.
|
2023-05-21 17:13:47 +02:00
|
|
|
func DeleteOrganization(org *org_model.Organization) error {
|
2022-11-12 21:18:50 +01:00
|
|
|
ctx, commiter, err := db.TxContext(db.DefaultContext)
|
2021-11-18 18:42:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer commiter.Close()
|
|
|
|
|
|
|
|
// Check ownership of repository.
|
2022-05-20 16:08:52 +02:00
|
|
|
count, err := repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{OwnerID: org.ID})
|
2021-11-18 18:42:27 +01:00
|
|
|
if err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("GetRepositoryCount: %w", err)
|
2021-11-18 18:42:27 +01:00
|
|
|
} else if count > 0 {
|
|
|
|
return models.ErrUserOwnRepos{UID: org.ID}
|
|
|
|
}
|
|
|
|
|
2022-03-30 10:42:47 +02:00
|
|
|
// Check ownership of packages.
|
|
|
|
if ownsPackages, err := packages_model.HasOwnerPackages(ctx, org.ID); err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("HasOwnerPackages: %w", err)
|
2022-03-30 10:42:47 +02:00
|
|
|
} else if ownsPackages {
|
|
|
|
return models.ErrUserOwnPackages{UID: org.ID}
|
|
|
|
}
|
|
|
|
|
2023-05-21 17:13:47 +02:00
|
|
|
if err := org_model.DeleteOrganization(ctx, org); err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("DeleteOrganization: %w", err)
|
2021-11-18 18:42:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 10:49:20 +01:00
|
|
|
path := user_model.UserPath(org.Name)
|
2021-11-18 18:42:27 +01:00
|
|
|
|
|
|
|
if err := util.RemoveAll(path); err != nil {
|
2023-05-21 17:13:47 +02:00
|
|
|
return fmt.Errorf("failed to RemoveAll %s: %w", path, err)
|
2021-11-18 18:42:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(org.Avatar) > 0 {
|
|
|
|
avatarPath := org.CustomAvatarRelativePath()
|
|
|
|
if err := storage.Avatars.Delete(avatarPath); err != nil {
|
2023-05-21 17:13:47 +02:00
|
|
|
return fmt.Errorf("failed to remove %s: %w", avatarPath, err)
|
2021-11-18 18:42:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-21 17:13:47 +02:00
|
|
|
|
|
|
|
// RenameOrganization renames an organization.
|
|
|
|
func RenameOrganization(ctx context.Context, org *org_model.Organization, newName string) error {
|
|
|
|
return user_service.RenameUser(ctx, org.AsUser(), newName)
|
|
|
|
}
|