Check subdomain is uniuqe across dbs

This commit is contained in:
Hillel Coren 2017-11-15 14:51:02 +02:00
parent a615084f78
commit 3439aa9a06
5 changed files with 97 additions and 5 deletions

View File

@ -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],

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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');
});
}
}

View File

@ -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',
);