diff --git a/app/Console/Commands/InitLookup.php b/app/Console/Commands/InitLookup.php index aa52ec4f4a35..f37de078be2f 100644 --- a/app/Console/Commands/InitLookup.php +++ b/app/Console/Commands/InitLookup.php @@ -21,7 +21,7 @@ class InitLookup extends Command * * @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. @@ -57,9 +57,12 @@ class InitLookup extends Command $database = $this->option('database'); $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->logMessage('Truncated'); } else { 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) { $data = []; @@ -340,6 +367,7 @@ class InitLookup extends Command protected function getOptions() { return [ + ['subdomain', null, InputOption::VALUE_OPTIONAL, 'Subdomain', null], ['truncate', null, InputOption::VALUE_OPTIONAL, 'Truncate', null], ['company_id', null, InputOption::VALUE_OPTIONAL, 'Company Id', null], ['page_size', null, InputOption::VALUE_OPTIONAL, 'Page Size', null], diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index f41b9e4cadf3..64b553e58732 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -769,11 +769,20 @@ class AccountController extends BaseController */ public function saveClientPortalSettings(SaveClientPortalSettings $request) { - $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)); + } $account->fill($request->all()); $account->client_view_css = $request->client_view_css; diff --git a/app/Models/LookupAccount.php b/app/Models/LookupAccount.php index 7d5cd8e8c12d..605769de00c6 100644 --- a/app/Models/LookupAccount.php +++ b/app/Models/LookupAccount.php @@ -55,4 +55,26 @@ class LookupAccount extends LookupModel 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; + } } diff --git a/database/migrations/2017_11_15_114422_add_subdomain_to_lookups.php b/database/migrations/2017_11_15_114422_add_subdomain_to_lookups.php new file mode 100644 index 000000000000..ce88a26b08e4 --- /dev/null +++ b/database/migrations/2017_11_15_114422_add_subdomain_to_lookups.php @@ -0,0 +1,32 @@ +string('subdomain')->nullable()->unique(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('lookup_accounts', function ($table) { + $table->dropColumn('subdomain'); + }); + } +} diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 315cbc56786c..cda9b7ff7225 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2522,6 +2522,7 @@ $LANG = array( 'set_self_hoat_url' => 'Self-Host URL', 'local_storage_required' => 'Error: local storage is not available.', 'your_password_reset_link' => 'Your Password Reset Link', + 'subdomain_taken' => 'The subdomain is already in use', );