1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Refactoring how we set the DB in the request cycle

This commit is contained in:
David Bomba 2019-07-12 15:03:30 +10:00
parent 89797b0991
commit feff6a1c76
19 changed files with 122 additions and 10 deletions

View File

@ -28,7 +28,8 @@ class CompanyFactory
$company->company_key = $this->createHash();
$company->settings = new CompanySettings(CompanySettings::defaults());
$company->db = config('database.default');
$company->domain = '';
return $company;
}

View File

@ -34,15 +34,14 @@ class InvoiceController extends BaseController
}
/**
* Show the list of Invoices
* List Invoices
*
* @param \App\Filters\ContactInvoiceFilters $filters The filters
* @param \App\Filters\InvoiceFilters $filters The filters
*
* @return \Illuminate\Http\Response
*/
public function index(InvoiceFilters $filters)
{
//$invoices = Invoice::whereClientId(auth('contact')->user()->client->id);
$invoices = Invoice::filter($filters);

View File

@ -92,5 +92,6 @@ class Kernel extends HttpKernel
'api_secret_check' => \App\Http\Middleware\ApiSecretCheck::class,
'contact_token_auth' => \App\Http\Middleware\ContactTokenAuth::class,
'contact_db' => \App\Http\Middleware\ContactSetDb::class,
'domain_db' => \App\Http\Middleware\SetDomainNameDb::class,
];
}

View File

@ -0,0 +1,46 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Middleware;
use App\Libraries\MultiDB;
use Closure;
class SetDomainNameDb
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$error['error'] = ['message' => 'Database could not be set'];
/*
* Use the host name to set the active DB
**/
if( $request->getHttpHost() && config('ninja.db.multi_db_enabled') && ! MultiDB::findAndSetDbByDomain($request->getHttpHost()))
{
return response()->json(json_encode($error, JSON_PRETTY_PRINT) ,403);
}
return $next($request);
}
}

View File

@ -12,6 +12,7 @@
namespace App\Libraries;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\CompanyToken;
use App\Models\User;
@ -37,6 +38,24 @@ class MultiDB
}
public static function checkDomainAvailable($domain) : bool
{
if (! config('ninja.db.multi_db_enabled'))
{
return Company::whereDomain($domain)->get()->count() == 0;
}
//multi-db active
foreach (self::$dbs as $db)
{
if(Company::whereDomain($domain)->get()->count() >=1)
return false;
}
return true;
}
public static function checkUserEmailExists($email) : bool
{
@ -122,6 +141,25 @@ class MultiDB
}
public static function findAndSetDbByDomain($host) :bool
{
foreach (self::$dbs as $db)
{
if($company = Company::on($db)->whereDomain($host)->first())
{
self::setDb($company->db);
return true;
}
}
return false;
}
/**
* @param $database
*/

View File

@ -53,7 +53,7 @@ class Company extends BaseModel
'work_phone',
'work_email',
'country_id',
'subdomain',
'domain',
'vat_number',
'id_number',
'size_id',

View File

