From 7c126169c8a331dfad6a76c58c99ad0ccf326c91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Fri, 25 Dec 2020 12:08:15 +0300 Subject: [PATCH] removed session from jobs --- app/BulkActions/Common/Companies.php | 6 +++--- app/Console/Commands/Update.php | 9 ++++++--- app/Http/Controllers/Api/Common/Companies.php | 8 ++++---- app/Http/Controllers/Common/Companies.php | 8 ++++---- app/Http/Controllers/Install/Updates.php | 2 +- app/Http/Controllers/Modules/Item.php | 8 ++++---- app/Jobs/Banking/CreateBankingDocumentTransaction.php | 2 +- app/Jobs/Common/CreateContact.php | 2 +- app/Jobs/Common/DeleteCompany.php | 8 +++++--- app/Jobs/Common/UpdateCompany.php | 7 +++++-- app/Jobs/Common/UpdateContact.php | 2 +- app/Jobs/Install/DisableModule.php | 4 ++-- app/Jobs/Install/DownloadModule.php | 4 ++-- app/Jobs/Install/EnableModule.php | 4 ++-- app/Jobs/Install/FinishUpdate.php | 8 ++++++-- app/Jobs/Install/InstallModule.php | 4 ++-- app/Jobs/Install/UninstallModule.php | 4 ++-- 17 files changed, 51 insertions(+), 39 deletions(-) diff --git a/app/BulkActions/Common/Companies.php b/app/BulkActions/Common/Companies.php index 1a385ec37..d8a848075 100644 --- a/app/BulkActions/Common/Companies.php +++ b/app/BulkActions/Common/Companies.php @@ -35,7 +35,7 @@ class Companies extends BulkAction foreach ($companies as $company) { try { - $this->dispatch(new UpdateCompany($company, $request->merge(['enabled' => 1]))); + $this->dispatch(new UpdateCompany($company, $request->merge(['enabled' => 1]), session('company_id'))); } catch (\Exception $e) { flash($e->getMessage())->error(); } @@ -48,7 +48,7 @@ class Companies extends BulkAction foreach ($companies as $company) { try { - $this->dispatch(new UpdateCompany($company, $request->merge(['enabled' => 0]))); + $this->dispatch(new UpdateCompany($company, $request->merge(['enabled' => 0]), session('company_id'))); } catch (\Exception $e) { flash($e->getMessage())->error(); } @@ -61,7 +61,7 @@ class Companies extends BulkAction foreach ($companies as $company) { try { - $this->dispatch(new DeleteCompany($company)); + $this->dispatch(new DeleteCompany($company, session('company_id'))); } catch (\Exception $e) { flash($e->getMessage())->error(); } diff --git a/app/Console/Commands/Update.php b/app/Console/Commands/Update.php index 63a1ebf0c..2bdeec8c1 100644 --- a/app/Console/Commands/Update.php +++ b/app/Console/Commands/Update.php @@ -23,6 +23,8 @@ class Update extends Command public $alias; + public $company; + public $new; public $old; @@ -51,6 +53,7 @@ class Update extends Command set_time_limit(3600); // 1 hour $this->alias = $this->argument('alias'); + $this->company = $this->argument('company'); if (false === $this->new = $this->getNewVersion()) { $this->error('Not able to get the latest version of ' . $this->alias . '!'); @@ -60,8 +63,8 @@ class Update extends Command $this->old = $this->getOldVersion(); - session(['company_id' => $this->argument('company')]); - setting()->setExtraColumns(['company_id' => $this->argument('company')]); + session(['company_id' => $this->company]); + setting()->setExtraColumns(['company_id' => $this->company]); if (!$path = $this->download()) { return self::CMD_ERROR; @@ -156,7 +159,7 @@ class Update extends Command $this->info('Finishing update...'); try { - $this->dispatch(new FinishUpdate($this->alias, $this->new, $this->old)); + $this->dispatch(new FinishUpdate($this->alias, $this->new, $this->old, $this->company)); } catch (\Exception $e) { $this->error($e->getMessage()); diff --git a/app/Http/Controllers/Api/Common/Companies.php b/app/Http/Controllers/Api/Common/Companies.php index 625467a35..7ad852f75 100644 --- a/app/Http/Controllers/Api/Common/Companies.php +++ b/app/Http/Controllers/Api/Common/Companies.php @@ -69,7 +69,7 @@ class Companies extends ApiController public function update(Company $company, Request $request) { try { - $company = $this->dispatch(new UpdateCompany($company, $request)); + $company = $this->dispatch(new UpdateCompany($company, $request, session('company_id'))); return $this->item($company->fresh(), new Transformer()); } catch (\Exception $e) { @@ -86,7 +86,7 @@ class Companies extends ApiController public function enable(Company $company) { try { - $company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 1]))); + $company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 1]), session('company_id'))); return $this->item($company->fresh(), new Transformer()); } catch (\Exception $e) { @@ -103,7 +103,7 @@ class Companies extends ApiController public function disable(Company $company) { try { - $company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 0]))); + $company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 0]), session('company_id'))); return $this->item($company->fresh(), new Transformer()); } catch (\Exception $e) { @@ -120,7 +120,7 @@ class Companies extends ApiController public function destroy(Company $company) { try { - $this->dispatch(new DeleteCompany($company)); + $this->dispatch(new DeleteCompany($company, session('company_id'))); return $this->response->noContent(); } catch (\Exception $e) { diff --git a/app/Http/Controllers/Common/Companies.php b/app/Http/Controllers/Common/Companies.php index 3876b7574..b5cf85efa 100644 --- a/app/Http/Controllers/Common/Companies.php +++ b/app/Http/Controllers/Common/Companies.php @@ -115,7 +115,7 @@ class Companies extends Controller { $company_id = session('company_id'); - $response = $this->ajaxDispatch(new UpdateCompany($company, $request)); + $response = $this->ajaxDispatch(new UpdateCompany($company, $request, session('company_id'))); if ($response['success']) { $response['redirect'] = route('companies.index'); @@ -147,7 +147,7 @@ class Companies extends Controller */ public function enable(Company $company) { - $response = $this->ajaxDispatch(new UpdateCompany($company, request()->merge(['enabled' => 1]))); + $response = $this->ajaxDispatch(new UpdateCompany($company, request()->merge(['enabled' => 1]), session('company_id'))); if ($response['success']) { $response['message'] = trans('messages.success.enabled', ['type' => trans_choice('general.companies', 1)]); @@ -165,7 +165,7 @@ class Companies extends Controller */ public function disable(Company $company) { - $response = $this->ajaxDispatch(new UpdateCompany($company, request()->merge(['enabled' => 0]))); + $response = $this->ajaxDispatch(new UpdateCompany($company, request()->merge(['enabled' => 0]), session('company_id'))); if ($response['success']) { $response['message'] = trans('messages.success.disabled', ['type' => trans_choice('general.companies', 1)]); @@ -183,7 +183,7 @@ class Companies extends Controller */ public function destroy(Company $company) { - $response = $this->ajaxDispatch(new DeleteCompany($company)); + $response = $this->ajaxDispatch(new DeleteCompany($company, session('company_id'))); $response['redirect'] = route('companies.index'); diff --git a/app/Http/Controllers/Install/Updates.php b/app/Http/Controllers/Install/Updates.php index 69d798380..f62b2c2c8 100644 --- a/app/Http/Controllers/Install/Updates.php +++ b/app/Http/Controllers/Install/Updates.php @@ -274,7 +274,7 @@ class Updates extends Controller set_time_limit(900); // 15 minutes try { - $this->dispatch(new FinishUpdate($request['alias'], $request['version'], $request['installed'])); + $this->dispatch(new FinishUpdate($request['alias'], $request['version'], $request['installed'], session('company_id'))); $json = [ 'success' => true, diff --git a/app/Http/Controllers/Modules/Item.php b/app/Http/Controllers/Modules/Item.php index 190968de3..ca72b8d7f 100644 --- a/app/Http/Controllers/Modules/Item.php +++ b/app/Http/Controllers/Modules/Item.php @@ -236,7 +236,7 @@ class Item extends Controller public function install(Request $request) { try { - $this->dispatch(new InstallModule($request['alias'])); + $this->dispatch(new InstallModule($request['alias'], session('company_id'))); $name = module($request['alias'])->getName(); @@ -275,7 +275,7 @@ class Item extends Controller try { $name = module($alias)->getName(); - $this->dispatch(new UninstallModule($alias)); + $this->dispatch(new UninstallModule($alias, session('company_id'))); $message = trans('modules.uninstalled', ['module' => $name]); @@ -294,7 +294,7 @@ class Item extends Controller try { $name = module($alias)->getName(); - $this->dispatch(new EnableModule($alias)); + $this->dispatch(new EnableModule($alias, session('company_id'))); $message = trans('modules.enabled', ['module' => $name]); @@ -313,7 +313,7 @@ class Item extends Controller try { $name = module($alias)->getName(); - $this->dispatch(new DisableModule($alias)); + $this->dispatch(new DisableModule($alias, session('company_id'))); $message = trans('modules.disabled', ['module' => $name]); diff --git a/app/Jobs/Banking/CreateBankingDocumentTransaction.php b/app/Jobs/Banking/CreateBankingDocumentTransaction.php index c58642684..cc1b89a47 100644 --- a/app/Jobs/Banking/CreateBankingDocumentTransaction.php +++ b/app/Jobs/Banking/CreateBankingDocumentTransaction.php @@ -71,7 +71,7 @@ class CreateBankingDocumentTransaction extends Job $this->request['amount'] = $this->model->amount - $this->model->paid_amount; } - $this->request['company_id'] = session('company_id'); + $this->request['company_id'] = $this->model->company_id; $this->request['currency_code'] = isset($this->request['currency_code']) ? $this->request['currency_code'] : $this->model->currency_code; $this->request['type'] = ($this->model->type === Document::INVOICE_TYPE) ? 'income' : 'expense'; $this->request['paid_at'] = isset($this->request['paid_at']) ? $this->request['paid_at'] : Date::now()->format('Y-m-d'); diff --git a/app/Jobs/Common/CreateContact.php b/app/Jobs/Common/CreateContact.php index 9f14e88d5..cb9460d2d 100644 --- a/app/Jobs/Common/CreateContact.php +++ b/app/Jobs/Common/CreateContact.php @@ -59,7 +59,7 @@ class CreateContact extends Job $user = User::create($data); $user->roles()->attach($customer_role); - $user->companies()->attach(session('company_id')); + $user->companies()->attach($data['company_id']); $this->request['user_id'] = $user->id; } diff --git a/app/Jobs/Common/DeleteCompany.php b/app/Jobs/Common/DeleteCompany.php index 3996900c4..6801cd57a 100644 --- a/app/Jobs/Common/DeleteCompany.php +++ b/app/Jobs/Common/DeleteCompany.php @@ -3,7 +3,6 @@ namespace App\Jobs\Common; use App\Abstracts\Job; -use App\Models\Common\Company; use App\Traits\Users; class DeleteCompany extends Job @@ -12,14 +11,17 @@ class DeleteCompany extends Job protected $company; + protected $active_company_id; + /** * Create a new job instance. * * @param $request */ - public function __construct($company) + public function __construct($company, $active_company_id) { $this->company = $company; + $this->active_company_id = $active_company_id; } /** @@ -53,7 +55,7 @@ class DeleteCompany extends Job public function authorize() { // Can't delete active company - if ($this->company->id == session('company_id')) { + if ($this->company->id == $this->active_company_id) { $message = trans('companies.error.delete_active'); throw new \Exception($message); diff --git a/app/Jobs/Common/UpdateCompany.php b/app/Jobs/Common/UpdateCompany.php index 517eb7fad..d4137ad6e 100644 --- a/app/Jobs/Common/UpdateCompany.php +++ b/app/Jobs/Common/UpdateCompany.php @@ -16,16 +16,19 @@ class UpdateCompany extends Job protected $request; + protected $active_company_id; + /** * Create a new job instance. * * @param $company * @param $request */ - public function __construct($company, $request) + public function __construct($company, $request, $active_company_id) { $this->company = $company; $this->request = $this->getRequestInstance($request); + $this->active_company_id = $active_company_id; } /** @@ -94,7 +97,7 @@ class UpdateCompany extends Job public function authorize() { // Can't disable active company - if (($this->request->get('enabled', 1) == 0) && ($this->company->id == session('company_id'))) { + if (($this->request->get('enabled', 1) == 0) && ($this->company->id == $this->active_company_id)) { $message = trans('companies.error.disable_active'); throw new \Exception($message); diff --git a/app/Jobs/Common/UpdateContact.php b/app/Jobs/Common/UpdateContact.php index 1bfa3d1ca..116fb3559 100644 --- a/app/Jobs/Common/UpdateContact.php +++ b/app/Jobs/Common/UpdateContact.php @@ -77,7 +77,7 @@ class UpdateContact extends Job $user = User::create($data); $user->roles()->attach($customer_role); - $user->companies()->attach(session('company_id')); + $user->companies()->attach($data['company_id']); $this->request['user_id'] = $user->id; } diff --git a/app/Jobs/Install/DisableModule.php b/app/Jobs/Install/DisableModule.php index c561bc49a..6f7d5d8ea 100644 --- a/app/Jobs/Install/DisableModule.php +++ b/app/Jobs/Install/DisableModule.php @@ -20,10 +20,10 @@ class DisableModule extends Job * @param $company_id * @param $locale */ - public function __construct($alias, $company_id = null, $locale = null) + public function __construct($alias, $company_id, $locale = null) { $this->alias = $alias; - $this->company_id = $company_id ?: session('company_id'); + $this->company_id = $company_id; $this->locale = $locale ?: app()->getLocale(); } diff --git a/app/Jobs/Install/DownloadModule.php b/app/Jobs/Install/DownloadModule.php index 996c4b2dd..3e7ab89b4 100644 --- a/app/Jobs/Install/DownloadModule.php +++ b/app/Jobs/Install/DownloadModule.php @@ -17,10 +17,10 @@ class DownloadModule extends Job * @param $alias * @param $company_id */ - public function __construct($alias, $company_id = null) + public function __construct($alias, $company_id) { $this->alias = $alias; - $this->company_id = $company_id ?: session('company_id'); + $this->company_id = $company_id; } /** diff --git a/app/Jobs/Install/EnableModule.php b/app/Jobs/Install/EnableModule.php index 6acb41d81..4f40b30eb 100644 --- a/app/Jobs/Install/EnableModule.php +++ b/app/Jobs/Install/EnableModule.php @@ -20,10 +20,10 @@ class EnableModule extends Job * @param $company_id * @param $locale */ - public function __construct($alias, $company_id = null, $locale = null) + public function __construct($alias, $company_id, $locale = null) { $this->alias = $alias; - $this->company_id = $company_id ?: session('company_id'); + $this->company_id = $company_id; $this->locale = $locale ?: app()->getLocale(); } diff --git a/app/Jobs/Install/FinishUpdate.php b/app/Jobs/Install/FinishUpdate.php index fa6d606f3..6c5dc6f2a 100644 --- a/app/Jobs/Install/FinishUpdate.php +++ b/app/Jobs/Install/FinishUpdate.php @@ -14,18 +14,22 @@ class FinishUpdate extends Job protected $old; + protected $company_id; + /** * Create a new job instance. * * @param $alias * @param $new * @param $old + * @param $company_id */ - public function __construct($alias, $new, $old) + public function __construct($alias, $new, $old, $company_id) { $this->alias = $alias; $this->new = $new; $this->old = $old; + $this->company_id = $company_id; } /** @@ -36,7 +40,7 @@ class FinishUpdate extends Job public function handle() { if ($this->alias == 'core') { - $companies = [session('company_id')]; + $companies = [$this->company_id]; } else { $companies = Module::alias($this->alias)->allCompanies()->cursor(); } diff --git a/app/Jobs/Install/InstallModule.php b/app/Jobs/Install/InstallModule.php index 72d60a1dd..705c5fb83 100644 --- a/app/Jobs/Install/InstallModule.php +++ b/app/Jobs/Install/InstallModule.php @@ -20,10 +20,10 @@ class InstallModule extends Job * @param $company_id * @param $locale */ - public function __construct($alias, $company_id = null, $locale = null) + public function __construct($alias, $company_id, $locale = null) { $this->alias = $alias; - $this->company_id = $company_id ?: session('company_id'); + $this->company_id = $company_id; $this->locale = $locale ?: app()->getLocale(); } diff --git a/app/Jobs/Install/UninstallModule.php b/app/Jobs/Install/UninstallModule.php index f9eac64ca..9a82c2dc1 100644 --- a/app/Jobs/Install/UninstallModule.php +++ b/app/Jobs/Install/UninstallModule.php @@ -20,10 +20,10 @@ class UninstallModule extends Job * @param $company_id * @param $locale */ - public function __construct($alias, $company_id = null, $locale = null) + public function __construct($alias, $company_id, $locale = null) { $this->alias = $alias; - $this->company_id = $company_id ?: session('company_id'); + $this->company_id = $company_id; $this->locale = $locale ?: app()->getLocale(); }