Check subdomain is uniuqe across dbs

This commit is contained in:
Hillel Coren 2017-11-15 14:59:20 +02:00
parent 3439aa9a06
commit 82286a2339
4 changed files with 28 additions and 3 deletions

View File

@ -44,7 +44,7 @@ class SaveClientPortalSettings extends Request
if (Utils::isNinja()) {
if ($this->custom_link == 'subdomain') {
$subdomain = substr(strtolower($input['subdomain']), 0, MAX_SUBDOMAIN_LENGTH);
$input['subdomain'] = preg_replace('/[^a-zA-Z0-9_\-\.]/', '', $subdomain);
$input['subdomain'] = preg_replace('/[^a-zA-Z0-9\-\.]/', '', $subdomain);
$input['iframe_url'] = null;
} else {
$iframeURL = substr(strtolower($input['iframe_url']), 0, MAX_IFRAME_URL_LENGTH);

View File

@ -1711,6 +1711,13 @@ Account::creating(function ($account)
LookupAccount::createAccount($account->account_key, $account->company_id);
});
Account::updating(function ($account) {
$dirty = $account->getDirty();
if (array_key_exists('subdomain', $dirty)) {
LookupAccount::updateAccount($account->account_key, $account);
}
});
Account::updated(function ($account) {
// prevent firing event if the invoice/quote counter was changed
// TODO: remove once counters are moved to separate table

View File

@ -55,6 +55,24 @@ class LookupAccount extends LookupModel
return $this->lookupCompany->dbServer->name;
}
public static function updateAccount($accountKey, $account)
{
if (! env('MULTI_DB_ENABLED')) {
return;
}
$current = config('database.default');
config(['database.default' => DB_NINJA_LOOKUP]);
$lookupAccount = LookupAccount::whereAccountKey($accountKey)
->firstOrFail();
$lookupAccount->subdomain = $account->subdomain;
$lookupAccount->save();
config(['database.default' => $current]);
}
public static function validateField($field, $value, $account = false)
{
if (! env('MULTI_DB_ENABLED')) {
@ -62,7 +80,7 @@ class LookupAccount extends LookupModel
}
$current = config('database.default');
config(['database.default' => DB_NINJA_LOOKUP]);
$lookupAccount = LookupAccount::where($field, '=', $value)->first();

View File

@ -467,7 +467,7 @@ iframe.src = '{{ rtrim(SITE_URL ,'/') }}/view/'
var input = $('#subdomain');
var val = input.val();
if (!val) return;
val = val.replace(/[^a-zA-Z0-9_\-]/g, '').toLowerCase().substring(0, {{ MAX_SUBDOMAIN_LENGTH }});
val = val.replace(/[^a-zA-Z0-9\-]/g, '').toLowerCase().substring(0, {{ MAX_SUBDOMAIN_LENGTH }});
input.val(val);
}