diff --git a/app/Factory/CompanyFactory.php b/app/Factory/CompanyFactory.php index 3cdd1e9ac2a4..6790058e8141 100644 --- a/app/Factory/CompanyFactory.php +++ b/app/Factory/CompanyFactory.php @@ -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; } diff --git a/app/Http/Controllers/Contact/InvoiceController.php b/app/Http/Controllers/Contact/InvoiceController.php index d3dd4aed9b63..d8d5e3c0cdf1 100644 --- a/app/Http/Controllers/Contact/InvoiceController.php +++ b/app/Http/Controllers/Contact/InvoiceController.php @@ -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); diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 9c63b72d3a42..d6da8ebe21e7 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -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, ]; } diff --git a/app/Http/Middleware/SetDomainNameDb.php b/app/Http/Middleware/SetDomainNameDb.php new file mode 100644 index 000000000000..852a543f2d9f --- /dev/null +++ b/app/Http/Middleware/SetDomainNameDb.php @@ -0,0 +1,46 @@ + '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); + } + + +} diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index f8b58d715ceb..ac6819c6b1dc 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -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 */ diff --git a/app/Models/Company.php b/app/Models/Company.php index 46cee31dc391..4ac787691034 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -53,7 +53,7 @@ class Company extends BaseModel 'work_phone', 'work_email', 'country_id', - 'subdomain', + 'domain', 'vat_number', 'id_number', 'size_id', diff --git a/app/Providers/MultiDatabaseUserProvider.php b/app/Providers/MultiDatabaseUserProvider.php index b21572899400..c25ee5f79919 100644 --- a/app/Providers/MultiDatabaseUserProvider.php +++ b/app/Providers/MultiDatabaseUserProvider.php @@ -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']; diff --git a/app/Transformers/CompanyTransformer.php b/app/Transformers/CompanyTransformer.php index 82e85ccba959..7ecb61df80af 100644 --- a/app/Transformers/CompanyTransformer.php +++ b/app/Transformers/CompanyTransformer.php @@ -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, diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index 09e83beeca91..ebf985062519 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -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(); diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index 7691f15309eb..bc08b4bd0672 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -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; diff --git a/database/seeds/UsersTableSeeder.php b/database/seeds/UsersTableSeeder.php index dd98293662b8..c35e5ecd76ad 100644 --- a/database/seeds/UsersTableSeeder.php +++ b/database/seeds/UsersTableSeeder.php @@ -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; diff --git a/tests/Feature/ClientTest.php b/tests/Feature/ClientTest.php index 7f7aeaa55eaf..7050092be0da 100644 --- a/tests/Feature/ClientTest.php +++ b/tests/Feature/ClientTest.php @@ -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(); diff --git a/tests/Feature/InvitationTest.php b/tests/Feature/InvitationTest.php index 947b2c882316..3374146a91fd 100644 --- a/tests/Feature/InvitationTest.php +++ b/tests/Feature/InvitationTest.php @@ -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; diff --git a/tests/Feature/LoginTest.php b/tests/Feature/LoginTest.php index 442727ce8144..093ff194dba6 100644 --- a/tests/Feature/LoginTest.php +++ b/tests/Feature/LoginTest.php @@ -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, [ diff --git a/tests/Integration/MultiDBUserTest.php b/tests/Integration/MultiDBUserTest.php index 16655490b19b..d64c694aeb29 100644 --- a/tests/Integration/MultiDBUserTest.php +++ b/tests/Integration/MultiDBUserTest.php @@ -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', + ]); diff --git a/tests/Integration/UniqueEmailTest.php b/tests/Integration/UniqueEmailTest.php index 23a4ceb736ae..e306cc6fc43f 100644 --- a/tests/Integration/UniqueEmailTest.php +++ b/tests/Integration/UniqueEmailTest.php @@ -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']); diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 6b473d06da4f..fac0dcafcbdf 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -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; diff --git a/tests/Unit/FactoryCreationTest.php b/tests/Unit/FactoryCreationTest.php index 8a40cc9112ba..178ed4b1965f 100644 --- a/tests/Unit/FactoryCreationTest.php +++ b/tests/Unit/FactoryCreationTest.php @@ -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; diff --git a/tests/Unit/GeneratesCounterTest.php b/tests/Unit/GeneratesCounterTest.php index 78fdc207a35b..ed4a82702426 100644 --- a/tests/Unit/GeneratesCounterTest.php +++ b/tests/Unit/GeneratesCounterTest.php @@ -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();