Delete subuser on owner change (#748)

* Delete subuser on owner change

* Move logic to Model
This commit is contained in:
MartinOscar 2024-12-03 23:55:02 +01:00 committed by GitHub
parent 6d42a15ec3
commit 09eac71f05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 0 deletions

View File

@ -19,6 +19,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Relations\MorphToMany;
use App\Exceptions\Http\Server\ServerStateConflictException; use App\Exceptions\Http\Server\ServerStateConflictException;
use App\Services\Subusers\SubuserDeletionService;
/** /**
* \App\Models\Server. * \App\Models\Server.
@ -203,6 +204,17 @@ class Server extends Model
]; ];
} }
protected static function booted(): void
{
static::saved(function (self $server) {
$subuser = $server->subusers()->where('user_id', $server->owner_id)->first();
if ($subuser) {
// @phpstan-ignore-next-line
app(SubuserDeletionService::class)->handle($subuser, $server);
}
});
}
/** /**
* Returns the format for server allocations when communicating with the Daemon. * Returns the format for server allocations when communicating with the Daemon.
*/ */

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::table('subusers')
->whereIn('user_id', function ($query) {
$query->select('id')
->from('servers')
->whereColumn('owner_id', 'subusers.server_id');
})
->delete();
}
/**
* Reverse the migrations.
*/
public function down(): void
{
// Not needed
}
};