From 4959917b7e2e9a5c4fb6fe4aaa381ac073d83a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 3 Dec 2020 15:33:18 +0100 Subject: [PATCH] fix issue with multiple companies switching --- app/Console/Commands/ImportMigrations.php | 30 +++++------ app/Http/Kernel.php | 2 + app/Http/Middleware/CheckClientExistence.php | 54 +++++++++++++++++++ app/Http/ViewComposers/PortalComposer.php | 3 +- app/Mail/DownloadInvoices.php | 1 - app/Mail/Engine/BaseEmailEngine.php | 1 - app/Mail/ExistingMigration.php | 1 - app/Mail/MigrationFailed.php | 1 - app/PaymentDrivers/CheckoutCom/CreditCard.php | 2 +- app/Services/Payment/RefundPayment.php | 2 - config/ninja.php | 2 +- routes/client.php | 2 +- 12 files changed, 74 insertions(+), 27 deletions(-) create mode 100644 app/Http/Middleware/CheckClientExistence.php diff --git a/app/Console/Commands/ImportMigrations.php b/app/Console/Commands/ImportMigrations.php index 069cf5ebcf..aee1fa3d98 100644 --- a/app/Console/Commands/ImportMigrations.php +++ b/app/Console/Commands/ImportMigrations.php @@ -19,7 +19,6 @@ use App\Exceptions\ResourceDependencyMissing; use App\Exceptions\ResourceNotAvailableForMigration; use App\Jobs\Util\Import; use App\Jobs\Util\StartMigration; -use App\Libraries\MultiDB; use App\Mail\MigrationFailed; use App\Models\Account; use App\Models\Company; @@ -85,7 +84,6 @@ class ImportMigrations extends Command foreach ($directory as $file) { if ($file->getExtension() === 'zip') { - $user = $this->getUser(); $company = $this->getUser()->companies()->first(); @@ -99,24 +97,24 @@ class ImportMigrations extends Command throw new ProcessingMigrationArchiveFailed('Processing migration archive failed. Migration file is possibly corrupted.'); } - $filename = pathinfo($file->getRealPath(), PATHINFO_FILENAME); + $filename = pathinfo($file->getRealPath(), PATHINFO_FILENAME); - $zip->extractTo(public_path("storage/migrations/{$filename}")); - $zip->close(); + $zip->extractTo(public_path("storage/migrations/{$filename}")); + $zip->close(); - $import_file = public_path("storage/migrations/$filename/migration.json"); + $import_file = public_path("storage/migrations/$filename/migration.json"); - Import::dispatch($import_file, $this->getUser()->companies()->first(), $this->getUser()); - // StartMigration::dispatch($file->getRealPath(), $this->getUser(), $this->getUser()->companies()->first()); + Import::dispatch($import_file, $this->getUser()->companies()->first(), $this->getUser()); + // StartMigration::dispatch($file->getRealPath(), $this->getUser(), $this->getUser()->companies()->first()); + } catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) { + \Mail::to($this->user)->send(new MigrationFailed($e, $e->getMessage())); + + if (app()->environment() !== 'production') { + info($e->getMessage()); + } } - catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) { - \Mail::to($this->user)->send(new MigrationFailed($e, $e->getMessage())); - - if (app()->environment() !== 'production') { - info($e->getMessage()); - } - - }}} + } + } } public function getUser(): User diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index ad2bc8595f..99dbe7bf29 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -13,6 +13,7 @@ namespace App\Http; use App\Http\Middleware\ApiSecretCheck; use App\Http\Middleware\Authenticate; +use App\Http\Middleware\CheckClientExistence; use App\Http\Middleware\CheckForMaintenanceMode; use App\Http\Middleware\ClientPortalEnabled; use App\Http\Middleware\ContactKeyLogin; @@ -155,5 +156,6 @@ class Kernel extends HttpKernel 'shop_token_auth' => ShopTokenAuth::class, 'phantom_secret' => PhantomSecret::class, 'contact_key_login' => ContactKeyLogin::class, + 'check_client_existence' => CheckClientExistence::class, ]; } diff --git a/app/Http/Middleware/CheckClientExistence.php b/app/Http/Middleware/CheckClientExistence.php new file mode 100644 index 0000000000..9252b42441 --- /dev/null +++ b/app/Http/Middleware/CheckClientExistence.php @@ -0,0 +1,54 @@ +where('email', auth('contact')->user()->email) + ->whereNotNull('email') + ->distinct('company_id') + ->whereHas('client', function ($query) { + return $query->whereNull('deleted_at'); + }) + ->get(); + + if (count($multiple_contacts) == 0) { + Auth::logout(); + + return redirect()->route('client.login'); + } + + if (count($multiple_contacts) == 1) { + Auth::guard('contact')->login($multiple_contacts[0], true); + } + + session()->put('multiple_contacts', $multiple_contacts); + + return $next($request); + } +} diff --git a/app/Http/ViewComposers/PortalComposer.php b/app/Http/ViewComposers/PortalComposer.php index 5d36d37c4e..e8c046df42 100644 --- a/app/Http/ViewComposers/PortalComposer.php +++ b/app/Http/ViewComposers/PortalComposer.php @@ -11,7 +11,6 @@ namespace App\Http\ViewComposers; -use App\Models\ClientContact; use App\Utils\Ninja; use App\Utils\TranslationHelper; use Illuminate\Support\Facades\Lang; @@ -55,7 +54,7 @@ class PortalComposer $data['settings'] = auth()->user()->client->getMergedSettings(); $data['currencies'] = TranslationHelper::getCurrencies(); - $data['multiple_contacts'] = ClientContact::where('email', auth('contact')->user()->email)->whereNotNull('email')->distinct('company_id')->get(); + $data['multiple_contacts'] = session()->get('multiple_contacts'); return $data; } diff --git a/app/Mail/DownloadInvoices.php b/app/Mail/DownloadInvoices.php index 2c65d34ee1..55859e1d72 100644 --- a/app/Mail/DownloadInvoices.php +++ b/app/Mail/DownloadInvoices.php @@ -30,7 +30,6 @@ class DownloadInvoices extends Mailable */ public function build() { - return $this->from(config('mail.from.name'), config('mail.from.address')) ->subject(ctrans('texts.download_files')) ->markdown( diff --git a/app/Mail/Engine/BaseEmailEngine.php b/app/Mail/Engine/BaseEmailEngine.php index f1b8a7b6cc..482f99a3b5 100644 --- a/app/Mail/Engine/BaseEmailEngine.php +++ b/app/Mail/Engine/BaseEmailEngine.php @@ -83,7 +83,6 @@ class BaseEmailEngine implements EngineInterface public function setAttachments($attachments) { - $this->attachments = array_merge($this->getAttachments(), $attachments); return $this; diff --git a/app/Mail/ExistingMigration.php b/app/Mail/ExistingMigration.php index 596947b7fc..a050fcb988 100644 --- a/app/Mail/ExistingMigration.php +++ b/app/Mail/ExistingMigration.php @@ -27,7 +27,6 @@ class ExistingMigration extends Mailable */ public function build() { - return $this->from(config('mail.from.name'), config('mail.from.address')) ->view('email.migration.existing'); } diff --git a/app/Mail/MigrationFailed.php b/app/Mail/MigrationFailed.php index 545736fd0b..e4bf4ef371 100644 --- a/app/Mail/MigrationFailed.php +++ b/app/Mail/MigrationFailed.php @@ -31,7 +31,6 @@ class MigrationFailed extends Mailable */ public function build() { - return $this->from(config('mail.from.name'), config('mail.from.address')) ->view('email.migration.failed'); } diff --git a/app/PaymentDrivers/CheckoutCom/CreditCard.php b/app/PaymentDrivers/CheckoutCom/CreditCard.php index e31c912425..aeffd15335 100644 --- a/app/PaymentDrivers/CheckoutCom/CreditCard.php +++ b/app/PaymentDrivers/CheckoutCom/CreditCard.php @@ -105,7 +105,7 @@ class CreditCard } private function attemptPaymentUsingCreditCard(PaymentResponseRequest $request) - { + { $checkout_response = $this->checkout->payment_hash->data->server_response; $method = new TokenSource( diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index c42ba39ae3..9b3cabe37a 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -73,7 +73,6 @@ class RefundPayment { if ($this->refund_data['gateway_refund'] !== false && $this->total_refund > 0) { if ($this->payment->company_gateway) { - $response = $this->payment->company_gateway->driver($this->payment->client)->refund($this->payment, $this->total_refund); $this->payment->refunded += $this->total_refund; @@ -84,7 +83,6 @@ class RefundPayment $this->payment->save(); throw new PaymentRefundFailed(); } - } } else { $this->payment->refunded += $this->total_refund; diff --git a/config/ninja.php b/config/ninja.php index 7276e438f2..236b2b05b5 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -23,7 +23,7 @@ return [ 'date_time_format' => 'Y-m-d H:i', 'daily_email_limit' => 300, 'error_email' => env('ERROR_EMAIL', ''), - 'mailer' => env('MAIL_MAILER',''), + 'mailer' => env('MAIL_MAILER', ''), 'company_id' => 0, 'hash_salt' => env('HASH_SALT', ''), 'currency_converter_api_key' => env('OPENEXCHANGE_APP_ID', ''), diff --git a/routes/client.php b/routes/client.php index 7828330157..2a26edee87 100644 --- a/routes/client.php +++ b/routes/client.php @@ -24,7 +24,7 @@ Route::get('tmp_pdf/{hash}', 'ClientPortal\TempRouteController@index')->name('tm Route::get('client/key_login/{contact_key}', 'ClientPortal\ContactHashLoginController@login')->name('client.contact_login')->middleware(['contact_key_login']); //todo implement domain DB -Route::group(['middleware' => ['auth:contact', 'locale'], 'prefix' => 'client', 'as' => 'client.'], function () { +Route::group(['middleware' => ['auth:contact', 'locale', 'check_client_existence'], 'prefix' => 'client', 'as' => 'client.'], function () { Route::get('dashboard', 'ClientPortal\DashboardController@index')->name('dashboard'); // name = (dashboard. index / create / show / update / destroy / edit Route::get('invoices', 'ClientPortal\InvoiceController@index')->name('invoices.index')->middleware('portal_enabled');