1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #5800 from turbo124/v5-develop

FIxes for multidb
This commit is contained in:
David Bomba 2021-05-24 14:57:55 +10:00 committed by GitHub
commit 391302daaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 112 additions and 63 deletions

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;
}
/**

View File

@ -293,10 +293,14 @@ class CSVImport implements ShouldQueue {
],
];
$payment_repository->save(
$payment_data,
PaymentFactory::create( $this->company->id, $invoice->user_id, $invoice->client_id )
);
/* Make sure we don't apply any payments to invoices with a Zero Amount*/
if($invoice->amount > 0)
{
$payment_repository->save(
$payment_data,
PaymentFactory::create( $this->company->id, $invoice->user_id, $invoice->client_id )
);
}
}
}
}

View File

@ -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
*/

View File

@ -52,37 +52,38 @@ class MultiDB
public static function checkDomainAvailable($subdomain) : bool
{
if (! config('ninja.db.multi_db_enabled')) {
if (! config('ninja.db.multi_db_enabled'))
return Company::whereSubdomain($subdomain)->get()->count() == 0;
}
$current_db = config('database.default');
//multi-db active
foreach (self::$dbs as $db) {
if (Company::on($db)->whereSubdomain($subdomain)->get()->count() >= 1) {
self::setDb($current_db);
return false;
}
}
//self::setDefaultDatabase();
self::setDb($current_db);
return true;
}
public static function checkUserEmailExists($email) : bool
{
if (! config('ninja.db.multi_db_enabled')) {
if (! config('ninja.db.multi_db_enabled'))
return User::where(['email' => $email])->get()->count() >= 1 ?? false; // true >= 1 emails found / false -> == emails found
}
$current_db = config('database.default');
//multi-db active
foreach (self::$dbs as $db) {
if (User::on($db)->where(['email' => $email])->get()->count() >= 1) { // if user already exists, validation will fail
self::setDb($current_db);
return true;
}
}
self::setDefaultDatabase();
self::setDb($current_db);
return false;
}
@ -102,19 +103,21 @@ class MultiDB
*/
public static function checkUserAndCompanyCoExist($email, $company_key) :bool
{
$current_db = config('database.default');
foreach (self::$dbs as $db) {
if (User::on($db)->where(['email' => $email])->get()->count() >= 1) { // if user already exists, validation will fail
if (Company::on($db)->where(['company_key' => $company_key])->get()->count() >= 1) {
self::setDb($current_db);
return true;
} else {
self::setDefaultDatabase();
self::setDb($current_db);
return false;
}
}
}
self::setDefaultDatabase();
self::setDb($current_db);
return true;
}
@ -125,20 +128,21 @@ class MultiDB
*/
public static function hasUser(array $data) : ?User
{
if (! config('ninja.db.multi_db_enabled')) {
if (! config('ninja.db.multi_db_enabled'))
return User::where($data)->withTrashed()->first();
}
$current_db = config('database.default');
foreach (self::$dbs as $db) {
self::setDB($db);
if ($user = User::where($data)->withTrashed()->first())
if ($user = User::where($data)->withTrashed()->first()) {
return $user;
}
}
self::setDefaultDatabase();
self::setDb($current_db);
return null;
}
@ -149,107 +153,119 @@ class MultiDB
*/
public static function hasContact(string $email) : ?ClientContact
{
if (! config('ninja.db.multi_db_enabled')) {
if (! config('ninja.db.multi_db_enabled'))
return ClientContact::where('email', $email)->withTrashed()->first();
}
$current_db = config('database.default');
foreach (self::$dbs as $db) {
$user = ClientContact::on($db)->where('email', $email)->withTrashed()->first();
if ($user) {
self::setDB($db);
self::setDb($db);
return $user;
}
}
self::setDefaultDatabase();
self::setDB($current_db);
return null;
}
public static function contactFindAndSetDb($token) :bool
{
$current_db = config('database.default');
foreach (self::$dbs as $db) {
if ($ct = ClientContact::on($db)->whereRaw('BINARY `token`= ?', [$token])->first()) {
self::setDb($ct->company->db);
self::setDb($db);
return true;
}
}
self::setDefaultDatabase();
self::setDB($current_db);
return false;
}
public static function userFindAndSetDb($email) : bool
{
$current_db = config('database.default');
//multi-db active
foreach (self::$dbs as $db) {
if (User::on($db)->where('email', $email)->count() >= 1){
nlog("setting db {$db}");
self::setDb($db);
return true;
}
}
self::setDefaultDatabase();
self::setDB($current_db);
return false;
}
public static function findAndSetDb($token) :bool
{
$current_db = config('database.default');
foreach (self::$dbs as $db) {
if ($ct = CompanyToken::on($db)->whereRaw('BINARY `token`= ?', [$token])->first()) {
self::setDb($ct->company->db);
return true;
}
}
self::setDefaultDatabase();
self::setDB($current_db);
return false;
}
public static function findAndSetDbByCompanyKey($company_key) :bool
{
$current_db = config('database.default');
foreach (self::$dbs as $db) {
if ($company = Company::on($db)->where('company_key', $company_key)->first()) {
self::setDb($company->db);
return true;
}
}
self::setDefaultDatabase();
self::setDB($current_db);
return false;
}
public static function findAndSetDbByContactKey($contact_key) :bool
{
$current_db = config('database.default');
foreach (self::$dbs as $db) {
if ($client_contact = ClientContact::on($db)->where('contact_key', $contact_key)->first()) {
self::setDb($client_contact->company->db);
return true;
}
}
self::setDefaultDatabase();
self::setDB($current_db);
return false;
}
public static function findAndSetDbByClientHash($client_hash) :bool
{
$current_db = config('database.default');
foreach (self::$dbs as $db) {
if ($client = Client::on($db)->where('client_hash', $client_hash)->first()) {
self::setDb($client->company->db);
return true;
}
}
self::setDefaultDatabase();
self::setDB($current_db);
return false;
}
@ -260,6 +276,8 @@ class MultiDB
if (! config('ninja.db.multi_db_enabled'))
return (Company::where($query_array)->exists() === true);
$current_db = config('database.default');
foreach (self::$dbs as $db) {
if ($company = Company::on($db)->where($query_array)->first()) {
self::setDb($company->db);
@ -267,7 +285,7 @@ class MultiDB
}
}
self::setDefaultDatabase();
self::setDB($current_db);
return false;
}
@ -275,20 +293,48 @@ class MultiDB
public static function findAndSetDbByInvitation($entity, $invitation_key)
{
$class = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
$current_db = config('database.default');
foreach (self::$dbs as $db) {
if ($invite = $class::on($db)->whereRaw('BINARY `key`= ?', [$invitation_key])->first()) {
self::setDb($db);
return true;
}
}
self::setDefaultDatabase();
self::setDB($current_db);
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
*/