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:
parent
89797b0991
commit
feff6a1c76
@ -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;
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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,
|
||||
];
|
||||
}
|
||||
|
46
app/Http/Middleware/SetDomainNameDb.php
Normal file
46
app/Http/Middleware/SetDomainNameDb.php
Normal 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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
|
||||
*/
|
||||
|
@ -53,7 +53,7 @@ class Company extends BaseModel
|
||||
'work_phone',
|
||||
'work_email',
|
||||
'country_id',
|
||||
'subdomain',
|
||||
'domain',
|
||||
'vat_number',
|
||||
'id_number',
|
||||
'size_id',
|
||||
|
@ -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'];
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -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, [
|
||||
|
@ -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',
|
||||
|
||||
]);
|
||||
|
||||
|
||||
|
@ -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']);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user