2021-11-19 01:42:27 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-11-19 01:42:27 +08:00
|
|
|
|
|
|
|
package org
|
|
|
|
|
|
|
|
import (
|
2023-05-21 23:13:47 +08:00
|
|
|
"context"
|
2021-11-19 01:42:27 +08:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2024-12-18 11:44:16 +08:00
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
2021-11-19 01:42:27 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2023-05-21 23:13:47 +08:00
|
|
|
org_model "code.gitea.io/gitea/models/organization"
|
2022-03-30 16:42:47 +08:00
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2024-12-18 11:44:16 +08:00
|
|
|
secret_model "code.gitea.io/gitea/models/secret"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-19 01:42:27 +08:00
|
|
|
"code.gitea.io/gitea/modules/storage"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-10-19 21:16:11 +08:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2021-11-19 01:42:27 +08:00
|
|
|
)
|
|
|
|
|
2024-12-18 11:44:16 +08:00
|
|
|
// deleteOrganization deletes models associated to an organization.
|
|
|
|
func deleteOrganization(ctx context.Context, org *org_model.Organization) error {
|
|
|
|
if org.Type != user_model.UserTypeOrganization {
|
|
|
|
return fmt.Errorf("%s is a user not an organization", org.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.DeleteBeans(ctx,
|
|
|
|
&org_model.Team{OrgID: org.ID},
|
|
|
|
&org_model.OrgUser{OrgID: org.ID},
|
|
|
|
&org_model.TeamUser{OrgID: org.ID},
|
|
|
|
&org_model.TeamUnit{OrgID: org.ID},
|
|
|
|
&org_model.TeamInvite{OrgID: org.ID},
|
|
|
|
&secret_model.Secret{OwnerID: org.ID},
|
|
|
|
&user_model.Blocking{BlockerID: org.ID},
|
|
|
|
&actions_model.ActionRunner{OwnerID: org.ID},
|
|
|
|
&actions_model.ActionRunnerToken{OwnerID: org.ID},
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("DeleteBeans: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := db.GetEngine(ctx).ID(org.ID).Delete(new(user_model.User)); err != nil {
|
|
|
|
return fmt.Errorf("Delete: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:42:27 +08:00
|
|
|
// DeleteOrganization completely and permanently deletes everything of organization.
|
2023-10-19 21:16:11 +08:00
|
|
|
func DeleteOrganization(ctx context.Context, org *org_model.Organization, purge bool) error {
|
2024-04-27 16:03:49 +08:00
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
2021-11-19 01:42:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-27 16:03:49 +08:00
|
|
|
defer committer.Close()
|
2021-11-19 01:42:27 +08:00
|
|
|
|
2023-10-19 21:16:11 +08:00
|
|
|
if purge {
|
|
|
|
err := repo_service.DeleteOwnerRepositoriesDirectly(ctx, org.AsUser())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:42:27 +08:00
|
|
|
// Check ownership of repository.
|
2022-05-20 22:08:52 +08:00
|
|
|
count, err := repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{OwnerID: org.ID})
|
2021-11-19 01:42:27 +08:00
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("GetRepositoryCount: %w", err)
|
2021-11-19 01:42:27 +08:00
|
|
|
} else if count > 0 {
|
|
|
|
return models.ErrUserOwnRepos{UID: org.ID}
|
|
|
|
}
|
|
|
|
|
2022-03-30 16:42:47 +08:00
|
|
|
// Check ownership of packages.
|
|
|
|
if ownsPackages, err := packages_model.HasOwnerPackages(ctx, org.ID); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("HasOwnerPackages: %w", err)
|
2022-03-30 16:42:47 +08:00
|
|
|
} else if ownsPackages {
|
|
|
|
return models.ErrUserOwnPackages{UID: org.ID}
|
|
|
|
}
|
|
|
|
|
2024-12-18 11:44:16 +08:00
|
|
|
if err := deleteOrganization(ctx, org); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("DeleteOrganization: %w", err)
|
2021-11-19 01:42:27 +08:00
|
|
|
}
|
|
|
|
|
2024-04-27 16:03:49 +08:00
|
|
|
if err := committer.Commit(); err != nil {
|
2021-11-19 01:42:27 +08:00
|
|
|
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 17:49:20 +08:00
|
|
|
path := user_model.UserPath(org.Name)
|
2021-11-19 01:42:27 +08:00
|
|
|
|
|
|
|
if err := util.RemoveAll(path); err != nil {
|
2023-05-21 23:13:47 +08:00
|
|
|
return fmt.Errorf("failed to RemoveAll %s: %w", path, err)
|
2021-11-19 01:42:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(org.Avatar) > 0 {
|
|
|
|
avatarPath := org.CustomAvatarRelativePath()
|
|
|
|
if err := storage.Avatars.Delete(avatarPath); err != nil {
|
2023-05-21 23:13:47 +08:00
|
|
|
return fmt.Errorf("failed to remove %s: %w", avatarPath, err)
|
2021-11-19 01:42:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|