mirror of
https://github.com/go-gitea/gitea.git
synced 2026-01-16 17:10:36 +00:00
Release attachments must belong to the intended repo (#36347)
This commit is contained in:
@@ -221,28 +221,25 @@ func MakeRepoPrivate(ctx context.Context, repo *repo_model.Repository) (err erro
|
||||
})
|
||||
}
|
||||
|
||||
// LinkedRepository returns the linked repo if any
|
||||
func LinkedRepository(ctx context.Context, a *repo_model.Attachment) (*repo_model.Repository, unit.Type, error) {
|
||||
// GetAttachmentLinkedType returns the linked type of attachment if any
|
||||
func GetAttachmentLinkedType(ctx context.Context, a *repo_model.Attachment) (unit.Type, error) {
|
||||
if a.IssueID != 0 {
|
||||
iss, err := issues_model.GetIssueByID(ctx, a.IssueID)
|
||||
if err != nil {
|
||||
return nil, unit.TypeIssues, err
|
||||
return unit.TypeIssues, err
|
||||
}
|
||||
repo, err := repo_model.GetRepositoryByID(ctx, iss.RepoID)
|
||||
unitType := unit.TypeIssues
|
||||
if iss.IsPull {
|
||||
unitType = unit.TypePullRequests
|
||||
}
|
||||
return repo, unitType, err
|
||||
} else if a.ReleaseID != 0 {
|
||||
rel, err := repo_model.GetReleaseByID(ctx, a.ReleaseID)
|
||||
if err != nil {
|
||||
return nil, unit.TypeReleases, err
|
||||
}
|
||||
repo, err := repo_model.GetRepositoryByID(ctx, rel.RepoID)
|
||||
return repo, unit.TypeReleases, err
|
||||
return unitType, nil
|
||||
}
|
||||
return nil, -1, nil
|
||||
|
||||
if a.ReleaseID != 0 {
|
||||
_, err := repo_model.GetReleaseByID(ctx, a.ReleaseID)
|
||||
return unit.TypeReleases, err
|
||||
}
|
||||
return unit.TypeInvalid, nil
|
||||
}
|
||||
|
||||
// CheckDaemonExportOK creates/removes git-daemon-export-ok for git-daemon...
|
||||
|
||||
@@ -16,28 +16,24 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLinkedRepository(t *testing.T) {
|
||||
func TestAttachLinkedType(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
testCases := []struct {
|
||||
name string
|
||||
attachID int64
|
||||
expectedRepo *repo_model.Repository
|
||||
expectedUnitType unit.Type
|
||||
}{
|
||||
{"LinkedIssue", 1, &repo_model.Repository{ID: 1}, unit.TypeIssues},
|
||||
{"LinkedComment", 3, &repo_model.Repository{ID: 1}, unit.TypePullRequests},
|
||||
{"LinkedRelease", 9, &repo_model.Repository{ID: 1}, unit.TypeReleases},
|
||||
{"Notlinked", 10, nil, -1},
|
||||
{"LinkedIssue", 1, unit.TypeIssues},
|
||||
{"LinkedComment", 3, unit.TypePullRequests},
|
||||
{"LinkedRelease", 9, unit.TypeReleases},
|
||||
{"Notlinked", 10, unit.TypeInvalid},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
attach, err := repo_model.GetAttachmentByID(t.Context(), tc.attachID)
|
||||
assert.NoError(t, err)
|
||||
repo, unitType, err := LinkedRepository(t.Context(), attach)
|
||||
unitType, err := GetAttachmentLinkedType(t.Context(), attach)
|
||||
assert.NoError(t, err)
|
||||
if tc.expectedRepo != nil {
|
||||
assert.Equal(t, tc.expectedRepo.ID, repo.ID)
|
||||
}
|
||||
assert.Equal(t, tc.expectedUnitType, unitType)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -239,6 +239,11 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
|
||||
if err := deleteUser(ctx, u, purge); err != nil {
|
||||
return fmt.Errorf("DeleteUser: %w", err)
|
||||
}
|
||||
|
||||
// Finally delete any unlinked attachments, this will also delete the attached files
|
||||
if err := deleteUserUnlinkedAttachments(ctx, u); err != nil {
|
||||
return fmt.Errorf("deleteUserUnlinkedAttachments: %w", err)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
@@ -269,6 +274,19 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteUserUnlinkedAttachments(ctx context.Context, u *user_model.User) error {
|
||||
attachments, err := repo_model.GetUnlinkedAttachmentsByUserID(ctx, u.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetUnlinkedAttachmentsByUserID: %w", err)
|
||||
}
|
||||
for _, attach := range attachments {
|
||||
if err := repo_model.DeleteAttachment(ctx, attach, true); err != nil {
|
||||
return fmt.Errorf("DeleteAttachment ID[%d]: %w", attach.ID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteInactiveUsers deletes all inactive users and their email addresses.
|
||||
func DeleteInactiveUsers(ctx context.Context, olderThan time.Duration) error {
|
||||
inactiveUsers, err := user_model.GetInactiveUsers(ctx, olderThan)
|
||||
|
||||
@@ -63,6 +63,24 @@ func TestDeleteUser(t *testing.T) {
|
||||
assert.Error(t, DeleteUser(t.Context(), org, false))
|
||||
}
|
||||
|
||||
func TestDeleteUserUnlinkedAttachments(t *testing.T) {
|
||||
t.Run("DeleteExisting", func(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 8})
|
||||
unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: 10})
|
||||
|
||||
assert.NoError(t, deleteUserUnlinkedAttachments(t.Context(), user))
|
||||
unittest.AssertNotExistsBean(t, &repo_model.Attachment{ID: 10})
|
||||
})
|
||||
|
||||
t.Run("NoUnlinkedAttachments", func(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
|
||||
assert.NoError(t, deleteUserUnlinkedAttachments(t.Context(), user))
|
||||
})
|
||||
}
|
||||
|
||||
func TestPurgeUser(t *testing.T) {
|
||||
test := func(userID int64) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
Reference in New Issue
Block a user