diff --git a/app/Http/Controllers/ImportJsonController.php b/app/Http/Controllers/ImportJsonController.php index 3f5a2054dc79..7f17eb43d1a5 100644 --- a/app/Http/Controllers/ImportJsonController.php +++ b/app/Http/Controllers/ImportJsonController.php @@ -15,6 +15,7 @@ use App\Exceptions\NonExistingMigrationFile; use App\Http\Requests\Import\ImportJsonRequest; use App\Jobs\Company\CompanyExport; use App\Jobs\Company\CompanyImport; +use App\Utils\Ninja; use App\Utils\Traits\MakesHash; use Illuminate\Http\Response; use Illuminate\Support\Facades\Cache; @@ -63,15 +64,12 @@ class ImportJsonController extends BaseController $import_file = $request->file('files'); - $contents = $this->unzipFile($import_file->getPathname()); + $file_location = $this->unzipFile($import_file->getPathname()); - $hash = Str::random(32); - - nlog($hash); - - Cache::put( $hash, base64_encode( $contents ), 3600 ); - - CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $hash, $request->except('files'))->delay(now()->addMinutes(1))->onQueue('himem');; + if(Ninja::isHosted()) + CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $file_location, $request->except('files'))->onQueue('himem'); + else + CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $file_location, $request->except('files')); return response()->json(['message' => 'Processing'], 200); @@ -90,11 +88,12 @@ class ImportJsonController extends BaseController if (! file_exists($file_location)) throw new NonExistingMigrationFile('Backup file does not exist, or is corrupted.'); - $data = file_get_contents($file_location); + //$data = file_get_contents($file_location); - unlink($file_contents); - unlink($file_location); + return $file_location; + //unlink($file_contents); + //unlink($file_location); - return $data; + //return $data; } } diff --git a/app/Http/Middleware/ContactRegister.php b/app/Http/Middleware/ContactRegister.php index 116dfe22d472..ade62aa84974 100644 --- a/app/Http/Middleware/ContactRegister.php +++ b/app/Http/Middleware/ContactRegister.php @@ -33,7 +33,8 @@ class ContactRegister if($company) { - abort_unless($company->client_can_register, 404); + if(! $company->client_can_register) + abort(400, 'Registration disabled'); $request->merge(['key' => $company->company_key]); @@ -49,7 +50,9 @@ class ContactRegister if($company = Company::where($query)->first()) { - abort_unless($company->client_can_register, 404); + + if(! $company->client_can_register) + abort(400, 'Registration disabled'); $request->merge(['key' => $company->company_key]); @@ -62,7 +65,10 @@ class ContactRegister if ($request->route()->parameter('company_key') && Ninja::isSelfHost()) { $company = Company::where('company_key', $request->company_key)->firstOrFail(); - abort_unless($company->client_can_register, 404); + if(! (bool)$company->client_can_register); + abort(400, 'Registration disabled'); + + $request->merge(['key' => $company->company_key]); return $next($request); } @@ -72,7 +78,8 @@ class ContactRegister if (!$request->route()->parameter('company_key') && Ninja::isSelfHost()) { $company = Account::first()->default_company; - abort_unless($company->client_can_register, 404); + if(! $company->client_can_register) + abort(400, 'Registration disabled'); $request->merge(['key' => $company->company_key]); diff --git a/app/Http/Requests/ClientPortal/RegisterRequest.php b/app/Http/Requests/ClientPortal/RegisterRequest.php index 6b74201d79d3..8ec7d723b8d2 100644 --- a/app/Http/Requests/ClientPortal/RegisterRequest.php +++ b/app/Http/Requests/ClientPortal/RegisterRequest.php @@ -2,6 +2,7 @@ namespace App\Http\Requests\ClientPortal; +use App\Libraries\MultiDB; use App\Models\Account; use App\Models\Company; use App\Utils\Ninja; @@ -36,10 +37,11 @@ class RegisterRequest extends FormRequest } public function company() - { - if ($this->subdomain) { - return Company::where('subdomain', $this->subdomain)->firstOrFail(); - } + { + + //this should be all we need, the rest SHOULD be redundant because of our Middleware + if ($this->key) + return Company::where('company_key', $this->key)->first(); if ($this->company_key) { return Company::where('company_key', $this->company_key)->firstOrFail(); @@ -48,11 +50,34 @@ class RegisterRequest extends FormRequest if (!$this->route()->parameter('company_key') && Ninja::isSelfHost()) { $company = Account::first()->default_company; - abort_unless($company->client_can_register, 404); + if(!$company->client_can_register) + abort(403, "This page is restricted"); return $company; } - abort(404, 'Register request not found.'); + if (Ninja::isHosted()) { + + $subdomain = explode('.', $this->getHost())[0]; + + $query = [ + 'subdomain' => $subdomain, + 'portal_mode' => 'subdomain', + ]; + + if($company = MultiDB::findAndSetDbByDomain($query)) + return $company; + + $query = [ + 'portal_domain' => $this->getSchemeAndHttpHost(), + 'portal_mode' => 'domain', + ]; + + if($company = MultiDB::findAndSetDbByDomain($query)) + return $company; + + } + + abort(400, 'Register request not found.'); } } diff --git a/app/Jobs/Company/CompanyImport.php b/app/Jobs/Company/CompanyImport.php index 81e4ffe1154d..c07ade77c403 100644 --- a/app/Jobs/Company/CompanyImport.php +++ b/app/Jobs/Company/CompanyImport.php @@ -85,7 +85,7 @@ class CompanyImport implements ShouldQueue public $user; - private $hash; + private $file_location; public $backup_file; @@ -146,11 +146,11 @@ class CompanyImport implements ShouldQueue * @param string $hash - the cache hash of the import data. * @param array $request->all() */ - public function __construct(Company $company, User $user, string $hash, array $request_array) + public function __construct(Company $company, User $user, string $file_location, array $request_array) { $this->company = $company; $this->user = $user; - $this->hash = $hash; + $this->file_location = $file_location; $this->request_array = $request_array; $this->current_app_version = config('ninja.app_version'); } @@ -164,14 +164,14 @@ class CompanyImport implements ShouldQueue $this->company_owner = $this->company->owner(); nlog("Company ID = {$this->company->id}"); - nlog("Hash ID = {$this->hash}"); + nlog("file_location ID = {$this->file_location}"); - $this->backup_file = Cache::get($this->hash); + // $this->backup_file = Cache::get($this->hash); - if ( empty( $this->backup_file ) ) + if ( empty( $this->file_location ) ) throw new \Exception('No import data found, has the cache expired?'); - $this->backup_file = json_decode(base64_decode($this->backup_file)); + $this->backup_file = json_decode(file_get_contents($this->file_location)); // nlog($this->backup_file); $this->checkUserCount(); @@ -198,6 +198,8 @@ class CompanyImport implements ShouldQueue } + unlink($this->file_location); + } /**