FIxes for multidb

This commit is contained in:
David Bomba 2021-05-24 14:24:16 +10:00
parent 76d8ec99c9
commit 3c99c65033
5 changed files with 52 additions and 25 deletions

View File

@ -12,7 +12,9 @@
namespace App\Factory; namespace App\Factory;
use App\DataMapper\CompanySettings; use App\DataMapper\CompanySettings;
use App\Libraries\MultiDB;
use App\Models\Company; use App\Models\Company;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
class CompanyFactory class CompanyFactory
@ -33,7 +35,12 @@ class CompanyFactory
$company->db = config('database.default'); $company->db = config('database.default');
//$company->custom_fields = (object) ['invoice1' => '1', 'invoice2' => '2', 'client1'=>'3']; //$company->custom_fields = (object) ['invoice1' => '1', 'invoice2' => '2', 'client1'=>'3'];
$company->custom_fields = (object) []; $company->custom_fields = (object) [];
if(Ninja::isHosted())
$company->subdomain = MultiDB::randomSubdomainGenerator();
else
$company->subdomain = ''; $company->subdomain = '';
$company->enabled_modules = config('ninja.enabled_modules'); //32767;//8191; //4095 $company->enabled_modules = config('ninja.enabled_modules'); //32767;//8191; //4095
$company->default_password_timeout = 1800000; $company->default_password_timeout = 1800000;

View File

@ -60,6 +60,12 @@ class UniqueUserRule implements Rule
*/ */
private function checkIfEmailExists($email) : bool private function checkIfEmailExists($email) : bool
{ {
return MultiDB::checkUserEmailExists($email); $current_db = config('database.default');
$result = MultiDB::checkUserEmailExists($email);
MultiDB::setDb($current_db);
return $result;
} }
} }

View File

@ -26,7 +26,14 @@ class ValidUserForCompany implements Rule
*/ */
public function passes($attribute, $value) 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;
} }
/** /**

View File

@ -298,7 +298,7 @@ class Import implements ShouldQueue
$data = $this->transformCompanyData($data); $data = $this->transformCompanyData($data);
if(Ninja::isHosted()) if(Ninja::isHosted())
$data['subdomain'] = $this->randomSubdomainGenerator(); $data['subdomain'] = MultiDB::randomSubdomainGenerator();
$rules = (new UpdateCompanyRequest())->rules(); $rules = (new UpdateCompanyRequest())->rules();
@ -1721,28 +1721,7 @@ class Import implements ShouldQueue
MultiDB::setDb($current_db); 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. /* 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 This next section will check for credit balances and reduce the client balance so that the V5 balances are correct
*/ */

View File

@ -289,6 +289,34 @@ class MultiDB
return false; 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 * @param $database
*/ */