diff --git a/app/Factory/CompanyFactory.php b/app/Factory/CompanyFactory.php index 5faf3df95cf4..6c58bf2a2216 100644 --- a/app/Factory/CompanyFactory.php +++ b/app/Factory/CompanyFactory.php @@ -12,7 +12,9 @@ namespace App\Factory; use App\DataMapper\CompanySettings; +use App\Libraries\MultiDB; use App\Models\Company; +use App\Utils\Ninja; use App\Utils\Traits\MakesHash; class CompanyFactory @@ -33,7 +35,12 @@ class CompanyFactory $company->db = config('database.default'); //$company->custom_fields = (object) ['invoice1' => '1', 'invoice2' => '2', 'client1'=>'3']; $company->custom_fields = (object) []; - $company->subdomain = ''; + + if(Ninja::isHosted()) + $company->subdomain = MultiDB::randomSubdomainGenerator(); + else + $company->subdomain = ''; + $company->enabled_modules = config('ninja.enabled_modules'); //32767;//8191; //4095 $company->default_password_timeout = 1800000; diff --git a/app/Http/ValidationRules/UniqueUserRule.php b/app/Http/ValidationRules/UniqueUserRule.php index b9bca3a2e5bc..e6eb7be90782 100644 --- a/app/Http/ValidationRules/UniqueUserRule.php +++ b/app/Http/ValidationRules/UniqueUserRule.php @@ -60,6 +60,12 @@ class UniqueUserRule implements Rule */ private function checkIfEmailExists($email) : bool { - return MultiDB::checkUserEmailExists($email); + $current_db = config('database.default'); + + $result = MultiDB::checkUserEmailExists($email); + + MultiDB::setDb($current_db); + + return $result; } } diff --git a/app/Http/ValidationRules/ValidUserForCompany.php b/app/Http/ValidationRules/ValidUserForCompany.php index 967ca56591d7..cb3825704606 100644 --- a/app/Http/ValidationRules/ValidUserForCompany.php +++ b/app/Http/ValidationRules/ValidUserForCompany.php @@ -26,7 +26,14 @@ class ValidUserForCompany implements Rule */ public function passes($attribute, $value) { - return MultiDB::checkUserAndCompanyCoExist($value, auth()->user()->company()->company_key, auth()->user()->company()->id); + $current_db = config('database.default'); + + $result = MultiDB::checkUserAndCompanyCoExist($value, auth()->user()->company()->company_key, auth()->user()->company()->id); + + + MultiDB::setDb($current_db); + + return $result; } /** diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 05818dca6a3a..03116bebb1e8 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -298,7 +298,7 @@ class Import implements ShouldQueue $data = $this->transformCompanyData($data); if(Ninja::isHosted()) - $data['subdomain'] = $this->randomSubdomainGenerator(); + $data['subdomain'] = MultiDB::randomSubdomainGenerator(); $rules = (new UpdateCompanyRequest())->rules(); @@ -1721,28 +1721,7 @@ class Import implements ShouldQueue MultiDB::setDb($current_db); } - private function randomSubdomainGenerator() - { - do { - $length = 8; - $string = ''; - $vowels = array("a","e","i","o","u"); - $consonants = array( - 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', - 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' - ); - $max = $length / 2; - for ($i = 1; $i <= $max; $i++) - { - $string .= $consonants[rand(0,19)]; - $string .= $vowels[rand(0,4)]; - } - } - while(!MultiDB::checkDomainAvailable($string)); - - return $string; - } /* In V4 we use negative invoices (credits) and add then into the client balance. In V5, these sit off ledger and are applied later. This next section will check for credit balances and reduce the client balance so that the V5 balances are correct */ diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index 5ff52a7c3b6a..95ca4dfabf48 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -289,6 +289,34 @@ class MultiDB return false; } + public function randomSubdomainGenerator() + { + $current_db = config('database.default'); + + do { + $length = 8; + $string = ''; + $vowels = array("a","e","i","o","u"); + $consonants = array( + 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', + 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' + ); + + $max = $length / 2; + for ($i = 1; $i <= $max; $i++) + { + $string .= $consonants[rand(0,19)]; + $string .= $vowels[rand(0,4)]; + } + } + while(!self::checkDomainAvailable($string)); + + self::setDb($current_db); + + return $string; + } + + /** * @param $database */