mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Check subdomain is uniuqe across dbs
This commit is contained in:
parent
a615084f78
commit
3439aa9a06
@ -21,7 +21,7 @@ class InitLookup extends Command
|
|||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'ninja:init-lookup {--truncate=} {--validate=} {--update=} {--company_id=} {--page_size=100} {--database=db-ninja-1}';
|
protected $signature = 'ninja:init-lookup {--truncate=} {--subdomain} {--validate=} {--update=} {--company_id=} {--page_size=100} {--database=db-ninja-1}';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The console command description.
|
* The console command description.
|
||||||
@ -57,9 +57,12 @@ class InitLookup extends Command
|
|||||||
$database = $this->option('database');
|
$database = $this->option('database');
|
||||||
$dbServer = DbServer::whereName($database)->first();
|
$dbServer = DbServer::whereName($database)->first();
|
||||||
|
|
||||||
if ($this->option('truncate')) {
|
if ($this->option('subdomain')) {
|
||||||
|
$this->logMessage('Updating subdomains...');
|
||||||
|
$this->popuplateSubdomains();
|
||||||
|
} else if ($this->option('truncate')) {
|
||||||
|
$this->logMessage('Truncating data...');
|
||||||
$this->truncateTables();
|
$this->truncateTables();
|
||||||
$this->logMessage('Truncated');
|
|
||||||
} else {
|
} else {
|
||||||
config(['database.default' => $this->option('database')]);
|
config(['database.default' => $this->option('database')]);
|
||||||
|
|
||||||
@ -87,6 +90,30 @@ class InitLookup extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function popuplateSubdomains()
|
||||||
|
{
|
||||||
|
$data = [];
|
||||||
|
|
||||||
|
config(['database.default' => $this->option('database')]);
|
||||||
|
|
||||||
|
$accounts = DB::table('accounts')
|
||||||
|
->orderBy('id')
|
||||||
|
->where('subdomain', '!=', '')
|
||||||
|
->get(['account_key', 'subdomain']);
|
||||||
|
foreach ($accounts as $account) {
|
||||||
|
$data[$account->account_key] = $account->subdomain;
|
||||||
|
}
|
||||||
|
|
||||||
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
||||||
|
|
||||||
|
$validate = $this->option('validate');
|
||||||
|
$update = $this->option('update');
|
||||||
|
|
||||||
|
foreach ($data as $accountKey => $subdomain) {
|
||||||
|
LookupAccount::whereAccountKey($accountKey)->update(['subdomain' => $subdomain]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function initCompanies($dbServerId, $offset = 0)
|
private function initCompanies($dbServerId, $offset = 0)
|
||||||
{
|
{
|
||||||
$data = [];
|
$data = [];
|
||||||
@ -340,6 +367,7 @@ class InitLookup extends Command
|
|||||||
protected function getOptions()
|
protected function getOptions()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
['subdomain', null, InputOption::VALUE_OPTIONAL, 'Subdomain', null],
|
||||||
['truncate', null, InputOption::VALUE_OPTIONAL, 'Truncate', null],
|
['truncate', null, InputOption::VALUE_OPTIONAL, 'Truncate', null],
|
||||||
['company_id', null, InputOption::VALUE_OPTIONAL, 'Company Id', null],
|
['company_id', null, InputOption::VALUE_OPTIONAL, 'Company Id', null],
|
||||||
['page_size', null, InputOption::VALUE_OPTIONAL, 'Page Size', null],
|
['page_size', null, InputOption::VALUE_OPTIONAL, 'Page Size', null],
|
||||||
|
@ -769,11 +769,20 @@ class AccountController extends BaseController
|
|||||||
*/
|
*/
|
||||||
public function saveClientPortalSettings(SaveClientPortalSettings $request)
|
public function saveClientPortalSettings(SaveClientPortalSettings $request)
|
||||||
{
|
{
|
||||||
|
|
||||||
$account = $request->user()->account;
|
$account = $request->user()->account;
|
||||||
|
|
||||||
if($account->subdomain !== $request->subdomain)
|
// check subdomain is unique in the lookup tables
|
||||||
|
if (request()->subdomain) {
|
||||||
|
if (! \App\Models\LookupAccount::validateField('subdomain', request()->subdomain, $account)) {
|
||||||
|
return Redirect::to('settings/' . ACCOUNT_CLIENT_PORTAL)
|
||||||
|
->withError(trans('texts.subdomain_taken'))
|
||||||
|
->withInput();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($account->subdomain !== $request->subdomain) {
|
||||||
event(new SubdomainWasUpdated($account));
|
event(new SubdomainWasUpdated($account));
|
||||||
|
}
|
||||||
|
|
||||||
$account->fill($request->all());
|
$account->fill($request->all());
|
||||||
$account->client_view_css = $request->client_view_css;
|
$account->client_view_css = $request->client_view_css;
|
||||||
|
@ -55,4 +55,26 @@ class LookupAccount extends LookupModel
|
|||||||
return $this->lookupCompany->dbServer->name;
|
return $this->lookupCompany->dbServer->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function validateField($field, $value, $account = false)
|
||||||
|
{
|
||||||
|
if (! env('MULTI_DB_ENABLED')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$current = config('database.default');
|
||||||
|
|
||||||
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
||||||
|
|
||||||
|
$lookupAccount = LookupAccount::where($field, '=', $value)->first();
|
||||||
|
|
||||||
|
if ($account) {
|
||||||
|
$isValid = ! $lookupAccount || ($lookupAccount->account_key == $account->account_key);
|
||||||
|
} else {
|
||||||
|
$isValid = ! $lookupAccount;
|
||||||
|
}
|
||||||
|
|
||||||
|
config(['database.default' => $current]);
|
||||||
|
|
||||||
|
return $isValid;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class AddSubdomainToLookups extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('lookup_accounts', function ($table) {
|
||||||
|
$table->string('subdomain')->nullable()->unique();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('lookup_accounts', function ($table) {
|
||||||
|
$table->dropColumn('subdomain');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -2522,6 +2522,7 @@ $LANG = array(
|
|||||||
'set_self_hoat_url' => 'Self-Host URL',
|
'set_self_hoat_url' => 'Self-Host URL',
|
||||||
'local_storage_required' => 'Error: local storage is not available.',
|
'local_storage_required' => 'Error: local storage is not available.',
|
||||||
'your_password_reset_link' => 'Your Password Reset Link',
|
'your_password_reset_link' => 'Your Password Reset Link',
|
||||||
|
'subdomain_taken' => 'The subdomain is already in use',
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user