chore: add foreign keys on all tables (#3311)
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\DBHelper;
|
||||
use App\Models\User\User;
|
||||
use App\Helpers\DateHelper;
|
||||
use App\Models\Contact\Tag;
|
||||
@@ -15,7 +14,6 @@ use App\Jobs\AddContactFromVCard;
|
||||
use App\Models\Account\ImportJob;
|
||||
use App\Models\Account\Invitation;
|
||||
use App\Services\User\EmailChange;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Exceptions\StripeException;
|
||||
use Lahaxearnaud\U2f\Models\U2fKey;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@@ -26,58 +24,14 @@ use Illuminate\Support\Facades\Storage;
|
||||
use LaravelWebauthn\Models\WebauthnKey;
|
||||
use App\Http\Requests\InvitationRequest;
|
||||
use App\Services\Contact\Tag\DestroyTag;
|
||||
use App\Services\Account\DestroyAllDocuments;
|
||||
use App\Services\Account\Settings\ResetAccount;
|
||||
use App\Services\Account\Settings\DestroyAccount;
|
||||
use PragmaRX\Google2FALaravel\Facade as Google2FA;
|
||||
use App\Http\Resources\Settings\U2fKey\U2fKey as U2fKeyResource;
|
||||
use App\Http\Resources\Settings\WebauthnKey\WebauthnKey as WebauthnKeyResource;
|
||||
|
||||
class SettingsController
|
||||
{
|
||||
protected $ignoredTables = [
|
||||
'accounts',
|
||||
'activity_type_activities',
|
||||
'activity_types',
|
||||
'api_usage',
|
||||
'cache',
|
||||
'countries',
|
||||
'contact_photo',
|
||||
'crons',
|
||||
'currencies',
|
||||
'contact_photo',
|
||||
'default_activity_types',
|
||||
'default_activity_type_categories',
|
||||
'default_contact_field_types',
|
||||
'default_contact_modules',
|
||||
'default_life_event_categories',
|
||||
'default_life_event_types',
|
||||
'default_relationship_type_groups',
|
||||
'default_relationship_types',
|
||||
'emotions',
|
||||
'emotions_primary',
|
||||
'emotions_secondary',
|
||||
'failed_jobs',
|
||||
'instances',
|
||||
'jobs',
|
||||
'migrations',
|
||||
'oauth_access_tokens',
|
||||
'oauth_auth_codes',
|
||||
'oauth_clients',
|
||||
'oauth_personal_access_clients',
|
||||
'oauth_refresh_tokens',
|
||||
'password_resets',
|
||||
'pet_categories',
|
||||
'sessions',
|
||||
'statistics',
|
||||
'subscriptions',
|
||||
'telescope_entries',
|
||||
'telescope_entries_tags',
|
||||
'telescope_monitoring',
|
||||
'terms',
|
||||
'u2f_key',
|
||||
'users',
|
||||
'webauthn_keys',
|
||||
];
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
@@ -155,40 +109,18 @@ class SettingsController
|
||||
*/
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$user = $request->user();
|
||||
$account = $user->account;
|
||||
|
||||
app(DestroyAllDocuments::class)->execute([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$tables = DBHelper::getTables();
|
||||
|
||||
// Looping over the tables
|
||||
foreach ($tables as $table) {
|
||||
$tableName = $table->table_name;
|
||||
|
||||
if (in_array($tableName, $this->ignoredTables)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DB::table($tableName)->where('account_id', $account->id)->delete();
|
||||
}
|
||||
|
||||
$account = auth()->user()->account;
|
||||
|
||||
if ($account->isSubscribed() && ! $account->has_access_to_paid_version_for_free) {
|
||||
try {
|
||||
$account->subscriptionCancel();
|
||||
} catch (StripeException $e) {
|
||||
return redirect()->route('settings.index')
|
||||
->withErrors($e->getMessage());
|
||||
}
|
||||
try {
|
||||
app(DestroyAccount::class)->execute([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
} catch (StripeException $e) {
|
||||
return redirect()->route('settings.index')
|
||||
->withErrors($e->getMessage());
|
||||
}
|
||||
|
||||
DB::table('accounts')->where('id', $account->id)->delete();
|
||||
auth()->logout();
|
||||
$user->delete();
|
||||
|
||||
return redirect()->route('login');
|
||||
}
|
||||
@@ -205,27 +137,10 @@ class SettingsController
|
||||
$user = $request->user();
|
||||
$account = $user->account;
|
||||
|
||||
app(DestroyAllDocuments::class)->execute([
|
||||
app(ResetAccount::class)->execute([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$tables = DBHelper::getTables();
|
||||
|
||||
// TODO(tom@tomrochette.com): We cannot simply iterate over tables to reset an account
|
||||
// as this will not work with foreign key constraints
|
||||
// Looping over the tables
|
||||
foreach ($tables as $table) {
|
||||
$tableName = $table->table_name;
|
||||
|
||||
if (in_array($tableName, $this->ignoredTables)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DB::table($tableName)->where('account_id', $account->id)->delete();
|
||||
}
|
||||
|
||||
$account->populateDefaultFields();
|
||||
|
||||
return redirect()->route('settings.index')
|
||||
->with('status', trans('settings.reset_success'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user