From 8953f83f87c6d0d038e1e5506779156f011f7b70 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 8 Jul 2017 15:51:13 -0500 Subject: [PATCH] Add migrations to handle cascade deletions for servers and users --- app/Providers/MacroServiceProvider.php | 3 +- ...eUserPermissionsToDeleteOnUserDeletion.php | 52 +++++++++++++++++++ ...llocationToReferenceNullOnServerDelete.php | 36 +++++++++++++ ...DeletionWhenAServerOrVariableIsDeleted.php | 40 ++++++++++++++ 4 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2017_07_08_152806_ChangeUserPermissionsToDeleteOnUserDeletion.php create mode 100644 database/migrations/2017_07_08_154416_SetAllocationToReferenceNullOnServerDelete.php create mode 100644 database/migrations/2017_07_08_154650_CascadeDeletionWhenAServerOrVariableIsDeleted.php diff --git a/app/Providers/MacroServiceProvider.php b/app/Providers/MacroServiceProvider.php index 8dd08f73b..600c0d3f3 100644 --- a/app/Providers/MacroServiceProvider.php +++ b/app/Providers/MacroServiceProvider.php @@ -30,6 +30,7 @@ use Carbon; use Request; use Pterodactyl\Models\APIKey; use Illuminate\Support\ServiceProvider; +use Pterodactyl\Services\ApiKeyService; class MacroServiceProvider extends ServiceProvider { @@ -60,7 +61,7 @@ class MacroServiceProvider extends ServiceProvider $parts = explode('.', Request::bearerToken()); - if (count($parts) === 2 && strlen($parts[0]) === APIKey::PUBLIC_KEY_LEN) { + if (count($parts) === 2 && strlen($parts[0]) === ApiKeyService::PUB_CRYPTO_BYTES * 2) { // Because the key itself isn't changing frequently, we simply cache this for // 15 minutes to speed up the API and keep requests flowing. return Cache::tags([ diff --git a/database/migrations/2017_07_08_152806_ChangeUserPermissionsToDeleteOnUserDeletion.php b/database/migrations/2017_07_08_152806_ChangeUserPermissionsToDeleteOnUserDeletion.php new file mode 100644 index 000000000..eaa7a4bf5 --- /dev/null +++ b/database/migrations/2017_07_08_152806_ChangeUserPermissionsToDeleteOnUserDeletion.php @@ -0,0 +1,52 @@ +dropForeign(['subuser_id']); + + $table->foreign('subuser_id')->references('id')->on('subusers')->onDelete('cascade'); + }); + + Schema::table('subusers', function (Blueprint $table) { + $table->dropForeign(['user_id']); + $table->dropForeign(['server_id']); + + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + $table->foreign('server_id')->references('id')->on('servers')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('subusers', function (Blueprint $table) { + $table->dropForeign(['user_id']); + $table->dropForeign(['server_id']); + + $table->foreign('user_id')->references('id')->on('users'); + $table->foreign('server_id')->references('id')->on('servers'); + }); + + Schema::table('permissions', function (Blueprint $table) { + $table->dropForeign(['subuser_id']); + + $table->foreign('subuser_id')->references('id')->on('subusers'); + }); + } +} diff --git a/database/migrations/2017_07_08_154416_SetAllocationToReferenceNullOnServerDelete.php b/database/migrations/2017_07_08_154416_SetAllocationToReferenceNullOnServerDelete.php new file mode 100644 index 000000000..76e475b0e --- /dev/null +++ b/database/migrations/2017_07_08_154416_SetAllocationToReferenceNullOnServerDelete.php @@ -0,0 +1,36 @@ +dropForeign(['server_id']); + + $table->foreign('server_id')->references('id')->on('servers')->onDelete('set null'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('allocations', function (Blueprint $table) { + $table->dropForeign(['server_id']); + + $table->foreign('server_id')->references('id')->on('servers'); + }); + } +} diff --git a/database/migrations/2017_07_08_154650_CascadeDeletionWhenAServerOrVariableIsDeleted.php b/database/migrations/2017_07_08_154650_CascadeDeletionWhenAServerOrVariableIsDeleted.php new file mode 100644 index 000000000..f599f02cc --- /dev/null +++ b/database/migrations/2017_07_08_154650_CascadeDeletionWhenAServerOrVariableIsDeleted.php @@ -0,0 +1,40 @@ +dropForeign(['server_id']); + $table->dropForeign(['variable_id']); + + $table->foreign('server_id')->references('id')->on('servers')->onDelete('cascade'); + $table->foreign('variable_id')->references('id')->on('service_variables')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('server_variables', function (Blueprint $table) { + $table->dropForeign(['server_id']); + $table->dropForeign(['variable_id']); + + $table->foreign('server_id')->references('id')->on('servers'); + $table->foreign('variable_id')->references('id')->on('service_variables'); + }); + } +}