fix(user-deletion): handle GitHub app sources across team cleanup

Limit team cleanup to apps owned by the deleted team and nullify cross-team application source references before deleting team-owned sources. Adds feature tests covering user deletion with GitHub app-backed applications, preserving system-wide apps, and nullifying external source links.
This commit is contained in:
Andras Bacsai committed 2026-04-05 18:08:06 +02:00
1 parent 968508583d
commit 3f564f9b2e
3 files changed
+187 -2

No files matched your search

+4 -2
View File
@@ -76,8 +76,10 @@ class Team extends Model implements SendsDiscord, SendsEmail, SendsPushover, Sen
foreach ($keys as $key) {
$key->delete();
}
$sources = $team->sources();
foreach ($sources as $source) {
// Only delete sources owned by this team, not system-wide ones from other teams
$teamSources = GithubApp::where('team_id', $team->id)->get()
->merge(GitlabApp::where('team_id', $team->id)->get());
foreach ($teamSources as $source) {
$source->delete();
}
$tags = Tag::whereTeamId($team->id)->get();
+17
View File
@@ -176,6 +176,23 @@ class User extends Authenticatable implements SendsEmail
$project->forceDelete();
}
// Detach applications from other teams that reference this team's sources,
// so the GithubApp/GitlabApp deleting guard doesn't block team deletion
$githubAppIds = GithubApp::where('team_id', $team->id)->pluck('id');
$gitlabAppIds = GitlabApp::where('team_id', $team->id)->pluck('id');
if ($githubAppIds->isNotEmpty()) {
Application::where('source_type', GithubApp::class)
->whereIn('source_id', $githubAppIds)
->update(['source_id' => null, 'source_type' => null]);
}
if ($gitlabAppIds->isNotEmpty()) {
Application::where('source_type', GitlabApp::class)
->whereIn('source_id', $gitlabAppIds)
->update(['source_id' => null, 'source_type' => null]);
}
$team->members()->detach($user->id);
$team->delete();
}