Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add foreign keys on all tables #3311

Merged
merged 23 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Enhancements:

* Add foreign keys to all tables
* Add English (UK) locale
* Add API methods to destroy and store documents
* Add API methods to manage photos and avatars
Expand Down
105 changes: 10 additions & 95 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +14,6 @@
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;
Expand All @@ -26,58 +24,14 @@
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.
*
Expand Down Expand Up @@ -155,40 +109,18 @@ public function save(SettingsRequest $request)
*/
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');
}
Expand All @@ -205,27 +137,10 @@ public function reset(Request $request)
$user = $request->user();
$account = $user->account;

app(DestroyAllDocuments::class)->execute([
app(ResetAccount::class)->execute([
'account_id' => $account->id,
]);

$tables = DBHelper::getTables();

// TODO([email protected]): 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'));
}
Expand Down
4 changes: 3 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function register()
\App\Services\Account\Company\CreateCompany::class => \App\Services\Account\Company\CreateCompany::class,
\App\Services\Account\Company\DestroyCompany::class => \App\Services\Account\Company\DestroyCompany::class,
\App\Services\Account\Company\UpdateCompany::class => \App\Services\Account\Company\UpdateCompany::class,
\App\Services\Account\DestroyAllDocuments::class => \App\Services\Account\DestroyAllDocuments::class,
\App\Services\Account\Settings\DestroyAllDocuments::class => \App\Services\Account\Settings\DestroyAllDocuments::class,
\App\Services\Account\Gender\CreateGender::class => \App\Services\Account\Gender\CreateGender::class,
\App\Services\Account\Gender\DestroyGender::class => \App\Services\Account\Gender\DestroyGender::class,
\App\Services\Account\Gender\UpdateGender::class => \App\Services\Account\Gender\UpdateGender::class,
Expand Down Expand Up @@ -153,5 +153,7 @@ public function register()
\App\Services\VCard\ExportVCard::class => \App\Services\VCard\ExportVCard::class,
\App\Services\VCard\ImportVCard::class => \App\Services\VCard\ImportVCard::class,
\App\Services\Account\Settings\ExportAccount::class => \App\Services\Account\Settings\ExportAccount::class,
\App\Services\Account\Settings\ResetAccount::class => \App\Services\Account\Settings\ResetAccount::class,
\App\Services\Account\Settings\DestroyAccount::class => \App\Services\Account\Settings\DestroyAccount::class,
];
}
88 changes: 88 additions & 0 deletions app/Services/Account/Settings/DestroyAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace App\Services\Account\Settings;

use App\Services\BaseService;
use App\Models\Account\Account;
use App\Exceptions\StripeException;

class DestroyAccount extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
];
}

/**
* Completely delete an account.
*
* @param array $data
* @throws StripeException
* @return void
*/
public function execute(array $data): void
{
$this->validate($data);

$account = Account::find($data['account_id']);

$this->destroyDocuments($account);

$this->destroyPhotos($account);

$this->cancelStripe($account);

$account->delete();
}

/**
* Destroy the documents.
*
* @param Account $account
* @return void
*/
private function destroyDocuments(Account $account)
{
app(DestroyAllDocuments::class)->execute([
'account_id' => $account->id,
]);
}

/**
* Destroy the photos.
*
* @param Account $account
* @return void
*/
private function destroyPhotos(Account $account)
{
app(DestroyAllPhotos::class)->execute([
'account_id' => $account->id,
]);
}

/**
* Cancel Stripe subscription.
*
* @param Account $account
* @throws StripeException
* @return void
*/
private function cancelStripe(Account $account)
{
if ($account->isSubscribed() && ! $account->has_access_to_paid_version_for_free) {
try {
$account->subscriptionCancel();
} catch (StripeException $e) {
throw new StripeException();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Services\Account;
namespace App\Services\Account\Settings;

use App\Services\BaseService;
use App\Models\Contact\Document;
Expand Down
49 changes: 49 additions & 0 deletions app/Services/Account/Settings/DestroyAllPhotos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Services\Account\Settings;

use App\Models\Account\Photo;
use App\Services\BaseService;
use Illuminate\Support\Facades\Storage;
use Illuminate\Contracts\Filesystem\FileNotFoundException;

class DestroyAllPhotos extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
];
}

/**
* Destroy all photos in an account.
*
* @param array $data
* @return bool
*/
public function execute(array $data): bool
{
$this->validate($data);

$photos = Photo::where('account_id', $data['account_id'])
->get();

foreach ($photos as $photo) {
try {
Storage::delete($photo->new_filename);
} catch (FileNotFoundException $e) {
continue;
}

$photo->delete();
}

return true;
}
}
26 changes: 0 additions & 26 deletions app/Services/Account/Settings/ExportAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ private function writeExport(array $data, User $user)
$this->exportMetaDataLoveRelationship($data);
$this->exportModule($data);
$this->exportNote($data);
$this->exportNotification($data);
$this->exportOccupation($data);
$this->exportPet($data);
$this->exportPhoto($data);
Expand Down Expand Up @@ -910,31 +909,6 @@ private function exportNote(array $data)
$this->buildInsertSQLQuery('notes', $foreignKey, $columns, $data);
}

/**
* Export the Notification table.
*
* @param array $data
*/
private function exportNotification(array $data)
{
$columns = [
'id',
'account_id',
'contact_id',
'reminder_id',
'delete_after_number_of_emails_sent',
'number_of_emails_sent',
'trigger_date',
'scheduled_number_days_before',
'created_at',
'updated_at',
];

$foreignKey = 'account_id';

$this->buildInsertSQLQuery('notifications', $foreignKey, $columns, $data);
}

/**
* Export the Occupation table.
*
Expand Down
Loading