@ -154,7 +154,8 @@ class MultiDatabaseUserProvider implements UserProvider
* @return bool
*/
public function validateCredentials(UserContract $user, array $credentials)
{ Log::error('validateCredentials');
{
//Log::error('validateCredentials');
$plain = $credentials['password'];

View File

@ -77,7 +77,7 @@ class CompanyTransformer extends EntityTransformer
'work_phone' => $company->work_phone,
'work_email' => $company->work_email,
'country_id' => (int) $company->country_id,
'subdomain' => $company->subdomain,
'domain' => $company->domain,
'db' => $company->db,
'vat_number' => $company->vat_number,
'id_number' => $company->id_number,

View File

@ -142,7 +142,7 @@ class CreateUsersTable extends Migration
$table->string('work_phone')->nullable();
$table->string('work_email')->nullable();
$table->unsignedInteger('country_id')->nullable();
$table->string('subdomain')->nullable();
$table->string('domain')->nullable();
$table->string('db')->nullable();
$table->string('vat_number')->nullable();
$table->string('id_number')->nullable();

View File

@ -29,6 +29,7 @@ class RandomDataSeeder extends Seeder
$account = factory(\App\Models\Account::class)->create();
$company = factory(\App\Models\Company::class)->create([
'account_id' => $account->id,
'domain' => 'ninja.test',
]);
$account->default_company_id = $company->id;

View File

@ -28,6 +28,7 @@ class UsersTableSeeder extends Seeder
$account = factory(\App\Models\Account::class)->create();
$company = factory(\App\Models\Company::class)->create([
'account_id' => $account->id,
'domain' => 'ninja.test',
]);
$account->default_company_id = $company->id;

View File

@ -64,7 +64,7 @@ class ClientTest extends TestCase
$acc = $response->json();
Log::error($acc);
$account = Account::find($this->decodePrimaryKey($acc['data']['id']));
$token = $account->default_company->tokens->first()->token;
@ -191,7 +191,8 @@ Log::error($acc);
$account = factory(\App\Models\Account::class)->create();
$company = factory(\App\Models\Company::class)->create([
'account_id' => $account->id,
]);
'domain' => 'ninja.test',
]);
$account->default_company_id = $company->id;
$account->save();

View File

@ -51,6 +51,7 @@ class InvitationTest extends TestCase
$account = factory(\App\Models\Account::class)->create();
$company = factory(\App\Models\Company::class)->create([
'account_id' => $account->id,
'domain' => 'ninja.test',
]);
$account->default_company_id = $company->id;

View File

@ -49,6 +49,8 @@ class LoginTest extends TestCase
]);
$company = factory(\App\Models\Company::class)->make([
'account_id' => $account->id,
'domain' => 'ninja.test',
]);
$user->companies()->attach($company->id, [
@ -81,6 +83,8 @@ class LoginTest extends TestCase
]);
$company = factory(\App\Models\Company::class)->make([
'account_id' => $account->id,
'domain' => 'ninja.test',
]);
$user->companies()->attach($company->id, [
@ -111,6 +115,8 @@ class LoginTest extends TestCase
]);
$company = factory(\App\Models\Company::class)->make([
'account_id' => $account->id,
'domain' => 'ninja.test',
]);
$user->companies()->attach($company->id, [
@ -138,6 +144,8 @@ class LoginTest extends TestCase
$company = factory(\App\Models\Company::class)->make([
'account_id' => $account->id,
'domain' => 'ninja.test',
]);
$user->companies()->attach($company->id, [

View File

@ -39,10 +39,14 @@ class MultiDBUserTest extends TestCase
$company = factory(\App\Models\Company::class)->make([
'account_id' => $account->id,
'domain' => 'ninja.test',
]);
$company2 = factory(\App\Models\Company::class)->make([
'account_id' => $account2->id,
'domain' => 'ninja.test',
]);

View File

@ -39,10 +39,15 @@ class UniqueEmailTest extends TestCase
$company = factory(\App\Models\Company::class)->make([
'account_id' => $account->id,
'domain' => 'ninja.test',
]);
$company2 = factory(\App\Models\Company::class)->make([
'account_id' => $account2->id,
'domain' => 'ninja.test',
]);
$company->setHidden(['settings', 'settings_object']);

View File

@ -52,6 +52,7 @@ trait MockAccountData
$this->account = factory(\App\Models\Account::class)->create();
$this->company = factory(\App\Models\Company::class)->create([
'account_id' => $this->account->id,
'domain' => 'ninja.test',
]);
$this->account->default_company_id = $this->company->id;

View File

@ -38,6 +38,8 @@ class FactoryCreationTest extends TestCase
$this->account = factory(\App\Models\Account::class)->create();
$this->company = factory(\App\Models\Company::class)->create([
'account_id' => $this->account->id,
'domain' => 'ninja.test',
]);
$this->account->default_company_id = $this->company->id;

View File

@ -39,6 +39,8 @@ class GeneratesCounterTest extends TestCase
$account = factory(\App\Models\Account::class)->create();
$company = factory(\App\Models\Company::class)->create([
'account_id' => $account->id,
'domain' => 'ninja.test',
]);
$account->default_company_id = $company->id;
$account->save();