1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Move user/org deletion to services (#17673)

This commit is contained in:
KN4CK3R
2021-11-18 18:42:27 +01:00
committed by GitHub
parent 55be5fe339
commit f34151bdb2
24 changed files with 382 additions and 301 deletions

61
services/org/org.go Normal file
View File

@@ -0,0 +1,61 @@
// 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/modules/storage"
"code.gitea.io/gitea/modules/util"
)
// DeleteOrganization completely and permanently deletes everything of organization.
func DeleteOrganization(org *models.User) error {
if !org.IsOrganization() {
return fmt.Errorf("%s is a user not an organization", org.Name)
}
ctx, commiter, err := db.TxContext()
if err != nil {
return err
}
defer commiter.Close()
// Check ownership of repository.
count, err := models.GetRepositoryCount(ctx, org)
if err != nil {
return fmt.Errorf("GetRepositoryCount: %v", err)
} else if count > 0 {
return models.ErrUserOwnRepos{UID: org.ID}
}
if err := models.DeleteOrganization(ctx, org); err != nil {
return fmt.Errorf("DeleteOrganization: %v", 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 := models.UserPath(org.Name)
if err := util.RemoveAll(path); err != nil {
return fmt.Errorf("Failed to RemoveAll %s: %v", 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: %v", avatarPath, err)
}
}
return nil
}

37
services/org/org_test.go Normal file
View File

@@ -0,0 +1,37 @@
// 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 (
"path/filepath"
"testing"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."))
}
func TestDeleteOrganization(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
org := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 6}).(*models.User)
assert.NoError(t, DeleteOrganization(org))
unittest.AssertNotExistsBean(t, &models.User{ID: 6})
unittest.AssertNotExistsBean(t, &models.OrgUser{OrgID: 6})
unittest.AssertNotExistsBean(t, &models.Team{OrgID: 6})
org = unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User)
err := DeleteOrganization(org)
assert.Error(t, err)
assert.True(t, models.IsErrUserOwnRepos(err))
user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User)
assert.Error(t, DeleteOrganization(user))
unittest.CheckConsistencyFor(t, &models.User{}, &models.Team{})
}